Skip to content

Commit

Permalink
Merge pull request #297 from codestates-seb/feat/useGet
Browse files Browse the repository at this point in the history
[Client] useGet에 제네릭 적용 및 403 에러 처리
  • Loading branch information
uxolrv authored Apr 5, 2023
2 parents f945900 + 1c453b3 commit ba9347c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions client/src/components/Etc/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,6 @@ export const ERROR_INFORMATION = `현재 정보를 불러올 수 없습니다.
잠시 후 다시 시도해주세요.`;

export const NO_ORDER_HISTORY = '주문 내역이 없습니다.';

export const TOKEN_EXPIRED_INFOMATION =
'로그인이 만료되었습니다. 다시 로그인해주세요.';
25 changes: 19 additions & 6 deletions client/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { useNavigate } from 'react-router-dom';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { useState } from 'react';
import { AxiosResponse } from 'axios';
import { AxiosResponse, AxiosError } from 'axios';
import { toast } from 'react-toastify';
import axiosInstance from '../utils/axiosInstance';
import { TOKEN_EXPIRED_INFOMATION } from '../components/Etc/Constants';

export function useGet(url: string, keyValue: string) {
const { isLoading, isError, isSuccess, data, error, refetch } = useQuery(
[keyValue],
() => axiosInstance.get(url),
);
export function useGet<T>(url: string, keyValue: string) {
const navigate = useNavigate();
const { isLoading, isError, isSuccess, data, error, refetch } = useQuery<
AxiosResponse<T>
>([keyValue], () => axiosInstance.get(url), {
onError: (errRes) => {
const { response } = errRes as AxiosError;

if (response?.status === 403) {
localStorage.clear();
navigate('/login');
toast.error(TOKEN_EXPIRED_INFOMATION);
}
},
});

return { isLoading, isError, isSuccess, data, error, refetch };
}
Expand Down

0 comments on commit ba9347c

Please sign in to comment.