126
126
>
127
127
mdi-pencil-outline
128
128
</v-icon >
129
+ <template v-else >
130
+ <v-icon @click =" saveQuestion(question)" >
131
+ mdi-check
132
+ </v-icon >
133
+ <v-icon
134
+ v-if =" !question.saved"
135
+ @click =" removeQuestion(question)"
136
+ class =" ml-5"
137
+ >
138
+ mdi-close
139
+ </v-icon >
140
+ </template >
129
141
<v-icon
130
- v-else
131
- @click =" saveQuestion(question)"
132
- >
133
- mdi-check
134
- </v-icon >
135
- <v-icon
136
- v-if =" isLastQuestion(question)"
142
+ v-if =" isLastQuestion(question) && question.saved"
137
143
class =" ml-5"
138
144
@click =" prepareDelete(question)"
139
145
>
145
151
</v-simple-table >
146
152
</v-card-text >
147
153
<v-card-actions v-if =" allNextPassageIsEmpty(index + 1)" >
148
- <span
149
- class =" d-inline-flex cursor-pointer"
154
+ <v-btn
150
155
@click =" addQuestion(index + 1)"
156
+ depressed
157
+ :disabled =" cannotAddQuestion"
151
158
>
152
- <v-icon class = " mr-3 ml-5 mb-3 " >
159
+ <v-icon left >
153
160
mdi-plus-circle-outline
154
161
</v-icon >
155
162
Add question
156
- </span >
163
+ </v-btn >
157
164
</v-card-actions >
158
165
</v-card >
159
166
@@ -184,6 +191,7 @@ import KLDialogConfirm from '@/components/KLDialogConfirm.vue'
184
191
declare interface LocalQuestion extends ReadingQuestion {
185
192
editing: boolean ;
186
193
isNew: boolean ;
194
+ saved: boolean ;
187
195
}
188
196
189
197
@Component ({
@@ -230,7 +238,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
230
238
231
239
initSuccessHook (): void {
232
240
this .questions .forEach (question => {
233
- const localQuestion: LocalQuestion = { ... question , editing: false , isNew: false }
241
+ const localQuestion: LocalQuestion = { ... question , editing: false , isNew: false , saved: true }
234
242
this .localQuestions .push (localQuestion )
235
243
})
236
244
}
@@ -257,15 +265,40 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
257
265
choices: lastQuestion !== null ? lastQuestion .choices : [],
258
266
answers: [],
259
267
editing: true ,
260
- isNew: true
268
+ isNew: true ,
269
+ saved: false
261
270
}
262
271
}
263
272
273
+ get cannotAddQuestion (): boolean {
274
+ return this .localQuestions .some (q => q .isNew )
275
+ }
276
+
264
277
addQuestion (passage : LocalQuestion [' passage' ]): void {
265
- this .localQuestions .forEach (q => { q .isNew = false })
278
+ if (this .localQuestions .some (q => q .isNew )) {
279
+ return
280
+ }
281
+ this .localQuestions .forEach (q => {
282
+ if (q .saved ) {
283
+ q .isNew = false
284
+ }
285
+ })
266
286
this .localQuestions .push (this .newQuestion (passage ))
267
287
}
268
288
289
+ removeQuestion (question : ReadingQuestion ): void {
290
+ this .localQuestions = this .localQuestions .filter (q => q .pk !== question .pk )
291
+ this .resetNumbers ()
292
+ }
293
+
294
+ resetNumbers (): void {
295
+ let number = 1
296
+ this .localQuestions .forEach (q => {
297
+ q .number = number
298
+ number ++
299
+ })
300
+ }
301
+
269
302
formatQuestionType (type : ReadingQuestion [' question_type' ]): string {
270
303
const mapper = {
271
304
multiple_choice: ' Multiple choice' ,
@@ -344,6 +377,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
344
377
} else {
345
378
this .updateQuestion (question )
346
379
}
380
+ question .saved = true
347
381
}
348
382
349
383
createQuestion (question : LocalQuestion ): void {
@@ -357,9 +391,16 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
357
391
}
358
392
359
393
this .$store .dispatch (' readingExercise/createQuestion' , payload )
360
- .then (() => {
361
- question .editing = false
362
- question .isNew = false
394
+ .then (pk => {
395
+ if (pk !== null ) {
396
+ question .pk = pk
397
+ question .editing = false
398
+ question .isNew = false
399
+
400
+ if (! this .localQuestions .some (q => q .isNew )) {
401
+ this .addQuestion (question .passage )
402
+ }
403
+ }
363
404
})
364
405
.catch (unexpectedExc )
365
406
}
@@ -386,7 +427,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
386
427
questionToDelete: LocalQuestion | null = null
387
428
388
429
prepareDelete (question : LocalQuestion ): void {
389
- this .questionToDelete = question
430
+ this .questionToDelete = { ... question }
390
431
this .deleteConfirm = true
391
432
}
392
433
@@ -404,6 +445,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
404
445
}
405
446
this .localQuestions .splice (index , 1 )
406
447
448
+ this .resetNumbers ()
407
449
this .deleteConfirm = false
408
450
})
409
451
.catch (unexpectedExc )
0 commit comments