File tree Expand file tree Collapse file tree 4 files changed +63
-10
lines changed Expand file tree Collapse file tree 4 files changed +63
-10
lines changed Original file line number Diff line number Diff line change @@ -38,13 +38,24 @@ model Account {
38
38
}
39
39
40
40
model User {
41
- id String @id @default (cuid () )
42
- name String ?
43
- address String ? @unique
44
- email String ? @unique
45
- emailVerified DateTime ?
46
- image String ?
47
- createdAt DateTime @default (now () )
48
- accounts Account []
49
- Todo Todo []
41
+ id String @id @default (cuid () )
42
+ name String ?
43
+ address String ? @unique
44
+ email String ? @unique
45
+ emailVerified DateTime ?
46
+ image String ?
47
+ createdAt DateTime @default (now () )
48
+ accounts Account []
49
+ Todo Todo []
50
+ CompletedQuizzes CompletedQuizzes []
51
+ }
52
+
53
+ model CompletedQuizzes {
54
+ id String @id @default (cuid () )
55
+ lesson String
56
+ userId String
57
+ completed Boolean @default (false )
58
+ createdAt DateTime @default (now () )
59
+ updatedAt DateTime @updatedAt
60
+ user User @relation (fields : [userId ] , references : [id ] , onDelete : Cascade )
50
61
}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import {
18
18
getCorrectAnswersIndexes ,
19
19
haveSameElements ,
20
20
} from "@/utils/QuizHelpers" ;
21
+ import { api } from "@/utils/api" ;
21
22
22
23
interface QuizProps {
23
24
quiz : string ;
@@ -123,7 +124,18 @@ const Quiz = (props: QuizProps): JSX.Element => {
123
124
} ) ;
124
125
} ;
125
126
127
+ // - Add
128
+ const { mutate : quizzesAddMutate , isLoading : quizzesAddIsLoading } =
129
+ api . completedQuizzes . add . useMutation ( {
130
+ onSuccess : ( ) => {
131
+ return quizSuccessToast ( ) ;
132
+ } ,
133
+ } ) ;
134
+
126
135
const quizSuccessToast = ( ) => {
136
+ // api.completedQuizzes.add.useMutation({
137
+ // lesson: props.quiz,
138
+ // });
127
139
toast ( {
128
140
title : "Amazing!" ,
129
141
description : "You have passed the lesson!" ,
@@ -159,7 +171,11 @@ const Quiz = (props: QuizProps): JSX.Element => {
159
171
return quizFailedToast ( wrongAnswersCounter ) ;
160
172
}
161
173
162
- return quizSuccessToast ( ) ;
174
+ // return quizSuccessToast();
175
+
176
+ quizzesAddMutate ( {
177
+ lesson : props . quiz ,
178
+ } ) ;
163
179
} ;
164
180
165
181
const cancelQuiz = ( ) => {
@@ -254,6 +270,7 @@ const Quiz = (props: QuizProps): JSX.Element => {
254
270
colorScheme = "green"
255
271
backgroundColor = "green.400"
256
272
onClick = { submit }
273
+ isLoading = { quizzesAddIsLoading }
257
274
>
258
275
Submit
259
276
</ Button >
Original file line number Diff line number Diff line change 1
1
import { createTRPCRouter } from "@/server/api/trpc" ;
2
2
import { exampleRouter } from "@/server/api/routers/example" ;
3
3
import { todosRouter } from "@/server/api/routers/todos" ;
4
+ import { completedQuizzesRouter } from "@/server/api/routers/completedquizzes" ;
4
5
5
6
/**
6
7
* This is the primary router for your server.
@@ -10,6 +11,7 @@ import { todosRouter } from "@/server/api/routers/todos";
10
11
export const appRouter = createTRPCRouter ( {
11
12
example : exampleRouter ,
12
13
todos : todosRouter ,
14
+ completedQuizzes : completedQuizzesRouter ,
13
15
} ) ;
14
16
15
17
// export type definition of API
Original file line number Diff line number Diff line change
1
+ // Imports
2
+ // ========================================================
3
+ import { z } from "zod" ;
4
+ import { createTRPCRouter , protectedProcedure } from "@/server/api/trpc" ;
5
+
6
+ // Router
7
+ // ========================================================
8
+ export const completedQuizzesRouter = createTRPCRouter ( {
9
+ /**
10
+ * Add completed Quizz belonging to the session user
11
+ */
12
+ add : protectedProcedure
13
+ . input ( z . object ( { lesson : z . string ( ) } ) )
14
+ . mutation ( async ( { input, ctx } ) => {
15
+ return await ctx . prisma . completedQuizzes . create ( {
16
+ data : {
17
+ lesson : input . lesson ,
18
+ userId : ctx . session . user . id ,
19
+ completed : true ,
20
+ } ,
21
+ } ) ;
22
+ } ) ,
23
+ } ) ;
You can’t perform that action at this time.
0 commit comments