Skip to content

Commit

Permalink
fix : 수정
Browse files Browse the repository at this point in the history
fix : 수정
  • Loading branch information
phyuna0525 authored Jun 2, 2024
2 parents c456963 + 8c897a5 commit b3780f9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
27 changes: 21 additions & 6 deletions src/api/afterManage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import {
import apiError from "@/hook/errorHandling";

export const GetClubList = (club: string) => {
const { handleError } = apiError();
return useQuery<ClubList[]>({
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);
}
},
});
};
Expand All @@ -33,11 +38,16 @@ export const FixStatus = () => {
};

export const AllStudent = () => {
const { handleError } = apiError();
return useQuery<Type[]>({
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);
}
},
});
};
Expand All @@ -56,11 +66,16 @@ export const PostStudent = () => {
};

export const GetAfterStudent = () => {
const { handleError } = apiError();
return useQuery<AfterStudent[]>({
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);
}
},
});
};
Expand Down
13 changes: 9 additions & 4 deletions src/api/classChange/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { FloorClass, changeClass } from "../type";
import apiError from "@/hook/errorHandling";

export const AcceptClassChange = (floor: number) => {
const { handleError } = apiError();
return useQuery<FloorClass[]>({
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);
}
},
});
};
Expand Down
4 changes: 1 addition & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
16 changes: 12 additions & 4 deletions src/api/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,13 +16,14 @@ interface Token {
}

export const useLogin = () => {
const BASEURL = process.env.NEXT_PUBLIC_API_KEY;
const [accessToken, setAccessToken] = useState<string | null>(null);
const [refreshToken, setRefreshToken] = useState<string | null>(null);

const loginMutation = useMutation<Token, Error, Login>({
mutationFn: (param: Login) => {
return instance
.post<Token>("/admin/login", {
return axios
.post<Token>(`${BASEURL}/admin/login`, {
...param,
})
.then((response) => {
Expand Down Expand Up @@ -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);
}
},
});
};
37 changes: 28 additions & 9 deletions src/api/main/index.ts
Original file line number Diff line number Diff line change
@@ -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<string>({
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<string>({
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<CountOutListType>({
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);
}
},
});
};
14 changes: 1 addition & 13 deletions src/app/provider.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
Expand Down
4 changes: 3 additions & 1 deletion src/hook/errorHandling.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useRouter } from "next/navigation";
import { useCallback } from "react";

type ErrorHandler = () => void;
Expand All @@ -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 = () => {
Expand Down

0 comments on commit b3780f9

Please sign in to comment.