File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed
Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ "use client" ;
2+
13import { Cinzel } from "next/font/google" ;
24import { useState } from "react" ;
35import { QuizData } from "@/lib/types/types" ;
6+ import { createClient } from "@/lib/supabase/client" ;
7+ // Idk if to use from server, or client
48
59const 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 } ` } >
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { QuizData } from "@/lib/types/types";
22
33export 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?" ,
Original file line number Diff line number Diff line change @@ -6,5 +6,6 @@ export type Question = {
66
77export type QuizData = {
88 title : string ;
9+ id : string ;
910 questions : Question [ ] ;
1011} ;
You can’t perform that action at this time.
0 commit comments