-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ ๊ฑฐ๋ ์ ์ ๋ก์ง ๊ตฌํ #67
Changes from all commits
fcf5e2f
b451e80
8f2812d
34f4639
4ed0890
f4494cb
69c265e
556056b
b4a4aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { useRouter } from 'next/navigation' | ||
import { toast } from '@/hooks/useToast' | ||
import { handleApiError } from '@/lib/handleApiError' | ||
import { postSuggestion } from '@/services/suggest/suggest' | ||
import { ItemSuggestion } from '@/types' | ||
|
||
const useSuggestMutation = (toCardId: number, fromCardId: number) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useSuggestMutation ๊ฐ์ ๊ฒฝ์ฐ ๊ธฐ์กด ์ํ๋ฅผ ์
๋ฐ์ดํธ ํ๋๊ฒ์ผ๋ก ๋ณด์ด๋๋ฐ |
||
const queryClient = useQueryClient() | ||
const router = useRouter() | ||
|
||
const queryKey = [toCardId, 'suggestions'] | ||
return useMutation({ | ||
mutationFn: postSuggestion, | ||
onMutate: async () => { | ||
// ๋กค๋ฐฑ์ ์ํ ์ด์ ๊ฐ ์ ์ฅ | ||
const previousSuggestions = queryClient.getQueryData([ | ||
toCardId, | ||
'suggestions', | ||
]) as ItemSuggestion[] | ||
|
||
await queryClient.cancelQueries({ queryKey }) | ||
|
||
const updateSuggestions = previousSuggestions | ||
|
||
//๋๊ด์ ์ ๋ฐ์ดํธ | ||
const indexToUpdate = previousSuggestions.findIndex( | ||
(suggestion) => suggestion.cardId === fromCardId, | ||
) | ||
updateSuggestions[indexToUpdate].suggestionStatus = 'WAITING' | ||
queryClient.setQueryData(queryKey, updateSuggestions) | ||
|
||
return { previousSuggestions } | ||
Comment on lines
+27
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํน์ UI๊ฐ ๋ฐ๋์ง ์๋ ์ด์๊ฐ ํด๊ฒฐ๋์ จ๋์? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ต!! ํด๊ฒฐ๋์์ต๋๋ค. ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๊น์ ๋ณต์ฌํ์ฌ ํด๊ฒฐํ์์ต๋๋ค. |
||
}, | ||
onError: (error, _, context) => { | ||
queryClient.setQueryData(queryKey, context?.previousSuggestions) | ||
const { shouldRedirect } = handleApiError(error) | ||
if (shouldRedirect) { | ||
router.push(shouldRedirect) | ||
} else { | ||
console.log(shouldRedirect, error) | ||
toast({ | ||
title: '์ ์์ ์คํจํ์ต๋๋ค', | ||
duration: 2000, | ||
}) | ||
} | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ queryKey }) | ||
}, | ||
}) | ||
} | ||
|
||
export default useSuggestMutation |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { getSuggestions } from '@/services/suggest/suggest' | ||
|
||
const useSuggestionsQuery = (itemId: number) => { | ||
return useQuery({ | ||
queryKey: [itemId, 'suggestions'] as const, | ||
queryFn: () => getSuggestions(itemId), | ||
}) | ||
} | ||
|
||
export default useSuggestionsQuery |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋จ ๋๊ฐ์ ๊ฐ์ ๋ ๋๋ง ํ๊ณ ์๋๋ฐ
๋๊ฐ ์ ๋๋ฉด ๊ทธ๋ฅ ํ์ด์ ์จ๋ ๋์ง ์์๊น์? ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tab Content์ ๋ด์ฉ์ด ๋ง์์ ํ์ด์ ์ฐ์ง ์๊ณ map์ผ๋ก ๋ฌถ์์ต๋๋ค!