@@ -3,27 +3,64 @@ import { useMutation, useQueryClient } from "@tanstack/react-query";
33import type { AxiosError , AxiosResponse } from "axios" ;
44import { useRouter } from "next/navigation" ;
55
6+ import useAuthStore from "@/lib/zustand/useAuthStore" ;
67import { toast } from "@/lib/zustand/useToastStore" ;
78import { 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 */
1243const 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 ) ;
0 commit comments