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 { useAlertService } from '@/shared/alert/alert.service'; import { useValidation } from '@/shared/composables'; import { type IQuestion, Question } from '@/shared/model/question.model'; import { type ISubject } from '@/shared/model/subject.model'; import QuestionService from './question.service'; export default defineComponent({ name: 'QuestionUpdate', setup() { const questionService = inject('questionService', () => new QuestionService()); const alertService = inject('alertService', () => useAlertService(), true); const question: Ref = ref(new Question()); const subjectService = inject('subjectService', () => new SubjectService()); const subjects: Ref = ref([]); 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 retrieveQuestion = async questionId => { try { const res = await questionService().find(questionId); question.value = res; } catch (error) { alertService.showHttpError(error.response); } }; if (route.params?.questionId) { retrieveQuestion(route.params.questionId); } const initRelationships = () => { subjectService() .retrieve({ page: 0, size: 100 }) .then(res => { subjects.value = res.data; }); }; initRelationships(); const { t: t$ } = useI18n(); const validations = useValidation(); const validationRules = { prompt: { required: validations.required(t$('entity.validation.required').toString()), minLength: validations.minLength(t$('entity.validation.minlength', { min: 5 }).toString(), 5), maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 2000 }).toString(), 2000), }, explanation: { maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 2000 }).toString(), 2000), }, active: { required: validations.required(t$('entity.validation.required').toString()), }, choices: {}, subject: { required: validations.required(t$('entity.validation.required').toString()), }, }; const v$ = useVuelidate(validationRules, question as any); v$.value.$validate(); return { questionService, alertService, question, previousState, isSaving, currentLanguage, subjects, v$, t$, }; }, created(): void {}, methods: { save(): void { this.isSaving = true; if (this.question.id) { this.questionService() .update(this.question) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showInfo(this.t$('questionBankApp.question.updated', { param: param.id })); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } else { this.questionService() .create(this.question) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showSuccess(this.t$('questionBankApp.question.created', { param: param.id }).toString()); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } }, }, });