import { type Ref, computed, defineComponent, inject, ref } from 'vue'; import { useI18n } from 'vue-i18n'; import { useRoute, useRouter } from 'vue-router'; import { useVuelidate } from '@vuelidate/core'; import ExamAttemptService from '@/entities/exam-attempt/exam-attempt.service'; import QuestionService from '@/entities/question/question.service'; import { useAlertService } from '@/shared/alert/alert.service'; import { useValidation } from '@/shared/composables'; import { OptionKey } from '@/shared/model/enumerations/option-key.model'; import { ExamAnswer, type IExamAnswer } from '@/shared/model/exam-answer.model'; import { type IExamAttempt } from '@/shared/model/exam-attempt.model'; import { type IQuestion } from '@/shared/model/question.model'; import ExamAnswerService from './exam-answer.service'; export default defineComponent({ name: 'ExamAnswerUpdate', setup() { const examAnswerService = inject('examAnswerService', () => new ExamAnswerService()); const alertService = inject('alertService', () => useAlertService(), true); const examAnswer: Ref = ref(new ExamAnswer()); const questionService = inject('questionService', () => new QuestionService()); const questions: Ref = ref([]); const examAttemptService = inject('examAttemptService', () => new ExamAttemptService()); const examAttempts: Ref = ref([]); const optionKeyValues: Ref = ref(Object.keys(OptionKey)); const isSaving = ref(false); const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'en'), true); const route = useRoute(); const router = useRouter(); const previousState = () => router.go(-1); const retrieveExamAnswer = async examAnswerId => { try { const res = await examAnswerService().find(examAnswerId); examAnswer.value = res; } catch (error) { alertService.showHttpError(error.response); } }; if (route.params?.examAnswerId) { retrieveExamAnswer(route.params.examAnswerId); } const initRelationships = () => { questionService() .retrieve() .then(res => { questions.value = res.data; }); examAttemptService() .retrieve() .then(res => { examAttempts.value = res.data; }); }; initRelationships(); const { t: t$ } = useI18n(); const validations = useValidation(); const validationRules = { questionTextSnapshot: { required: validations.required(t$('entity.validation.required').toString()), maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 2000 }).toString(), 2000), }, explanationSnapshot: { maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 2000 }).toString(), 2000), }, selectedOption: {}, selectedContentSnapshot: { maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 500 }).toString(), 500), }, correct: {}, awardedPoints: { integer: validations.integer(t$('entity.validation.number').toString()), min: validations.minValue(t$('entity.validation.min', { min: 0 }).toString(), 0), }, question: { required: validations.required(t$('entity.validation.required').toString()), }, attempt: { required: validations.required(t$('entity.validation.required').toString()), }, }; const v$ = useVuelidate(validationRules, examAnswer as any); v$.value.$validate(); return { examAnswerService, alertService, examAnswer, previousState, optionKeyValues, isSaving, currentLanguage, questions, examAttempts, v$, t$, }; }, created(): void {}, methods: { save(): void { this.isSaving = true; if (this.examAnswer.id) { this.examAnswerService() .update(this.examAnswer) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showInfo(this.t$('questionBankApp.examAnswer.updated', { param: param.id })); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } else { this.examAnswerService() .create(this.examAnswer) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showSuccess(this.t$('questionBankApp.examAnswer.created', { param: param.id }).toString()); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } }, }, });