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 StrutturaService from '@/entities/struttura/struttura.service'; import { useAlertService } from '@/shared/alert/alert.service'; import { useDateFormat, useValidation } from '@/shared/composables'; import useDataUtils from '@/shared/data/data-utils.service'; import { type IModelloLiberatoria, ModelloLiberatoria } from '@/shared/model/modello-liberatoria.model'; import { type IStruttura } from '@/shared/model/struttura.model'; import ModelloLiberatoriaService from './modello-liberatoria.service'; export default defineComponent({ name: 'ModelloLiberatoriaUpdate', setup() { const modelloLiberatoriaService = inject('modelloLiberatoriaService', () => new ModelloLiberatoriaService()); const alertService = inject('alertService', () => useAlertService(), true); const modelloLiberatoria: Ref = ref(new ModelloLiberatoria()); const strutturaService = inject('strutturaService', () => new StrutturaService()); const strutturas: Ref = ref([]); const isSaving = ref(false); const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'it'), true); const route = useRoute(); const router = useRouter(); const previousState = () => router.go(-1); const retrieveModelloLiberatoria = async modelloLiberatoriaId => { try { const res = await modelloLiberatoriaService().find(modelloLiberatoriaId); res.validoDal = new Date(res.validoDal); res.validoAl = new Date(res.validoAl); modelloLiberatoria.value = res; } catch (error) { alertService.showHttpError(error.response); } }; if (route.params?.modelloLiberatoriaId) { retrieveModelloLiberatoria(route.params.modelloLiberatoriaId); } const initRelationships = () => { strutturaService() .retrieve() .then(res => { strutturas.value = res.data; }); }; initRelationships(); const dataUtils = useDataUtils(); const { t: t$ } = useI18n(); const validations = useValidation(); const validationRules = { nome: {}, testo: {}, documento: {}, validoDal: {}, validoAl: {}, struttura: {}, }; const v$ = useVuelidate(validationRules, modelloLiberatoria as any); v$.value.$validate(); return { modelloLiberatoriaService, alertService, modelloLiberatoria, previousState, isSaving, currentLanguage, strutturas, ...dataUtils, v$, ...useDateFormat({ entityRef: modelloLiberatoria }), t$, }; }, created(): void {}, methods: { save(): void { this.isSaving = true; if (this.modelloLiberatoria.id) { this.modelloLiberatoriaService() .update(this.modelloLiberatoria) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showInfo(this.t$('smartbookingApp.modelloLiberatoria.updated', { param: param.id })); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } else { this.modelloLiberatoriaService() .create(this.modelloLiberatoria) .then(param => { this.isSaving = false; this.previousState(); this.alertService.showSuccess(this.t$('smartbookingApp.modelloLiberatoria.created', { param: param.id }).toString()); }) .catch(error => { this.isSaving = false; this.alertService.showHttpError(error.response); }); } }, }, });