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 { useAlertService } from '@/shared/alert/alert.service'; import { useValidation } from '@/shared/composables'; import { type ISubject, Subject } from '@/shared/model/subject.model'; import SubjectService from './subject.service'; export default defineComponent({ name: 'SubjectUpdate', setup() { const subjectService = inject('subjectService', () => new SubjectService()); const alertService = inject('alertService', () => useAlertService(), true); const subject: Ref = ref(new Subject()); 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 retrieveSubject = async subjectId => { try { const res = await subjectService().find(subjectId); subject.value = res; } catch (error) { alertService.showHttpError(error.response); } }; if (route.params?.subjectId) { retrieveSubject(route.params.subjectId); } const { t: t$ } = useI18n(); const validations = useValidation(); const validationRules = { name: { required: validations.required(t$('entity.validation.required').toString()), minLength: validations.minLength(t$('entity.validation.minlength', { min: 2 }).toString(), 2), maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 100 }).toString(), 100), }, description: { maxLength: validations.maxLength(t$('entity.validation.maxlength', { max: 1000 }).toString(), 1000), }, active: { required: validations.required(t$('entity.validation.required').toString()), }, }; const v$ = useVuelidate(validationRules, subject as any); v$.value.$validate(); return { subjectService, alertService, subject, previousState, isSaving, currentLanguage, v$, t$, }; }, created(): void {}, methods: { save(): void { this.isSaving = true; if (this.subject.id) { this.subjectService() .update(this.subject) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showInfo(this.t$('questionBankApp.subject.updated', { param: param.id })); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } else { this.subjectService() .create(this.subject) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showSuccess(this.t$('questionBankApp.subject.created', { param: param.id }).toString()); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } }, }, });