@@ -109,6 +109,14 @@ export default function QuizEditorPage() {
109109 } ,
110110 } ) ;
111111
112+ const { mutate : createQuiz , isPending : isCreatingQuiz , isSuccess : isQuizCreated } = createQuizMutation ;
113+
114+ useEffect ( ( ) => {
115+ if ( ! isLoading && quiz === null && ! isCreatingQuiz && ! isQuizCreated && lessonId ) {
116+ createQuiz ( { title : 'Untitled Quiz' , lessonId } ) ;
117+ }
118+ } , [ isLoading , quiz , isCreatingQuiz , isQuizCreated , lessonId , createQuiz ] ) ;
119+
112120 useEffect ( ( ) => {
113121 if ( quiz ?. questions ) {
114122 // eslint-disable-next-line react-hooks/set-state-in-effect
@@ -194,15 +202,15 @@ export default function QuizEditorPage() {
194202 < div className = "max-w-4xl mx-auto space-y-8" >
195203 < div className = "flex justify-between items-center" >
196204 < h2 className = "text-xl font-semibold" > Questions</ h2 >
197- < Button onClick = { ( ) => setEditingQuestionId ( 'new' ) } >
205+ < Button onClick = { ( ) => setEditingQuestionId ( 'new' ) } disabled = { ! quiz } >
198206 < Plus className = "w-4 h-4 mr-2" />
199207 Add Question
200208 </ Button >
201209 </ div >
202210
203- { editingQuestionId === 'new' && (
211+ { editingQuestionId === 'new' && quiz && (
204212 < QuestionEditor
205- quizId = { quiz ? .id ?? '' }
213+ quizId = { quiz . id }
206214 onCancel = { ( ) => setEditingQuestionId ( null ) }
207215 onSave = { ( ) => {
208216 setEditingQuestionId ( null ) ;
@@ -214,35 +222,37 @@ export default function QuizEditorPage() {
214222 ) }
215223
216224 < div className = "space-y-4" >
217- < DndContext
218- sensors = { sensors }
219- collisionDetection = { closestCenter }
220- onDragEnd = { handleDragEnd }
221- >
222- < SortableContext
223- items = { orderedQuestions }
224- strategy = { verticalListSortingStrategy }
225+ { quiz && (
226+ < DndContext
227+ sensors = { sensors }
228+ collisionDetection = { closestCenter }
229+ onDragEnd = { handleDragEnd }
225230 >
226- { orderedQuestions . map ( ( question , index ) => (
227- < SortableQuestionItem
228- key = { question . id }
229- question = { question }
230- index = { index }
231- isEditing = { editingQuestionId === question . id }
232- onEdit = { ( ) => setEditingQuestionId ( question . id ) }
233- onDelete = { ( ) => { } }
234- onCancel = { ( ) => setEditingQuestionId ( null ) }
235- onSave = { ( ) => {
236- setEditingQuestionId ( null ) ;
237- void queryClient . invalidateQueries ( {
238- queryKey : [ 'quiz' , 'lesson' , lessonId ] ,
239- } ) ;
240- } }
241- quizId = { quiz ?. id ?? '' }
242- />
243- ) ) }
244- </ SortableContext >
245- </ DndContext >
231+ < SortableContext
232+ items = { orderedQuestions }
233+ strategy = { verticalListSortingStrategy }
234+ >
235+ { orderedQuestions . map ( ( question , index ) => (
236+ < SortableQuestionItem
237+ key = { question . id }
238+ question = { question }
239+ index = { index }
240+ isEditing = { editingQuestionId === question . id }
241+ onEdit = { ( ) => setEditingQuestionId ( question . id ) }
242+ onDelete = { ( ) => { } }
243+ onCancel = { ( ) => setEditingQuestionId ( null ) }
244+ onSave = { ( ) => {
245+ setEditingQuestionId ( null ) ;
246+ void queryClient . invalidateQueries ( {
247+ queryKey : [ 'quiz' , 'lesson' , lessonId ] ,
248+ } ) ;
249+ } }
250+ quizId = { quiz . id }
251+ />
252+ ) ) }
253+ </ SortableContext >
254+ </ DndContext >
255+ ) }
246256 { quiz ?. questions ?. length === 0 && ! editingQuestionId && (
247257 < div className = "text-center py-12 border-2 border-dashed rounded-lg bg-muted/50" >
248258 < p className = "text-muted-foreground mb-4" > No questions yet</ p >
@@ -392,6 +402,9 @@ function QuestionEditor({
392402 const mutation = useMutation ( {
393403 mutationFn : ( data : QuestionFormData ) => {
394404 if ( isNew ) {
405+ if ( ! quizId ) {
406+ throw new Error ( 'Quiz ID is missing. Please try refreshing the page.' ) ;
407+ }
395408 return quizzesApi . createQuestion ( quizId , data ) ;
396409 }
397410 if ( ! question ) {
0 commit comments