Files
simrsx-fe/app/handlers/medicine.handler.ts
T

95 lines
2.4 KiB
TypeScript

import { ref } from 'vue'
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
import { postMedicine, patchMedicine, removeMedicine } from '~/services/medicine.service'
export const recId = ref<number>(0)
export const recAction = ref<string>('')
export const recItem = ref<any>(null)
export const isReadonly = ref(false)
export const isProcessing = ref(false)
export const isFormEntryDialogOpen = ref(false)
export const isRecordConfirmationOpen = ref(false)
export function onResetState() {
recId.value = 0
recAction.value = ''
recItem.value = null
}
export async function handleActionSave(
values: any,
refresh: () => void,
reset: () => void,
toast: ToastFn
) {
isProcessing.value = true;
await handleAsyncAction<[any], any>({
action: postMedicine,
args: [values],
toast,
successMessage: 'Data berhasil disimpan',
errorMessage: 'Gagal menyimpan data',
onSuccess: () => {
isFormEntryDialogOpen.value = false;
if (refresh) refresh();
},
onFinally: (isSuccess: boolean) => {
if (isSuccess) setTimeout(reset, 500);
isProcessing.value = false;
},
});
}
export async function handleActionEdit(
id: number | string,
values: any,
refresh: () => void,
reset: () => void,
toast: ToastFn
) {
isProcessing.value = true;
await handleAsyncAction<[number | string, any], any>({
action: patchMedicine,
args: [id, values],
toast,
successMessage: 'Data berhasil diubah',
errorMessage: 'Gagal mengubah data',
onSuccess: () => {
isFormEntryDialogOpen.value = false;
if (refresh) refresh();
},
onFinally: (isSuccess: boolean) => {
if (isSuccess) setTimeout(reset, 500);
isProcessing.value = false;
},
});
}
export async function handleActionRemove(
id: number | string,
refresh: () => void,
toast: ToastFn
) {
isProcessing.value = true;
await handleAsyncAction<[number | string], any>({
action: removeMedicine,
args: [id],
toast,
successMessage: 'Data berhasil dihapus',
errorMessage: 'Gagal menghapus data',
onSuccess: () => {
isRecordConfirmationOpen.value = false;
if (refresh) refresh();
},
onFinally: () => {
isProcessing.value = false;
},
});
}
export function handleCancelForm(reset: () => void) {
isFormEntryDialogOpen.value = false;
isReadonly.value = false;
setTimeout(reset, 300);
}