Skip to content

Commit 0d00729

Browse files
feat: add and connect database for quiz progress tracking
1 parent ca1c289 commit 0d00729

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/components/tutorial-quiz.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
"use client";
2+
13
import { Cinzel } from "next/font/google";
24
import { useState } from "react";
35
import { QuizData } from "@/lib/types/types";
6+
import { createClient } from "@/lib/supabase/client";
7+
// Idk if to use from server, or client
48

59
const cinzel = Cinzel({ subsets: ["latin"], weight: ["700"] });
610

@@ -40,6 +44,7 @@ export default function Quiz({ quizData }: QuizProps) {
4044
setIsCorrect(null);
4145
} else {
4246
setShowResults(true);
47+
updateUserQuizProgress();
4348
}
4449
}, 2000); // 2 second delay so they can read the feedback
4550
};
@@ -52,6 +57,30 @@ export default function Quiz({ quizData }: QuizProps) {
5257
setIsCorrect(null);
5358
};
5459

60+
// Updates database that user completed quiz, no score to keep things simple
61+
const updateUserQuizProgress = async () => {
62+
const supabase = createClient();
63+
64+
// Get user
65+
const {
66+
data: { user },
67+
} = await supabase.auth.getUser();
68+
69+
if (!user) {
70+
console.error("User not logged in, cannot save progress.");
71+
return;
72+
}
73+
74+
const { error: insertError } = await supabase.from("user_quiz_progress").insert({
75+
user_id: user.id,
76+
quiz_id: quizData.id,
77+
});
78+
79+
if (insertError) {
80+
console.error(`Supabase insertion error: ${insertError}`);
81+
}
82+
};
83+
5584
return (
5685
<div className="mt-12 p-8 border-2 border-amber-800/50 rounded-xl bg-amber-50/50 shadow-inner shadow-amber-900/20">
5786
<h2 className={`text-3xl font-bold text-center mb-6 text-amber-900 ${cinzel.className}`}>

src/data/quizzes/01-hello-world.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { QuizData } from "@/lib/types/types";
22

33
export const helloWorldQuiz: QuizData = {
44
title: "The Oracle's First Greeting",
5+
id: "hello-world",
56
questions: [
67
{
78
questionText: "What is the primary purpose of a 'Hello World' program?",

src/lib/types/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export type Question = {
66

77
export type QuizData = {
88
title: string;
9+
id: string;
910
questions: Question[];
1011
};

0 commit comments

Comments
 (0)