Skip to content

Commit

Permalink
[FEAT] 관리자가 아닐때 어드민 페이지 로그인 시도시 alert 창 띄우기
Browse files Browse the repository at this point in the history
- 기존에 AxiosResponse로 감싸서 반환할시에, 받아온 data의 message를 읽어올 수 없었습니다.
- 따라서 response.dto라는 반환용 dto를 만들어서, 받아온 data를 감싸 반환하도록 수정하였습니다.
  • Loading branch information
boeunLee committed May 15, 2024
1 parent d9d65cf commit 2d3ccf9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/MagainzeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MagazineList = () => {
const magazineList = useRecoilValue(magazineListState);

useEffect(() => {
if (magazineListResult) {
if (magazineListResult?.data) {
setMagazineList(magazineListResult.data);
}
}, [magazineListResult]);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/api/dto/response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 반환용 DTO
*/
export class ResponseDto<T> {
message?: string;
success?: boolean;
data?: T;

constructor(data?: any) {
if (data.data) this.data = data.data;
if (data.message) this.message = data.message;
if (data.success) this.success = data.success;
}
}
20 changes: 18 additions & 2 deletions src/lib/hooks/useGetMagazineList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { AxiosResponse } from 'axios';
import type { AxiosResponse } from 'axios';
import useSWR from 'swr';

import { gamGetFetcher } from '../axios';
import { magazineListData } from '../../types/magazine';
import { useEffect } from 'react';
import { clearUserSession } from '../token';
import { useNavigate } from 'react-router-dom';
import { ResponseDto } from '../api/dto/response.dto';

const useGetMagazineList = () => {
const { data, error } = useSWR<AxiosResponse<magazineListData[]>>(`/api/v1/admin/magazine/list`, gamGetFetcher, {
const navigate = useNavigate();
const { data, error } = useSWR<ResponseDto<magazineListData[]>>(`/api/v1/admin/magazine/list`, gamGetFetcher, {
errorRetryCount: 3,
});

/**
* 관리자 권한이 없으면 alert창 띄우고 로그인 화면으로 돌아감
*/
useEffect(() => {
if (data?.message === '관리자 권한이 없습니다.') {
window.alert(data.message);
clearUserSession();
navigate('/');
}
}, [data, error]);

return {
magazineListResult: data,
isLoading: !data && !error,
Expand Down

0 comments on commit 2d3ccf9

Please sign in to comment.