Skip to content

Commit

Permalink
유저 페이지 작업하기 (#20)
Browse files Browse the repository at this point in the history
* feat: 데이터 타입 수정

* feat: 제출한 퀴즈 데이터 페칭 로직 구현

* feat: 유저 페이지 구현

* chore: husky 명령어 수정

* feat: queryOptions로 변경
  • Loading branch information
hyoribogo authored Dec 26, 2023
1 parent 860c9d6 commit f3b2f62
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint-staged
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
npm.cmd run lint-staged
;;
*)
npm run lint-staged
;;
esac
52 changes: 52 additions & 0 deletions app/my/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { QuizCard } from '@/components/quiz/quiz-card';
import quizOptions from '@/services/quiz/options';
import { createClient } from '@/utils/supabase/server';
import {
HydrationBoundary,
QueryClient,
dehydrate,
} from '@tanstack/react-query';
import { cookies } from 'next/headers';

export default async function Page() {
const cookieStore = cookies();
const supabase = createClient(cookieStore);
const {
data: { user },
} = await supabase.auth.getUser();

const queryClient = new QueryClient();

const submittedQuiz = await queryClient.fetchQuery(
quizOptions.submitted(user?.id ?? '')
);

return (
<>
<h1 className="text-right text-3xl font-bold">
{user?.user_metadata.user_name}
</h1>
<h2 className="text-xl font-bold">풀었던 퀴즈</h2>
<HydrationBoundary state={dehydrate(queryClient)}>
{submittedQuiz && submittedQuiz.length ? (
<ul className="flex flex-col gap-6">
{submittedQuiz.map(
({ quiz_id, success, quizzes }) =>
quizzes && (
<QuizCard
key={quiz_id}
status={success ? 'correct' : 'wrong'}
difficulty={quizzes.difficulty}
title={quizzes.title}
summary={quizzes.summary}
/>
)
)}
</ul>
) : (
<p>제출한 퀴즈가 없습니다.</p>
)}
</HydrationBoundary>
</>
);
}
13 changes: 13 additions & 0 deletions services/quiz/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ const quizAPI = {
return data;
},

getSubmittedQuiz: async (userId: string) => {
const supabase: SupabaseClient<Database> = createClient();

const { data } = await supabase
.from('quizsubmissions')
.select(`*, quizzes (id, *)`)
.eq('user_id', userId);

console.log(data);

return data;
},

getChoicesOfQuiz: async (quizId: number) => {
const supabase: SupabaseClient<Database> = createClient();

Expand Down
4 changes: 4 additions & 0 deletions services/quiz/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function useGetQuiz(quizId: number) {
return useSuspenseQuery(quizOptions.detail(quizId));
}

export function useGetSubmittedQuiz(userId: string) {
return useSuspenseQuery(quizOptions.submitted(userId));
}

export function useGetChoicesOfQuiz(quizId: number) {
return useSuspenseQuery(quizOptions.choices(quizId));
}
Expand Down
6 changes: 6 additions & 0 deletions services/quiz/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const quizOptions = {
queryFn: () => quizAPI.getQuiz(quizId),
}),

submitted: (userId: string) =>
queryOptions({
queryKey: [...quizOptions.all, 'submitted', userId],
queryFn: () => quizAPI.getSubmittedQuiz(userId),
}),

choices: (quizId: number) =>
queryOptions({
queryKey: [...quizOptions.detail(quizId).queryKey, 'choices'],
Expand Down

0 comments on commit f3b2f62

Please sign in to comment.