import { type Ref, defineComponent, inject, onMounted, ref } from 'vue'; import RegistrationService from './registration.service'; import { type IRegistration } from '@/shared/model/registration.model'; import useDataUtils from '@/shared/data/data-utils.service'; import { useDateFormat } from '@/shared/composables'; import { useAlertService } from '@/shared/alert/alert.service'; export default defineComponent({ compatConfig: { MODE: 3 }, name: 'Registration', setup() { const dateFormat = useDateFormat(); const dataUtils = useDataUtils(); const registrationService = inject('registrationService', () => new RegistrationService()); const alertService = inject('alertService', () => useAlertService(), true); const registrations: Ref = ref([]); const isFetching = ref(false); const clear = () => {}; const retrieveRegistrations = async () => { isFetching.value = true; try { const res = await registrationService().retrieve(); registrations.value = res.data; } catch (err) { alertService.showHttpError(err.response); } finally { isFetching.value = false; } }; const handleSyncList = () => { retrieveRegistrations(); }; onMounted(async () => { await retrieveRegistrations(); }); const removeId: Ref = ref(null); const removeEntity = ref(null); const prepareRemove = (instance: IRegistration) => { removeId.value = instance.id; removeEntity.value.show(); }; const closeDialog = () => { removeEntity.value.hide(); }; const removeRegistration = async () => { try { await registrationService().delete(removeId.value); const message = `A Registration is deleted with identifier ${removeId.value}`; alertService.showInfo(message, { variant: 'danger' }); removeId.value = null; retrieveRegistrations(); closeDialog(); } catch (error) { alertService.showHttpError(error.response); } }; return { registrations, handleSyncList, isFetching, retrieveRegistrations, clear, ...dateFormat, removeId, removeEntity, prepareRemove, closeDialog, removeRegistration, ...dataUtils, }; }, });