import { type Ref, defineComponent, inject, onMounted, ref } from 'vue'; import { useI18n } from 'vue-i18n'; import { useAlertService } from '@/shared/alert/alert.service'; import { type IChoice } from '@/shared/model/choice.model'; import ChoiceService from './choice.service'; export default defineComponent({ name: 'Choice', setup() { const { t: t$ } = useI18n(); const choiceService = inject('choiceService', () => new ChoiceService()); const alertService = inject('alertService', () => useAlertService(), true); const choices: Ref = ref([]); const isFetching = ref(false); const clear = () => {}; const retrieveChoices = async () => { isFetching.value = true; try { const res = await choiceService().retrieve(); choices.value = res.data; } catch (err) { alertService.showHttpError(err.response); } finally { isFetching.value = false; } }; const handleSyncList = () => { retrieveChoices(); }; onMounted(async () => { await retrieveChoices(); }); const removeId: Ref = ref(null); const removeEntity = ref(null); const prepareRemove = (instance: IChoice) => { removeId.value = instance.id; removeEntity.value.show(); }; const closeDialog = () => { removeEntity.value.hide(); }; const removeChoice = async () => { try { await choiceService().delete(removeId.value); const message = t$('questionBankApp.choice.deleted', { param: removeId.value }).toString(); alertService.showInfo(message, { variant: 'danger' }); removeId.value = null; retrieveChoices(); closeDialog(); } catch (error) { alertService.showHttpError(error.response); } }; return { choices, handleSyncList, isFetching, retrieveChoices, clear, removeId, removeEntity, prepareRemove, closeDialog, removeChoice, t$, }; }, });