From 94e8b26fa828e96081fca5e9428ffe160cbb059c Mon Sep 17 00:00:00 2001 From: riefive Date: Thu, 25 Sep 2025 16:03:01 +0700 Subject: [PATCH] feat(medicine): add handlers --- app/handlers/medicine-group.handler.ts | 101 ++++++++++++++++++++++++ app/handlers/medicine-method.handler.ts | 101 ++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 app/handlers/medicine-group.handler.ts create mode 100644 app/handlers/medicine-method.handler.ts diff --git a/app/handlers/medicine-group.handler.ts b/app/handlers/medicine-group.handler.ts new file mode 100644 index 00000000..f349355f --- /dev/null +++ b/app/handlers/medicine-group.handler.ts @@ -0,0 +1,101 @@ +import { ref } from 'vue' + +// Handlers +import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' + +// Services +import { postMedicineGroup, patchMedicineGroup, removeMedicineGroup } from '~/services/medicine-group.service' + +const recId = ref(0) +const recAction = ref('') +const recItem = ref(null) +const isReadonly = ref(false) +const isProcessing = ref(false) +const isFormEntryDialogOpen = ref(false) +const isRecordConfirmationOpen = ref(false) + +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: postMedicineGroup, + 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: patchMedicineGroup, + 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: removeMedicineGroup, + args: [id], + toast, + successMessage: 'Data berhasil dihapus', + errorMessage: 'Gagal menghapus data', + onSuccess: () => { + if (refresh) refresh(); + }, + onFinally: () => { + onResetState(); + isProcessing.value = false; + }, + }); +} + +export function handleCancelForm(reset: () => void) { + isFormEntryDialogOpen.value = false + setTimeout(() => { + reset() + }, 500) +} + +export { recId, recAction, recItem, isReadonly, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen } diff --git a/app/handlers/medicine-method.handler.ts b/app/handlers/medicine-method.handler.ts new file mode 100644 index 00000000..2a681776 --- /dev/null +++ b/app/handlers/medicine-method.handler.ts @@ -0,0 +1,101 @@ +import { ref } from 'vue' + +// Handlers +import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' + +// Services +import { postMedicineMethod, patchMedicineMethod, removeMedicineMethod } from '~/services/medicine-method.service' + +const recId = ref(0) +const recAction = ref('') +const recItem = ref(null) +const isReadonly = ref(false) +const isProcessing = ref(false) +const isFormEntryDialogOpen = ref(false) +const isRecordConfirmationOpen = ref(false) + +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: postMedicineMethod, + 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: patchMedicineMethod, + 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: removeMedicineMethod, + args: [id], + toast, + successMessage: 'Data berhasil dihapus', + errorMessage: 'Gagal menghapus data', + onSuccess: () => { + if (refresh) refresh(); + }, + onFinally: () => { + onResetState(); + isProcessing.value = false; + }, + }); +} + +export function handleCancelForm(reset: () => void) { + isFormEntryDialogOpen.value = false + setTimeout(() => { + reset() + }, 500) +} + +export { recId, recAction, recItem, isReadonly, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen }