Skip to content

Commit ab1f237

Browse files
authored
Merge pull request #177 from GDSC-DGU/refactor/#176
[Refactor] Notice, Admin 관련 API 코드 리팩토링
2 parents c488c4e + 59f07e0 commit ab1f237

File tree

24 files changed

+473
-381
lines changed

24 files changed

+473
-381
lines changed

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isInAppBrowser } from "./utils/isInApp";
88
import dayjs from "dayjs";
99
import "@/utils/dayjs";
1010
import { Wrapper } from "@googlemaps/react-wrapper";
11+
import GlobalLoading from "./components/common/GlobalLoading";
1112

1213
export default function App() {
1314
const inApp = isInAppBrowser();
@@ -23,6 +24,7 @@ export default function App() {
2324
<>
2425
<GlobalFCMListener />
2526
<RouterProvider router={router} />
27+
<GlobalLoading />
2628
</>
2729
)}
2830
</ThemeProvider>

src/api/admin/admin.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { useAuthStore } from "@/stores/useAuthStore";
21
import { sendRequest } from "../request";
32
import { adminInstance } from "../instance";
4-
import { Role } from "@/pages/admin/types/role";
53

64
interface LoginResponse {
75
accessToken: string;
@@ -13,20 +11,6 @@ interface LoginPayload {
1311
role: string;
1412
}
1513

16-
export const loginAPI = async (payload: LoginPayload) => {
17-
const response = await sendRequest<LoginResponse>(
18-
adminInstance,
19-
"POST",
20-
"/login",
21-
payload
22-
);
23-
24-
if (response.success) {
25-
useAuthStore
26-
.getState()
27-
.login(response.data.accessToken, payload.role as Role);
28-
return response;
29-
} else {
30-
return response;
31-
}
14+
export const adminLoginAPI = (payload: LoginPayload) => {
15+
return sendRequest<LoginResponse>(adminInstance, "POST", "/login", payload);
3216
};

src/api/admin/hooks/useLogin.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useMutation } from "@tanstack/react-query";
2+
import { adminLoginAPI } from "../admin";
3+
import { useAuthStore } from "@/stores/useAuthStore";
4+
import type { Role } from "@/pages/admin/types/role";
5+
6+
interface LoginPayload {
7+
loginId: string;
8+
password: string;
9+
role: string;
10+
}
11+
12+
export const useAdminLogin = () => {
13+
const login = useAuthStore((state) => state.login);
14+
15+
return useMutation({
16+
mutationFn: (payload: LoginPayload) => adminLoginAPI(payload),
17+
onSuccess: (response, variables) => {
18+
if (response.success) {
19+
login(response.data.accessToken, variables.role as Role);
20+
}
21+
},
22+
});
23+
};

src/api/notice/adminLost.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { sendRequest } from "@/api/request";
2+
import { adminInstance } from "@/api/instance";
3+
4+
export const LostPostAPI = async (formData: FormData) => {
5+
return sendRequest<string>(adminInstance, "POST", `/festa/losts`, formData);
6+
};
7+
8+
export const LostPatchAPI = async (formData: FormData) => {
9+
return sendRequest<string>(adminInstance, "PATCH", `/festa/losts`, formData, {
10+
"Content-Type": "multipart/form-data",
11+
});
12+
};
13+
14+
export const LostDeleteAPI = async (lostId: number) => {
15+
return sendRequest<string>(adminInstance, "DELETE", `/festa/losts/${lostId}`);
16+
};

src/api/notice/adminNotice.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { sendRequest } from "@/api/request";
2+
import { adminInstance } from "@/api/instance";
3+
4+
export const NoticePostAPI = async (formData: FormData) => {
5+
return sendRequest<string>(adminInstance, "POST", `/festa/notices`, formData);
6+
};
7+
8+
export const NoticePatchAPI = async (formData: FormData) => {
9+
return sendRequest<string>(
10+
adminInstance,
11+
"PATCH",
12+
`/festa/notices`,
13+
formData,
14+
{
15+
"Content-Type": "multipart/form-data",
16+
}
17+
);
18+
};
19+
20+
export const NoticeDeleteAPI = async (noticeId: number) => {
21+
return sendRequest<string>(
22+
adminInstance,
23+
"DELETE",
24+
`/festa/notices/${noticeId}`
25+
);
26+
};

src/api/notice/hooks/useLost.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
import { LostListAPI, LostDetailAPI } from "../lost";
3+
import { withDelayedGlobalLoading } from "@/utils/delayedGlobalLoading";
4+
5+
export const useLostList = () => {
6+
const query = useQuery({
7+
queryKey: ["lostList"],
8+
queryFn: () => withDelayedGlobalLoading(LostListAPI()),
9+
refetchInterval: 10000,
10+
refetchOnWindowFocus: true,
11+
staleTime: 0,
12+
});
13+
14+
return query;
15+
};
16+
17+
export const useLostDetail = (id: number) => {
18+
const query = useQuery({
19+
queryKey: ["lostDetail", id],
20+
queryFn: () => withDelayedGlobalLoading(LostDetailAPI(id)),
21+
enabled: !!id,
22+
refetchOnWindowFocus: true,
23+
staleTime: 10000,
24+
});
25+
26+
return query;
27+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useMutation, useQueryClient } from "@tanstack/react-query";
2+
import { LostPostAPI, LostDeleteAPI, LostPatchAPI } from "../adminLost";
3+
import { withDelayedGlobalLoading } from "@/utils/delayedGlobalLoading";
4+
5+
export const usePostLost = () => {
6+
const queryClient = useQueryClient();
7+
8+
return useMutation({
9+
mutationFn: (formData: FormData) =>
10+
withDelayedGlobalLoading(LostPostAPI(formData)),
11+
onSuccess: () => {
12+
queryClient.invalidateQueries({
13+
queryKey: ["lostList"],
14+
});
15+
},
16+
});
17+
};
18+
19+
export const usePatchLost = () => {
20+
const queryClient = useQueryClient();
21+
22+
return useMutation({
23+
mutationFn: (formData: FormData) =>
24+
withDelayedGlobalLoading(LostPatchAPI(formData)),
25+
onSuccess: () => {
26+
queryClient.invalidateQueries({ queryKey: ["lostList"] });
27+
},
28+
});
29+
};
30+
31+
export const useDeleteLost = () => {
32+
const queryClient = useQueryClient();
33+
34+
return useMutation({
35+
mutationFn: (noticeId: number) =>
36+
withDelayedGlobalLoading(LostDeleteAPI(noticeId)),
37+
onSuccess: () => {
38+
queryClient.invalidateQueries({ queryKey: ["lostList"] });
39+
},
40+
});
41+
};

src/api/notice/hooks/useNotice.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
import { NoticeDetailAPI, NoticeListAPI } from "../notice";
3+
import { withDelayedGlobalLoading } from "@/utils/delayedGlobalLoading";
4+
5+
export const useNoticeList = () => {
6+
const query = useQuery({
7+
queryKey: ["noticeList"],
8+
queryFn: () => withDelayedGlobalLoading(NoticeListAPI()),
9+
refetchOnWindowFocus: false,
10+
staleTime: 1000 * 60 * 5,
11+
});
12+
13+
return query;
14+
};
15+
16+
export const useNoticeDetail = (id: number) => {
17+
const query = useQuery({
18+
queryKey: ["noticeDetail", id],
19+
queryFn: () => withDelayedGlobalLoading(NoticeDetailAPI(id)),
20+
enabled: !!id,
21+
refetchOnWindowFocus: false,
22+
staleTime: 1000 * 60 * 5,
23+
});
24+
25+
return query;
26+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useMutation, useQueryClient } from "@tanstack/react-query";
2+
import { NoticePostAPI, NoticePatchAPI, NoticeDeleteAPI } from "../adminNotice";
3+
import { withDelayedGlobalLoading } from "@/utils/delayedGlobalLoading";
4+
5+
export const usePostNotice = () => {
6+
const queryClient = useQueryClient();
7+
8+
return useMutation({
9+
mutationFn: (formData: FormData) =>
10+
withDelayedGlobalLoading(NoticePostAPI(formData)),
11+
onSuccess: () => {
12+
queryClient.invalidateQueries({
13+
queryKey: ["noticeList"],
14+
});
15+
},
16+
});
17+
};
18+
19+
export const usePatchNotice = () => {
20+
const queryClient = useQueryClient();
21+
22+
return useMutation({
23+
mutationFn: (formData: FormData) =>
24+
withDelayedGlobalLoading(NoticePatchAPI(formData)),
25+
onSuccess: () => {
26+
queryClient.invalidateQueries({ queryKey: ["noticeList"] });
27+
},
28+
});
29+
};
30+
31+
export const useDeleteNotice = () => {
32+
const queryClient = useQueryClient();
33+
34+
return useMutation({
35+
mutationFn: (noticeId: number) =>
36+
withDelayedGlobalLoading(NoticeDeleteAPI(noticeId)),
37+
onSuccess: () => {
38+
queryClient.invalidateQueries({ queryKey: ["noticeList"] });
39+
},
40+
});
41+
};

src/api/notice/lost.ts

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { sendRequest } from "../request";
2-
import { lambdaInstance, adminInstance } from "../instance";
2+
import { lambdaInstance } from "../instance";
33
import type { LostItemType } from "@/pages/notice/types/lostItems";
4-
import { useLostStore } from "@/stores/useLostStore";
54
import type { LostDetailType } from "@/pages/lost-detail/types/lostDetails";
65

76
interface LostListResponse {
@@ -10,79 +9,9 @@ interface LostListResponse {
109
}
1110

1211
export const LostListAPI = async () => {
13-
const response = await sendRequest<LostListResponse>(
14-
lambdaInstance,
15-
"GET",
16-
"/losts"
17-
);
18-
19-
if (response.success) {
20-
useLostStore.getState().setLostList(response.data.item_lists);
21-
return response.data;
22-
} else {
23-
return response.error;
24-
}
12+
return sendRequest<LostListResponse>(lambdaInstance, "GET", "/losts");
2513
};
2614

2715
export const LostDetailAPI = async (lostId: number) => {
28-
const response = await sendRequest<LostDetailType>(
29-
lambdaInstance,
30-
"GET",
31-
`/losts/${lostId}`
32-
);
33-
34-
if (response.success) {
35-
useLostStore.getState().setLostDetail(response.data);
36-
return response.data;
37-
} else {
38-
return response.error;
39-
}
40-
};
41-
42-
export const LostPostAPI = async (formData: FormData) => {
43-
const response = await sendRequest<string>(
44-
adminInstance,
45-
"POST",
46-
`/festa/losts`,
47-
formData
48-
);
49-
50-
if (response.success) {
51-
await LostListAPI();
52-
return { success: true };
53-
} else {
54-
return { success: false };
55-
}
56-
};
57-
58-
export const LostPatchAPI = async (formData: FormData) => {
59-
const response = await sendRequest<string>(
60-
adminInstance,
61-
"PATCH",
62-
`/festa/losts`,
63-
formData,
64-
{ "Content-Type": "multipart/form-data" }
65-
);
66-
67-
if (response.success) {
68-
await LostListAPI();
69-
return { success: true };
70-
} else {
71-
return { success: false };
72-
}
73-
};
74-
75-
export const LostDeleteAPI = async (lostId: number) => {
76-
const response = await sendRequest<string>(
77-
adminInstance,
78-
"DELETE",
79-
`/festa/losts/${lostId}`
80-
);
81-
82-
if (response.data) {
83-
const newList = await LostListAPI();
84-
return { success: true, newList };
85-
} else {
86-
return { success: false };
87-
}
16+
return sendRequest<LostDetailType>(lambdaInstance, "GET", `/losts/${lostId}`);
8817
};

0 commit comments

Comments
 (0)