-
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: quiz 관련 쿼리 레이어 정리 * refactor: 새로운 quiz 쿼리 사용 * remove: 기존의 quiz 훅 제거 * chore: import 관련 빌드 문제 해결 * style: mutate 이름 submitQuiz 로 변경
- Loading branch information
1 parent
8875172
commit 5fa7d31
Showing
7 changed files
with
104 additions
and
86 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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
import { createClient } from '@/utils/supabase/client'; | ||
import { SupabaseClient } from '@supabase/supabase-js'; | ||
|
||
const quizAPI = { | ||
getQuiz: async (id: number) => { | ||
const supabase: SupabaseClient<Database> = createClient(); | ||
|
||
const { data } = await supabase | ||
.from('quizzes') | ||
.select(`*, users!quizzes_user_id_fkey(id, name)`) | ||
.eq('id', id) | ||
.limit(1) | ||
.single(); | ||
|
||
return data; | ||
}, | ||
|
||
getChoicesOfQuiz: async (quizId: number) => { | ||
const supabase: SupabaseClient<Database> = createClient(); | ||
|
||
const { data } = await supabase | ||
.from('choices') | ||
.select(`id, quiz_id, description`) | ||
.eq('quiz_id', quizId); | ||
|
||
return data; | ||
}, | ||
|
||
postQuizSubmission: async (params: { quizId: number; choiceId: number }) => { | ||
const { quizId, choiceId } = params; | ||
|
||
const res = await fetch('/api/quiz-submission', { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
quizId, | ||
choiceId, | ||
}), | ||
}); | ||
|
||
const json = await res.json(); | ||
|
||
if (!res.ok) { | ||
throw new Error(json.error); | ||
} | ||
|
||
return json; | ||
}, | ||
}; | ||
|
||
export default quizAPI; |
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,17 @@ | ||
import { useSuspenseQuery, useMutation } from '@tanstack/react-query'; | ||
import quizOptions from './options'; | ||
import quizAPI from './api'; | ||
|
||
export function useGetQuiz(quizId: number) { | ||
return useSuspenseQuery(quizOptions.detail(quizId)); | ||
} | ||
|
||
export function useGetChoicesOfQuiz(quizId: number) { | ||
return useSuspenseQuery(quizOptions.choices(quizId)); | ||
} | ||
|
||
export function useSubmitQuiz() { | ||
return useMutation({ | ||
mutationFn: quizAPI.postQuizSubmission, | ||
}); | ||
} |
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,20 @@ | ||
import { queryOptions } from '@tanstack/react-query'; | ||
import quizAPI from './api'; | ||
|
||
const quizOptions = { | ||
all: ['quizzes'] as const, | ||
|
||
detail: (quizId: number) => | ||
queryOptions({ | ||
queryKey: [...quizOptions.all, 'detail', quizId], | ||
queryFn: () => quizAPI.getQuiz(quizId), | ||
}), | ||
|
||
choices: (quizId: number) => | ||
queryOptions({ | ||
queryKey: [...quizOptions.detail(quizId).queryKey, 'choices'], | ||
queryFn: () => quizAPI.getChoicesOfQuiz(quizId), | ||
}), | ||
}; | ||
|
||
export default quizOptions; |