141 lines
4.5 KiB
TypeScript
141 lines
4.5 KiB
TypeScript
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 SubjectService from '@/entities/subject/subject.service';
|
|
import UserService from '@/entities/user/user.service';
|
|
import { useAlertService } from '@/shared/alert/alert.service';
|
|
import { useDateFormat, useValidation } from '@/shared/composables';
|
|
import { AttemptStatus } from '@/shared/model/enumerations/attempt-status.model';
|
|
import { ExamAttempt, type IExamAttempt } from '@/shared/model/exam-attempt.model';
|
|
import { type ISubject } from '@/shared/model/subject.model';
|
|
|
|
import ExamAttemptService from './exam-attempt.service';
|
|
|
|
export default defineComponent({
|
|
name: 'ExamAttemptUpdate',
|
|
setup() {
|
|
const examAttemptService = inject('examAttemptService', () => new ExamAttemptService());
|
|
const alertService = inject('alertService', () => useAlertService(), true);
|
|
|
|
const examAttempt: Ref<IExamAttempt> = ref(new ExamAttempt());
|
|
|
|
const subjectService = inject('subjectService', () => new SubjectService());
|
|
|
|
const subjects: Ref<ISubject[]> = ref([]);
|
|
const userService = inject('userService', () => new UserService());
|
|
const users: Ref<Array<any>> = ref([]);
|
|
const attemptStatusValues: Ref<string[]> = ref(Object.keys(AttemptStatus));
|
|
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 retrieveExamAttempt = async examAttemptId => {
|
|
try {
|
|
const res = await examAttemptService().find(examAttemptId);
|
|
res.startedAt = new Date(res.startedAt);
|
|
res.submittedAt = new Date(res.submittedAt);
|
|
examAttempt.value = res;
|
|
} catch (error) {
|
|
alertService.showHttpError(error.response);
|
|
}
|
|
};
|
|
|
|
if (route.params?.examAttemptId) {
|
|
retrieveExamAttempt(route.params.examAttemptId);
|
|
}
|
|
|
|
const initRelationships = () => {
|
|
subjectService()
|
|
.retrieve()
|
|
.then(res => {
|
|
subjects.value = res.data;
|
|
});
|
|
userService()
|
|
.retrieve()
|
|
.then(res => {
|
|
users.value = res.data;
|
|
});
|
|
};
|
|
|
|
initRelationships();
|
|
|
|
const { t: t$ } = useI18n();
|
|
const validations = useValidation();
|
|
const validationRules = {
|
|
startedAt: {
|
|
required: validations.required(t$('entity.validation.required').toString()),
|
|
},
|
|
submittedAt: {},
|
|
status: {
|
|
required: validations.required(t$('entity.validation.required').toString()),
|
|
},
|
|
score: {
|
|
integer: validations.integer(t$('entity.validation.number').toString()),
|
|
min: validations.minValue(t$('entity.validation.min', { min: 0 }).toString(), 0),
|
|
},
|
|
answers: {},
|
|
subject: {
|
|
required: validations.required(t$('entity.validation.required').toString()),
|
|
},
|
|
user: {
|
|
required: validations.required(t$('entity.validation.required').toString()),
|
|
},
|
|
};
|
|
const v$ = useVuelidate(validationRules, examAttempt as any);
|
|
v$.value.$validate();
|
|
|
|
return {
|
|
examAttemptService,
|
|
alertService,
|
|
examAttempt,
|
|
previousState,
|
|
attemptStatusValues,
|
|
isSaving,
|
|
currentLanguage,
|
|
subjects,
|
|
users,
|
|
v$,
|
|
...useDateFormat({ entityRef: examAttempt }),
|
|
t$,
|
|
};
|
|
},
|
|
created(): void {},
|
|
methods: {
|
|
save(): void {
|
|
this.isSaving = true;
|
|
if (this.examAttempt.id) {
|
|
this.examAttemptService()
|
|
.update(this.examAttempt)
|
|
.then(param => {
|
|
this.isSaving = false;
|
|
this.previousState();
|
|
this.alertService.showInfo(this.t$('questionBankApp.examAttempt.updated', { param: param.id }));
|
|
})
|
|
.catch(error => {
|
|
this.isSaving = false;
|
|
this.alertService.showHttpError(error.response);
|
|
});
|
|
} else {
|
|
this.examAttemptService()
|
|
.create(this.examAttempt)
|
|
.then(param => {
|
|
this.isSaving = false;
|
|
this.previousState();
|
|
this.alertService.showSuccess(this.t$('questionBankApp.examAttempt.created', { param: param.id }).toString());
|
|
})
|
|
.catch(error => {
|
|
this.isSaving = false;
|
|
this.alertService.showHttpError(error.response);
|
|
});
|
|
}
|
|
},
|
|
},
|
|
});
|