From 8c897a517b38b1892722df1d00d88b001751c407 Mon Sep 17 00:00:00 2001 From: hyuna Date: Sun, 2 Jun 2024 22:47:01 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/afterManage/index.ts | 27 ++++++++++++++++++++------ src/api/classChange/index.ts | 13 +++++++++---- src/api/index.ts | 4 +--- src/api/login/index.ts | 16 ++++++++++++---- src/api/main/index.ts | 37 +++++++++++++++++++++++++++--------- src/app/provider.tsx | 14 +------------- src/hook/errorHandling.ts | 4 +++- 7 files changed, 75 insertions(+), 40 deletions(-) diff --git a/src/api/afterManage/index.ts b/src/api/afterManage/index.ts index 70a824e..523a2d1 100644 --- a/src/api/afterManage/index.ts +++ b/src/api/afterManage/index.ts @@ -10,11 +10,16 @@ import { import apiError from "@/hook/errorHandling"; export const GetClubList = (club: string) => { + const { handleError } = apiError(); return useQuery({ queryKey: ["GetClubList", club], queryFn: async () => { - const response = await instance.get(`/attendance/club?club=${club}`); - return response.data; + try { + const response = await instance.get(`/attendance/club?club=${club}`); + return response.data; + } catch (error) { + handleError(error); + } }, }); }; @@ -33,11 +38,16 @@ export const FixStatus = () => { }; export const AllStudent = () => { + const { handleError } = apiError(); return useQuery({ queryKey: ["AllStudent"], queryFn: async () => { - const response = await instance.get(`/after/search`); - return response.data; + try { + const response = await instance.get(`/after/search`); + return response.data; + } catch (error) { + handleError(error); + } }, }); }; @@ -56,11 +66,16 @@ export const PostStudent = () => { }; export const GetAfterStudent = () => { + const { handleError } = apiError(); return useQuery({ queryKey: ["GetAfterStudent"], queryFn: async () => { - const response = await instance.get(`/after/all`); - return response.data; + try { + const response = await instance.get(`/after/all`); + return response.data; + } catch (error) { + handleError(error); + } }, }); }; diff --git a/src/api/classChange/index.ts b/src/api/classChange/index.ts index 8507b3d..dac036f 100644 --- a/src/api/classChange/index.ts +++ b/src/api/classChange/index.ts @@ -4,13 +4,18 @@ import { FloorClass, changeClass } from "../type"; import apiError from "@/hook/errorHandling"; export const AcceptClassChange = (floor: number) => { + const { handleError } = apiError(); return useQuery({ queryKey: ["AcceptClassChange", floor], queryFn: async () => { - const response = await instance.get( - `/class-room/floor?floor=${floor}&status=QUIET` - ); - return response.data; + try { + const response = await instance.get( + `/class-room/floor?floor=${floor}&status=QUIET` + ); + return response.data; + } catch (error) { + handleError(error); + } }, }); }; diff --git a/src/api/index.ts b/src/api/index.ts index 6e574b1..9bd3841 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -17,9 +17,7 @@ instance.interceptors.request.use( (config) => { if (typeof window !== "undefined") { const accessToken = cookie.get("access_token"); - if (accessToken) { - config.headers.Authorization = `Bearer ${accessToken}`; - } + config.headers.Authorization = `Bearer ${accessToken}`; } return config; }, diff --git a/src/api/login/index.ts b/src/api/login/index.ts index b80f6c8..68f677c 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -2,6 +2,8 @@ import { useState } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { instance } from ".."; import { cookie } from "@/util/auth"; +import axios from "axios"; +import apiError from "@/hook/errorHandling"; interface Login { admin_id: string; @@ -14,13 +16,14 @@ interface Token { } export const useLogin = () => { + const BASEURL = process.env.NEXT_PUBLIC_API_KEY; const [accessToken, setAccessToken] = useState(null); const [refreshToken, setRefreshToken] = useState(null); const loginMutation = useMutation({ mutationFn: (param: Login) => { - return instance - .post("/admin/login", { + return axios + .post(`${BASEURL}/admin/login`, { ...param, }) .then((response) => { @@ -50,11 +53,16 @@ export const useLogin = () => { }; export const GetTeacherName = () => { + const { handleError } = apiError(); return useQuery<{ name: string }>({ queryKey: ["GetTeacherName"], queryFn: async () => { - const response = await instance.get(`/admin/my-name`); - return response.data; + try { + const response = await instance.get(`/admin/my-name`); + return response.data; + } catch (error) { + handleError(error); + } }, }); }; diff --git a/src/api/main/index.ts b/src/api/main/index.ts index a60e26a..fb3691b 100644 --- a/src/api/main/index.ts +++ b/src/api/main/index.ts @@ -1,36 +1,55 @@ import { useQuery } from "@tanstack/react-query"; import { instance } from ".."; import { CountOutListType } from "../type"; +import apiError from "@/hook/errorHandling"; export const GetName = () => { + const { handleError } = apiError(); return useQuery({ queryKey: ["name"], queryFn: async () => { - const result = await instance.get("/admin/my-name"); - localStorage.setItem("name", result.data.name); - localStorage.setItem("grade", JSON.stringify(result.data.grade)); - localStorage.setItem("class_num", JSON.stringify(result.data.class_num)); - return result.data.name; + try { + const result = await instance.get("/admin/my-name"); + localStorage.setItem("name", result.data.name); + localStorage.setItem("grade", JSON.stringify(result.data.grade)); + localStorage.setItem( + "class_num", + JSON.stringify(result.data.class_num) + ); + return result.data.name; + } catch (error) { + handleError(error); + } }, }); }; export const GetTodaydirector = () => { + const { handleError } = apiError(); return useQuery({ queryKey: ["director"], queryFn: async () => { - const result = await instance.get("self-study/admin"); - return result.data; + try { + const result = await instance.get("self-study/admin"); + return result.data; + } catch (error) { + handleError(error); + } }, }); }; export const CountOutList = () => { + const { handleError } = apiError(); return useQuery({ queryKey: ["outList"], queryFn: async () => { - const result = await instance.get("application/status"); - return result.data; + try { + const result = await instance.get("application/status"); + return result.data; + } catch (error) { + handleError(error); + } }, }); }; diff --git a/src/app/provider.tsx b/src/app/provider.tsx index bd7b1ee..36ee0f4 100644 --- a/src/app/provider.tsx +++ b/src/app/provider.tsx @@ -1,24 +1,12 @@ "use client"; -import { cookie } from "@/util/auth"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import { useRouter } from "next/navigation"; -import React, { useEffect } from "react"; +import React from "react"; interface ProviderProps { children: React.ReactNode; } export const Provider = ({ children }: ProviderProps) => { - const router = useRouter(); - const accessToken = cookie.get("access_token"); - - useEffect(() => { - if (!accessToken) { - router.push("/login"); - alert("로그인 후 이용해 주세요"); - } - }, [accessToken, router]); - const queryClient = new QueryClient(); return ( {children} diff --git a/src/hook/errorHandling.ts b/src/hook/errorHandling.ts index ae7b971..e09c1cd 100644 --- a/src/hook/errorHandling.ts +++ b/src/hook/errorHandling.ts @@ -1,3 +1,4 @@ +import { useRouter } from "next/navigation"; import { useCallback } from "react"; type ErrorHandler = () => void; @@ -7,12 +8,13 @@ type HandleType = { }; const apiError = () => { + const router = useRouter(); const handle400: ErrorHandler = () => { alert("400 잘못된 요청입니다"); }; const handle401: ErrorHandler = () => { - alert("401 인증에 실패했습니다"); + router.push("login"); }; const handle403: ErrorHandler = () => {