Skip to content

Commit 59f6ae5

Browse files
improve add question page
1 parent 28ad009 commit 59f6ae5

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

src/store/reading-exercises.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ export const readingExercise: Module<ReadingExerciseState, RootState> = {
119119
commit('SET_QUESTION_PAGINATION', pagination)
120120
},
121121

122-
async createQuestion ({ state, commit }, payload: ReadingQuestionCreateReq): Promise<void> {
123-
if (state.currentReadingExercise === undefined) return
122+
async createQuestion ({ state, commit }, payload: ReadingQuestionCreateReq): Promise<ReadingQuestion['pk'] | null> {
123+
if (state.currentReadingExercise === undefined) return null
124124
payload.exercise = state.currentReadingExercise.url
125125
const data = await Api.readingQuestion.create(payload)
126126
commit('ADD_QUESTION', data)
127+
return data.pk
127128
},
128129

129130
async updateQuestion (

src/views/reading-exercise/ReadingExerciseEditAnswers.vue

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,20 @@
126126
>
127127
mdi-pencil-outline
128128
</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>
129141
<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"
137143
class="ml-5"
138144
@click="prepareDelete(question)"
139145
>
@@ -145,15 +151,16 @@
145151
</v-simple-table>
146152
</v-card-text>
147153
<v-card-actions v-if="allNextPassageIsEmpty(index + 1)">
148-
<span
149-
class="d-inline-flex cursor-pointer"
154+
<v-btn
150155
@click="addQuestion(index + 1)"
156+
depressed
157+
:disabled="cannotAddQuestion"
151158
>
152-
<v-icon class="mr-3 ml-5 mb-3">
159+
<v-icon left>
153160
mdi-plus-circle-outline
154161
</v-icon>
155162
Add question
156-
</span>
163+
</v-btn>
157164
</v-card-actions>
158165
</v-card>
159166

@@ -184,6 +191,7 @@ import KLDialogConfirm from '@/components/KLDialogConfirm.vue'
184191
declare interface LocalQuestion extends ReadingQuestion {
185192
editing: boolean;
186193
isNew: boolean;
194+
saved: boolean;
187195
}
188196
189197
@Component({
@@ -230,7 +238,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
230238
231239
initSuccessHook (): void {
232240
this.questions.forEach(question => {
233-
const localQuestion: LocalQuestion = { ...question, editing: false, isNew: false }
241+
const localQuestion: LocalQuestion = { ...question, editing: false, isNew: false, saved: true }
234242
this.localQuestions.push(localQuestion)
235243
})
236244
}
@@ -257,15 +265,40 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
257265
choices: lastQuestion !== null ? lastQuestion.choices : [],
258266
answers: [],
259267
editing: true,
260-
isNew: true
268+
isNew: true,
269+
saved: false
261270
}
262271
}
263272
273+
get cannotAddQuestion (): boolean {
274+
return this.localQuestions.some(q => q.isNew)
275+
}
276+
264277
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+
})
266286
this.localQuestions.push(this.newQuestion(passage))
267287
}
268288
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+
269302
formatQuestionType (type: ReadingQuestion['question_type']): string {
270303
const mapper = {
271304
multiple_choice: 'Multiple choice',
@@ -344,6 +377,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
344377
} else {
345378
this.updateQuestion(question)
346379
}
380+
question.saved = true
347381
}
348382
349383
createQuestion (question: LocalQuestion): void {
@@ -357,9 +391,16 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
357391
}
358392
359393
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+
}
363404
})
364405
.catch(unexpectedExc)
365406
}
@@ -386,7 +427,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
386427
questionToDelete: LocalQuestion | null = null
387428
388429
prepareDelete (question: LocalQuestion): void {
389-
this.questionToDelete = question
430+
this.questionToDelete = { ...question }
390431
this.deleteConfirm = true
391432
}
392433
@@ -404,6 +445,7 @@ export default class ReadingExerciseEditAnswers extends Mixins(ReadingExerciseMi
404445
}
405446
this.localQuestions.splice(index, 1)
406447
448+
this.resetNumbers()
407449
this.deleteConfirm = false
408450
})
409451
.catch(unexpectedExc)

0 commit comments

Comments
 (0)