@@ -6,19 +6,22 @@ import { QueryKeys } from "./queryKey";
66
77import { PostCreateRequest , PostIdResponse } from "@/types/community" ;
88
9+ import useAuthStore from "@/lib/zustand/useAuthStore" ;
910import { toast } from "@/lib/zustand/useToastStore" ;
1011import { useMutation , useQueryClient } from "@tanstack/react-query" ;
1112
1213/**
1314 * @description 게시글 생성 API 함수
1415 * @param request - 게시글 생성 요청 데이터
15- * @returns Promise<PostIdResponse>
16+ * @returns Promise<PostIdResponse & { boardCode: string } >
1617 */
17- const createPost = async ( request : PostCreateRequest ) : Promise < PostIdResponse > => {
18+ const createPost = async (
19+ request : PostCreateRequest
20+ ) : Promise < PostIdResponse & { boardCode : string } > => {
1821 const convertedRequest : FormData = new FormData ( ) ;
1922 convertedRequest . append (
2023 "postCreateRequest" ,
21- new Blob ( [ JSON . stringify ( request . postCreateRequest ) ] , { type : "application/json" } ) ,
24+ new Blob ( [ JSON . stringify ( request . postCreateRequest ) ] , { type : "application/json" } )
2225 ) ;
2326 request . file . forEach ( ( file ) => {
2427 convertedRequest . append ( "file" , file ) ;
@@ -27,20 +30,56 @@ const createPost = async (request: PostCreateRequest): Promise<PostIdResponse> =
2730 const response : AxiosResponse < PostIdResponse > = await axiosInstance . post ( `/posts` , convertedRequest , {
2831 headers : { "Content-Type" : "multipart/form-data" } ,
2932 } ) ;
30- return response . data ;
33+
34+ return {
35+ ...response . data ,
36+ boardCode : request . postCreateRequest . boardCode ,
37+ } ;
38+ } ;
39+
40+ /**
41+ * @description ISR 페이지를 revalidate하는 함수
42+ * @param boardCode - 게시판 코드
43+ * @param accessToken - 사용자 인증 토큰
44+ */
45+ const revalidateCommunityPage = async ( boardCode : string , accessToken : string ) => {
46+ try {
47+ if ( ! accessToken ) {
48+ console . warn ( "Revalidation skipped: No access token available" ) ;
49+ return ;
50+ }
51+
52+ await fetch ( "/api/revalidate" , {
53+ method : "POST" ,
54+ headers : {
55+ "Content-Type" : "application/json" ,
56+ Authorization : `Bearer ${ accessToken } ` ,
57+ } ,
58+ body : JSON . stringify ( { boardCode } ) ,
59+ } ) ;
60+ } catch ( error ) {
61+ console . error ( "Revalidate failed:" , error ) ;
62+ }
3163} ;
3264
3365/**
3466 * @description 게시글 생성을 위한 useMutation 커스텀 훅
3567 */
3668const useCreatePost = ( ) => {
3769 const queryClient = useQueryClient ( ) ;
70+ const { accessToken } = useAuthStore ( ) ;
3871
3972 return useMutation ( {
4073 mutationFn : createPost ,
41- onSuccess : ( ) => {
74+ onSuccess : async ( data ) => {
4275 // 게시글 목록 쿼리를 무효화하여 최신 목록 반영
4376 queryClient . invalidateQueries ( { queryKey : [ QueryKeys . posts ] } ) ;
77+
78+ // ISR 페이지 revalidate (사용자 인증 토큰 사용)
79+ if ( accessToken ) {
80+ await revalidateCommunityPage ( data . boardCode , accessToken ) ;
81+ }
82+
4483 toast . success ( "게시글이 등록되었습니다." ) ;
4584 } ,
4685 onError : ( error ) => {
0 commit comments