-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: params 변경 * feat: 북마크 patch api 구현
- Loading branch information
Showing
7 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { recipeApi } from '@/apis'; | ||
import type { RecipeBookmarkRequestBody, RecipeDetail } from '@/types/recipe'; | ||
|
||
const headers = { 'Content-Type': 'application/json' }; | ||
|
||
const patchRecipeBookmark = (recipeId: number, body: RecipeBookmarkRequestBody) => { | ||
return recipeApi.patch({ params: `/${recipeId}/bookmark`, credentials: true }, headers, body); | ||
}; | ||
|
||
const useRecipeBookmarkMutation = (recipeId: number) => { | ||
const queryClient = useQueryClient(); | ||
|
||
const queryKey = ['recipeDetail', recipeId]; | ||
|
||
return useMutation({ | ||
mutationFn: (body: RecipeBookmarkRequestBody) => patchRecipeBookmark(recipeId, body), | ||
onMutate: async (newBookmarkRequest) => { | ||
await queryClient.cancelQueries({ queryKey: queryKey }); | ||
|
||
const previousRequest = queryClient.getQueryData<RecipeDetail>(queryKey); | ||
|
||
if (previousRequest) { | ||
queryClient.setQueryData(queryKey, () => ({ | ||
...previousRequest, | ||
bookmark: newBookmarkRequest.bookmark, | ||
})); | ||
} | ||
|
||
return { previousRequest }; | ||
}, | ||
onError: (error, _, context) => { | ||
queryClient.setQueryData(queryKey, context?.previousRequest); | ||
|
||
if (error instanceof Error) { | ||
throw new Error(error.message); | ||
} | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ queryKey: queryKey }); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useRecipeBookmarkMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters