Skip to content

Commit b684ca9

Browse files
committed
๐Ÿ› ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ๋ฐ ์‚ญ์ œ ์‹œ ISR revalidate ํŠธ๋ฆฌ๊ฑฐ ์ถ”๊ฐ€
1 parent 4a3ec54 commit b684ca9

4 files changed

Lines changed: 78 additions & 6 deletions

File tree

โ€Žapps/web/src/apis/community/deletePost.tsโ€Ž

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,64 @@ import { useMutation, useQueryClient } from "@tanstack/react-query";
33
import type { AxiosError, AxiosResponse } from "axios";
44
import { useRouter } from "next/navigation";
55

6+
import useAuthStore from "@/lib/zustand/useAuthStore";
67
import { toast } from "@/lib/zustand/useToastStore";
78
import { CommunityQueryKeys, communityApi, type DeletePostResponse } from "./api";
89

10+
interface DeletePostVariables {
11+
postId: number;
12+
boardCode?: string;
13+
}
14+
15+
/**
16+
* @description ISR ํŽ˜์ด์ง€๋ฅผ revalidateํ•˜๋Š” ํ•จ์ˆ˜
17+
* @param boardCode - ๊ฒŒ์‹œํŒ ์ฝ”๋“œ
18+
* @param accessToken - ์‚ฌ์šฉ์ž ์ธ์ฆ ํ† ํฐ
19+
*/
20+
const revalidateCommunityPage = async (boardCode: string, accessToken: string) => {
21+
try {
22+
if (!accessToken) {
23+
console.warn("Revalidation skipped: No access token available");
24+
return;
25+
}
26+
27+
await fetch("/api/revalidate", {
28+
method: "POST",
29+
headers: {
30+
"Content-Type": "application/json",
31+
Authorization: `Bearer ${accessToken}`,
32+
},
33+
body: JSON.stringify({ boardCode }),
34+
});
35+
} catch (error) {
36+
console.error("Revalidate failed:", error);
37+
}
38+
};
39+
940
/**
1041
* @description ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ๋ฅผ ์œ„ํ•œ useMutation ์ปค์Šคํ…€ ํ›…
1142
*/
1243
const useDeletePost = () => {
1344
const router = useRouter();
1445
const queryClient = useQueryClient();
46+
const { accessToken } = useAuthStore();
1547

16-
return useMutation<AxiosResponse<DeletePostResponse>, AxiosError, number>({
17-
mutationFn: communityApi.deletePost,
18-
onSuccess: () => {
48+
return useMutation<AxiosResponse<DeletePostResponse>, AxiosError, DeletePostVariables>({
49+
mutationFn: ({ postId }) => communityApi.deletePost(postId),
50+
onSuccess: async (_result, variables) => {
1951
// 'posts' ์ฟผ๋ฆฌ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™”ํ•˜์—ฌ
2052
// ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
2153
queryClient.invalidateQueries({ queryKey: [CommunityQueryKeys.posts] });
2254

55+
// ISR ํŽ˜์ด์ง€ revalidate
56+
if (variables.boardCode && accessToken) {
57+
await revalidateCommunityPage(variables.boardCode, accessToken);
58+
}
59+
2360
toast.success("๊ฒŒ์‹œ๊ธ€์ด ์„ฑ๊ณต์ ์œผ๋กœ ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
2461

2562
// ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํŽ˜์ด์ง€ ์ด๋™
26-
router.replace("/community/FREE");
63+
router.replace(`/community/${variables.boardCode || "FREE"}`);
2764
},
2865
onError: (error) => {
2966
console.error("๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์‹คํŒจ:", error);

โ€Žapps/web/src/apis/community/patchUpdatePost.tsโ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
11
import { useMutation, useQueryClient } from "@tanstack/react-query";
22
import type { AxiosError } from "axios";
33

4+
import useAuthStore from "@/lib/zustand/useAuthStore";
45
import { toast } from "@/lib/zustand/useToastStore";
56
import { CommunityQueryKeys, communityApi, type PostIdResponse, type PostUpdateRequest } from "./api";
67

78
interface UpdatePostVariables {
89
postId: number;
910
data: PostUpdateRequest;
11+
boardCode?: string;
1012
}
1113

14+
/**
15+
* @description ISR ํŽ˜์ด์ง€๋ฅผ revalidateํ•˜๋Š” ํ•จ์ˆ˜
16+
* @param boardCode - ๊ฒŒ์‹œํŒ ์ฝ”๋“œ
17+
* @param accessToken - ์‚ฌ์šฉ์ž ์ธ์ฆ ํ† ํฐ
18+
*/
19+
const revalidateCommunityPage = async (boardCode: string, accessToken: string) => {
20+
try {
21+
if (!accessToken) {
22+
console.warn("Revalidation skipped: No access token available");
23+
return;
24+
}
25+
26+
await fetch("/api/revalidate", {
27+
method: "POST",
28+
headers: {
29+
"Content-Type": "application/json",
30+
Authorization: `Bearer ${accessToken}`,
31+
},
32+
body: JSON.stringify({ boardCode }),
33+
});
34+
} catch (error) {
35+
console.error("Revalidate failed:", error);
36+
}
37+
};
38+
1239
/**
1340
* @description ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •์„ ์œ„ํ•œ useMutation ์ปค์Šคํ…€ ํ›…
1441
*/
1542
const useUpdatePost = () => {
1643
const queryClient = useQueryClient();
44+
const { accessToken } = useAuthStore();
1745

1846
return useMutation<PostIdResponse, AxiosError, UpdatePostVariables>({
1947
mutationFn: ({ postId, data }) => communityApi.updatePost(postId, data),
20-
onSuccess: (_result, variables) => {
48+
onSuccess: async (_result, variables) => {
2149
// ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ์ฟผ๋ฆฌ์™€ ๋ชฉ๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™”
2250
queryClient.invalidateQueries({ queryKey: [CommunityQueryKeys.posts, variables.postId] });
2351
queryClient.invalidateQueries({ queryKey: [CommunityQueryKeys.posts] });
52+
53+
// ISR ํŽ˜์ด์ง€ revalidate
54+
if (variables.boardCode && accessToken) {
55+
await revalidateCommunityPage(variables.boardCode, accessToken);
56+
}
57+
2458
toast.success("๊ฒŒ์‹œ๊ธ€์ด ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
2559
},
2660
onError: (error) => {

โ€Žapps/web/src/app/community/[boardCode]/[postId]/KebabMenu.tsxโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const KebabMenu = ({ postId, boardCode, isOwner = false }: KebabMenuProps) => {
113113
<button
114114
onClick={() => {
115115
if (confirm("์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?")) {
116-
deletePost(postId);
116+
deletePost({ postId, boardCode });
117117
}
118118
}}
119119
className={`flex w-full items-center gap-3 rounded-md px-3 py-2.5 text-gray-700 typo-regular-2 hover:bg-k-50`}

โ€Žapps/web/src/app/community/[boardCode]/[postId]/modify/PostModifyForm.tsxโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const PostModifyForm = ({
6363
updatePostMutation.mutate(
6464
{
6565
postId,
66+
boardCode,
6667
data: {
6768
postUpdateRequest: {
6869
postCategory: defaultPostCategory,

0 commit comments

Comments
ย (0)