Skip to content

Commit d51b422

Browse files
submit answers
1 parent 74a6c97 commit d51b422

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

src/api/endpoints.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const endpoints = {
2424
detail: '/classroom/reading-exercises/<pk>/',
2525
create: '/classroom/reading-exercises/',
2626
update: '/classroom/reading-exercises/<pk>/',
27-
uploadImage: '/classroom/reading-exercises/upload-image/'
27+
uploadImage: '/classroom/reading-exercises/upload-image/',
28+
submitAnswers: '/classroom/reading-exercises/<pk>/submit-answers/'
2829
},
2930
readingQuestion: {
3031
list: '/classroom/reading-questions/',

src/api/reading-exercise.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import { ReadingExerciseCreateReq, ReadingExerciseDetailRes, ReadingExerciseListRes, ReadingExerciseUpdateReq } from '@/interfaces/api/reading-exercise'
2+
import { ReadingExerciseCreateReq, ReadingExerciseDetailRes, ReadingExerciseListRes, ReadingExerciseSubmitAnswersReq, ReadingExerciseUpdateReq } from '@/interfaces/api/reading-exercise'
33
import { ReadingExercise } from '@/interfaces/reading-exercise'
44
import { endpoints, replacePk } from './endpoints'
55

@@ -24,5 +24,10 @@ export const readingExercise = {
2424
const endpoint = replacePk(endpoints.readingExercise.update, pk)
2525
const res = await Vue.axios.patch(endpoint, payload)
2626
return res.data
27+
},
28+
29+
async submitAnswers (pk: ReadingExercise['pk'], payload: ReadingExerciseSubmitAnswersReq[]): Promise<void> {
30+
const endpoint = replacePk(endpoints.readingExercise.submitAnswers, pk)
31+
await Vue.axios.post(endpoint, payload)
2732
}
2833
}

src/interfaces/api/reading-exercise.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { ReadingExercise } from "../reading-exercise";
2-
import { PaginatedRes } from "./common";
1+
import { ReadingExercise } from '../reading-exercise'
2+
import { PaginatedRes } from './common'
3+
import { ReadingAnswer } from '../reading-question'
34

45
export declare interface ReadingExerciseDetailRes extends ReadingExercise {}
56

@@ -17,3 +18,5 @@ export declare interface ReadingExerciseUpdateReq {
1718
identifier: string;
1819
content: string;
1920
}
21+
22+
export declare interface ReadingExerciseSubmitAnswersReq extends ReadingAnswer {}

src/views/reading-exercise/ReadingExerciseSubmit.vue

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,67 @@
4747
</v-simple-table>
4848
</v-col>
4949
</v-row>
50+
51+
<v-btn
52+
color="primary"
53+
class="d-block mx-auto mt-7"
54+
width="150"
55+
@click="confirmSubmit = true"
56+
>
57+
Submit
58+
</v-btn>
5059
</v-col>
5160
</v-row>
5261
</div>
62+
63+
<v-dialog
64+
v-model="confirmSubmit"
65+
width="500"
66+
>
67+
<v-card>
68+
<v-card-title>
69+
Please confirm
70+
</v-card-title>
71+
<v-card-text>
72+
<div
73+
v-if="!allAnswered"
74+
class="mb-3 d-flex error--text font-weight-bold"
75+
>
76+
<v-icon color="error">
77+
mdi-alert-outline
78+
</v-icon>
79+
<span class="ml-3">
80+
You haven't answered all questions!
81+
</span>
82+
</div>
83+
Are you sure to submit?
84+
You cannot edit your answers after this.
85+
</v-card-text>
86+
<v-card-actions>
87+
<v-spacer></v-spacer>
88+
<v-btn
89+
text
90+
@click="confirmSubmit = false"
91+
>
92+
Cancel
93+
</v-btn>
94+
<v-btn
95+
color="primary"
96+
depressed
97+
:loading="loadingSubmit"
98+
@click="submitAnswers"
99+
>
100+
Confirm
101+
</v-btn>
102+
</v-card-actions>
103+
</v-card>
104+
</v-dialog>
53105
</v-container>
54106
</template>
55107

56108
<script lang="ts">
109+
import { Api } from '@/api'
110+
import { ReadingExerciseSubmitAnswersReq } from '@/interfaces/api/reading-exercise'
57111
import { ReadingExercise } from '@/interfaces/reading-exercise'
58112
import { ReadingAnswer, ReadingQuestion } from '@/interfaces/reading-question'
59113
import { unexpectedExc } from '@/utils'
@@ -141,6 +195,33 @@ export default class ReadingExerciseSubmit extends Vue {
141195
get answersCol2 (): ReadingAnswer[] {
142196
return this.answers.filter(answer => answer.question_number > 20)
143197
}
198+
199+
get allAnswered (): boolean {
200+
return this.answers.every(answer => answer.content !== '')
201+
}
202+
203+
confirmSubmit = false
204+
loadingSubmit = false
205+
206+
submitAnswers (): void {
207+
if (this.loadingSubmit) return
208+
this.loadingSubmit = true
209+
210+
const payload: ReadingExerciseSubmitAnswersReq[] = this.answers
211+
.filter(answer => answer.content !== '')
212+
.map(answer => {
213+
delete answer.question_type
214+
delete answer.choices
215+
return answer
216+
})
217+
218+
Api.readingExercise.submitAnswers(this.exercise.pk, payload)
219+
.catch(unexpectedExc)
220+
.finally(() => {
221+
this.confirmSubmit = false
222+
this.loadingSubmit = false
223+
})
224+
}
144225
}
145226
</script>
146227

0 commit comments

Comments
 (0)