1- import React , { startTransition , useEffect , useState } from "react" ;
1+ import React , { useState } from "react" ;
22import { Section } from "@courselit/components-library" ;
33import {
44 LESSON_QUIZ_ADD_QUESTION ,
@@ -34,31 +34,17 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
3434 ( content && content . passingGrade ) || DEFAULT_PASSING_GRADE ,
3535 ) ;
3636
37- useEffect ( ( ) => {
38- startTransition ( ( ) => {
39- if ( content . questions ) {
40- setQuestions ( content . questions ) ;
41- }
42- if ( content . passingGrade ) {
43- setPassingGradePercentage ( content . passingGrade ) ;
44- }
45- if ( content . requiresPassingGrade ) {
46- setPassingGradeRequired ( content . requiresPassingGrade ) ;
47- }
48- } ) ;
49- } , [ content ] ) ;
50-
51- useEffect ( ( ) => {
37+ const notifyChange = ( updatedQuestions : Question [ ] ) => {
5238 onChange ( {
53- questions,
39+ questions : updatedQuestions ,
5440 requiresPassingGrade : passingGradeRequired ,
5541 passingGrade : passingGradePercentage ,
5642 } ) ;
57- } , [ questions , passingGradeRequired , passingGradePercentage , onChange ] ) ;
43+ } ;
5844
5945 const addNewOption = ( questionIndex : number ) => {
60- setQuestions ( ( prevQuestions ) =>
61- prevQuestions . map ( ( question , index ) =>
46+ setQuestions ( ( prevQuestions ) => {
47+ const updatedQuestions = prevQuestions . map ( ( question , index ) =>
6248 index === questionIndex
6349 ? {
6450 ...question ,
@@ -68,14 +54,16 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
6854 ] ,
6955 }
7056 : question ,
71- ) ,
72- ) ;
57+ ) ;
58+ notifyChange ( updatedQuestions ) ;
59+ return updatedQuestions ;
60+ } ) ;
7361 } ;
7462
7563 const setCorrectAnswer =
7664 ( questionIndex : number ) => ( index : number , checked : boolean ) => {
77- setQuestions ( ( prevQuestions ) =>
78- prevQuestions . map ( ( question , qIdx ) =>
65+ setQuestions ( ( prevQuestions ) => {
66+ const updatedQuestions = prevQuestions . map ( ( question , qIdx ) =>
7967 qIdx === questionIndex
8068 ? {
8169 ...question ,
@@ -86,14 +74,16 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
8674 ) ,
8775 }
8876 : question ,
89- ) ,
90- ) ;
77+ ) ;
78+ notifyChange ( updatedQuestions ) ;
79+ return updatedQuestions ;
80+ } ) ;
9181 } ;
9282
9383 const setOptionText =
9484 ( questionIndex : number ) => ( index : number , text : string ) => {
95- setQuestions ( ( prevQuestions ) =>
96- prevQuestions . map ( ( question , qIdx ) =>
85+ setQuestions ( ( prevQuestions ) => {
86+ const updatedQuestions = prevQuestions . map ( ( question , qIdx ) =>
9787 qIdx === questionIndex
9888 ? {
9989 ...question ,
@@ -104,21 +94,25 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
10494 ) ,
10595 }
10696 : question ,
107- ) ,
108- ) ;
97+ ) ;
98+ notifyChange ( updatedQuestions ) ;
99+ return updatedQuestions ;
100+ } ) ;
109101 } ;
110102
111103 const setQuestionText = ( index : number ) => ( text : string ) => {
112- setQuestions ( ( prevQuestions ) =>
113- prevQuestions . map ( ( question , qIdx ) =>
104+ setQuestions ( ( prevQuestions ) => {
105+ const updatedQuestions = prevQuestions . map ( ( question , qIdx ) =>
114106 qIdx === index ? { ...question , text } : question ,
115- ) ,
116- ) ;
107+ ) ;
108+ notifyChange ( updatedQuestions ) ;
109+ return updatedQuestions ;
110+ } ) ;
117111 } ;
118112
119113 const removeOption = ( questionIndex : number ) => ( index : number ) => {
120- setQuestions ( ( prevQuestions ) =>
121- prevQuestions . map ( ( question , qIdx ) =>
114+ setQuestions ( ( prevQuestions ) => {
115+ const updatedQuestions = prevQuestions . map ( ( question , qIdx ) =>
122116 qIdx === questionIndex
123117 ? {
124118 ...question ,
@@ -127,26 +121,35 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
127121 ) ,
128122 }
129123 : question ,
130- ) ,
131- ) ;
124+ ) ;
125+ notifyChange ( updatedQuestions ) ;
126+ return updatedQuestions ;
127+ } ) ;
132128 } ;
133129
134130 const deleteQuestion = ( questionIndex : number ) => {
135- setQuestions ( ( prevQuestions ) =>
136- prevQuestions . filter ( ( _ , idx ) => idx !== questionIndex ) ,
137- ) ;
131+ setQuestions ( ( prevQuestions ) => {
132+ const updatedQuestions = prevQuestions . filter (
133+ ( _ , idx ) => idx !== questionIndex ,
134+ ) ;
135+ notifyChange ( updatedQuestions ) ;
136+ return updatedQuestions ;
137+ } ) ;
138138 } ;
139139
140- const addNewQuestion = ( ) =>
141- setQuestions ( ( prevQuestions ) => [
142- ...prevQuestions ,
143- {
144- text : `${ LESSON_QUIZ_QUESTION_PLACEHOLDER } #${
145- prevQuestions . length + 1
146- } `,
147- options : [ { text : "" , correctAnswer : false } ] ,
148- } ,
149- ] ) ;
140+ const addNewQuestion = ( ) => {
141+ setQuestions ( ( prevQuestions ) => {
142+ const updatedQuestions = [
143+ ...prevQuestions ,
144+ {
145+ text : `${ LESSON_QUIZ_QUESTION_PLACEHOLDER } #${ prevQuestions . length + 1 } ` ,
146+ options : [ { text : "" , correctAnswer : false } ] ,
147+ } ,
148+ ] ;
149+ notifyChange ( updatedQuestions ) ;
150+ return updatedQuestions ;
151+ } ) ;
152+ } ;
150153
151154 return (
152155 < div className = "flex flex-col gap-8 mb-8" >
@@ -190,17 +193,28 @@ export function QuizBuilder({ content, onChange }: QuizBuilderProps) {
190193 < Switch
191194 id = "preview"
192195 checked = { passingGradeRequired }
193- onCheckedChange = { ( checked ) =>
194- setPassingGradeRequired ( checked )
195- }
196+ onCheckedChange = { ( checked ) => {
197+ setPassingGradeRequired ( checked ) ;
198+ onChange ( {
199+ questions,
200+ requiresPassingGrade : checked ,
201+ passingGrade : passingGradePercentage ,
202+ } ) ;
203+ } }
196204 />
197205 </ div >
198206 < Input
199207 type = "number"
200208 value = { passingGradePercentage }
201- onChange = { ( e ) =>
202- setPassingGradePercentage ( parseInt ( e . target . value ) )
203- }
209+ onChange = { ( e ) => {
210+ const newValue = parseInt ( e . target . value ) ;
211+ setPassingGradePercentage ( newValue ) ;
212+ onChange ( {
213+ questions,
214+ requiresPassingGrade : passingGradeRequired ,
215+ passingGrade : newValue ,
216+ } ) ;
217+ } }
204218 disabled = { ! passingGradeRequired }
205219 min = { 0 }
206220 max = { 100 }
0 commit comments