Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
phyuna0525 committed May 7, 2024
2 parents d7f948b + dc5682d commit 226ea82
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/api/afterManage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ClubList,
Type,
} from "../type";
import apiError from "@/hook/errorHandling";

export const GetClubList = (club: string) => {
return useQuery<ClubList[]>({
Expand All @@ -19,12 +20,13 @@ export const GetClubList = (club: string) => {
};

export const FixStatus = () => {
const { handleError } = apiError();
return useMutation<void, Error, ChangeStatus[]>({
mutationFn: async (param) => {
try {
await instance.patch(`/attendance/modify`, param);
} catch (error) {
console.log(error);
handleError(error);
}
},
});
Expand All @@ -41,12 +43,13 @@ export const AllStudent = () => {
};

export const PostStudent = () => {
const { handleError } = apiError();
return useMutation<void, Error, { student_num: string }[]>({
mutationFn: async (param) => {
try {
await instance.post(`/after`, param);
} catch (error) {
console.log(error);
handleError(error);
}
},
});
Expand All @@ -63,6 +66,7 @@ export const GetAfterStudent = () => {
};

export const AfterStudentDelete = () => {
const { handleError } = apiError();
return useMutation<void, Error, { id: string }>({
mutationFn: async ({ id }) => {
try {
Expand All @@ -72,20 +76,21 @@ export const AfterStudentDelete = () => {
},
});
} catch (error) {
console.log(error);
handleError(error);
throw new Error("Failed to delete student.");
}
},
});
};

export const CheckStatus = () => {
const { handleError } = apiError();
return useMutation<void, Error, ChangeClub[]>({
mutationFn: async (param) => {
try {
await instance.patch(`/attendance/modify`, param);
} catch (error) {
console.log(error);
handleError(error);
}
},
});
Expand Down
7 changes: 5 additions & 2 deletions src/api/attendanceCheck/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { instance } from "..";
import { AttendanceChack, ClubList } from "../type";
import apiError from "@/hook/errorHandling";

export const GetStudentsAttendance = () => {
const { handleError } = apiError();
return useMutation<ClubList[], Error, { grade: number; class: number }>({
mutationFn: async (param) => {
try {
Expand All @@ -11,19 +13,20 @@ export const GetStudentsAttendance = () => {
);
return response.data;
} catch (error) {
console.log(error);
handleError(error);
}
},
});
};

export const AttendanceSave = () => {
const { handleError } = apiError();
return useMutation<void, Error, AttendanceChack[]>({
mutationFn: async (param) => {
try {
await instance.patch("/attendance/modify", param);
} catch (error) {
console.log(error);
handleError(error);
}
},
});
Expand Down
7 changes: 5 additions & 2 deletions src/api/bug/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { instance } from "..";
import apiError from "@/hook/errorHandling";

interface BugProp {
title: string;
Expand All @@ -8,6 +9,7 @@ interface BugProp {
}

export const BugPost = () => {
const { handleError } = apiError();
return useMutation<void, Error, BugProp>({
mutationFn: async (param) => {
try {
Expand All @@ -17,13 +19,14 @@ export const BugPost = () => {
file_name: param.file_name,
});
} catch (error) {
console.log("오류");
handleError(error);
}
},
});
};

export const BugImg = () => {
const { handleError } = apiError();
return useMutation<string, Error, { file: File }>({
mutationFn: async (param) => {
try {
Expand All @@ -32,7 +35,7 @@ export const BugImg = () => {
const result = await instance.post(`/bug/upload`, formData);
return result.data;
} catch (error) {
console.log(error);
handleError(error);
throw new Error("파일 업로드 중에 오류가 발생했습니다.");
}
},
Expand Down
7 changes: 5 additions & 2 deletions src/api/classChange/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { instance } from "..";
import { FloorClass, changeClass } from "../type";
import apiError from "@/hook/errorHandling";

export const AcceptClassChange = (floor: number) => {
return useQuery<FloorClass[]>({
Expand All @@ -15,6 +16,7 @@ export const AcceptClassChange = (floor: number) => {
};

export const AcceptClass = () => {
const { handleError } = apiError();
return useMutation<void, Error, { status: string; id: string[] }>({
mutationFn: async (param) => {
try {
Expand All @@ -23,13 +25,14 @@ export const AcceptClass = () => {
ids: param.id,
});
} catch (error) {
console.log(error);
handleError(error);
}
},
});
};

export const GetFloor = () => {
const { handleError } = apiError();
return useMutation<changeClass[], void, { floor: number }>({
mutationFn: async (param) => {
try {
Expand All @@ -38,7 +41,7 @@ export const GetFloor = () => {
);
return response.data;
} catch (error) {
throw error;
handleError(error);
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/api/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import { useMutation, useQuery } from "@tanstack/react-query";
import { instance } from "..";
import { cookie } from '@/util/auth';
import { cookie } from "@/util/auth";

interface Login {
admin_id: string;
Expand Down
10 changes: 7 additions & 3 deletions src/api/outList/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { instance } from "..";
import { Accept, ClassProp, applicationOK, earlyReturnHome } from "../type";
import apiError from "@/hook/errorHandling";

export const Application = () => {
return useQuery<applicationOK[]>({
Expand All @@ -23,6 +24,7 @@ export const EarlyReturn = () => {
};

export const ReturnSchool = () => {
const { handleError } = apiError();
return useMutation<Error, void, { id: string }>({
mutationFn: async (param) => {
try {
Expand All @@ -31,13 +33,14 @@ export const ReturnSchool = () => {
);
return response.data;
} catch (error) {
throw error;
handleError(error);
}
},
});
};

export const GetClass = () => {
const { handleError } = apiError();
return useMutation<applicationOK[], Error, ClassProp>({
mutationFn: async (param: ClassProp) => {
try {
Expand All @@ -46,12 +49,13 @@ export const GetClass = () => {
);
return response.data;
} catch (error) {
throw error;
handleError(error);
}
},
});
};
export const OutAcceptApi = () => {
const { handleError } = apiError();
return useMutation<void, Error, Accept>({
mutationFn: async (param) => {
try {
Expand All @@ -62,7 +66,7 @@ export const OutAcceptApi = () => {
});
return response.data;
} catch (error) {
throw error;
handleError(error);
}
},
});
Expand Down
71 changes: 71 additions & 0 deletions src/hook/errorHandling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useCallback } from "react";

type ErrorHandler = () => void;

type HandleType = {
[key: string]: ErrorHandler | { [key: string]: ErrorHandler };
};

const apiError = () => {
const handle400: ErrorHandler = () => {
alert("400 잘못된 요청입니다");
};

const handle401: ErrorHandler = () => {
alert("401 인증에 실패했습니다");
};

const handle403: ErrorHandler = () => {
alert("403 권한이 없습니다");
};

const handle404: ErrorHandler = () => {
alert("404 찾을 수 없습니다");
};

const handle503: ErrorHandler = () => {
alert("503 : 관리자에게 문의하세요");
};

const handle500: ErrorHandler = () => {
alert("500 : 관리자에게 문의주세요");
};

const handleDefault: ErrorHandler = () => {
alert("알 수 없는 오류가 발생했습니다");
};

const defaultHandlers: HandleType = {
"400": handle400,
"401": handle401,
"403": handle403,
"404": handle404,
"500": handle500,
"503": handle503,
default: handleDefault,
};

const handleError = useCallback((error: any) => {
const httpStatus = error.response?.status;
const errorMessage = error.data?.errorMessage;

if (httpStatus) {
const selectedHandler: ErrorHandler | { [key: string]: ErrorHandler } =
defaultHandlers[httpStatus] || defaultHandlers.default;
const specificErrorHandler = errorMessage
? (selectedHandler as { [key: string]: ErrorHandler })[errorMessage]
: null;
if (specificErrorHandler) {
specificErrorHandler();
} else {
(selectedHandler as ErrorHandler)();
}
} else {
alert(`오류가 발생했습니다: ${error.message}`);
}
}, []);

return { handleError };
};

export default apiError;

0 comments on commit 226ea82

Please sign in to comment.