diff --git a/app/handlers/_handler.ts b/app/handlers/_handler.ts index d05e6f39..4148c726 100644 --- a/app/handlers/_handler.ts +++ b/app/handlers/_handler.ts @@ -1,3 +1,109 @@ +import { ref } from 'vue' + +// Factory for CRUD handler state and actions +export function createCrudHandler(crud: { + post: (...args: any[]) => Promise + patch: (...args: any[]) => Promise + remove: (...args: any[]) => Promise +}) { + 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 + } + + async function handleActionSave(values: any, refresh: () => void, reset: () => void, toast: ToastFn) { + isProcessing.value = true + await handleAsyncAction<[any], any>({ + action: crud.post, + 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 + }, + }) + } + + async function handleActionEdit( + id: number | string, + values: any, + refresh: () => void, + reset: () => void, + toast: ToastFn, + ) { + isProcessing.value = true + await handleAsyncAction<[number | string, any], any>({ + action: crud.patch, + 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 + }, + }) + } + + async function handleActionRemove(id: number | string, refresh: () => void, toast: ToastFn) { + isProcessing.value = true + await handleAsyncAction<[number | string], any>({ + action: crud.remove, + args: [id], + toast, + successMessage: 'Data berhasil dihapus', + errorMessage: 'Gagal menghapus data', + onSuccess: () => { + isRecordConfirmationOpen.value = false + if (refresh) refresh() + }, + onFinally: () => { + isProcessing.value = false + }, + }) + } + + function handleCancelForm(reset: () => void) { + isFormEntryDialogOpen.value = false + isReadonly.value = false + setTimeout(reset, 300) + } + + return { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, + } +} + // Reusable async handler for CRUD actions with toast and state management export type ToastFn = (params: { title: string; description: string; variant: 'default' | 'destructive' }) => void diff --git a/app/handlers/device.handler.ts b/app/handlers/device.handler.ts index e269f61b..14de6830 100644 --- a/app/handlers/device.handler.ts +++ b/app/handlers/device.handler.ts @@ -1,101 +1,21 @@ -import { ref } from 'vue' - -// Handlers -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' - -// Services +import { createCrudHandler } from '~/handlers/_handler' import { postDevice, patchDevice, removeDevice } from '~/services/device.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: postDevice, - 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: patchDevice, - 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: removeDevice, - 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 } +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postDevice, + patch: patchDevice, + remove: removeDevice, +}) diff --git a/app/handlers/material.handler.ts b/app/handlers/material.handler.ts index d61e5f01..750f7967 100644 --- a/app/handlers/material.handler.ts +++ b/app/handlers/material.handler.ts @@ -1,92 +1,21 @@ -import { ref } from 'vue' - -// Handlers -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' - -// Services +import { createCrudHandler } from '~/handlers/_handler' import { postMaterial, patchMaterial, removeMaterial } from '~/services/material.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: postMaterial, - 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: patchMaterial, - 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: removeMaterial, - 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 } +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postMaterial, + patch: patchMaterial, + remove: removeMaterial, +}) diff --git a/app/handlers/medicine-group.handler.ts b/app/handlers/medicine-group.handler.ts index f349355f..998ab285 100644 --- a/app/handlers/medicine-group.handler.ts +++ b/app/handlers/medicine-group.handler.ts @@ -1,101 +1,21 @@ -import { ref } from 'vue' - -// Handlers -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' - -// Services +import { createCrudHandler } from '~/handlers/_handler' 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 } +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postMedicineGroup, + patch: patchMedicineGroup, + remove: removeMedicineGroup, +}) diff --git a/app/handlers/medicine-method.handler.ts b/app/handlers/medicine-method.handler.ts index 2a681776..df4e0ff9 100644 --- a/app/handlers/medicine-method.handler.ts +++ b/app/handlers/medicine-method.handler.ts @@ -1,101 +1,21 @@ -import { ref } from 'vue' - -// Handlers -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' - -// Services +import { createCrudHandler } from '~/handlers/_handler' 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 } +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postMedicineMethod, + patch: patchMedicineMethod, + remove: removeMedicineMethod, +}) diff --git a/app/handlers/medicine.handler.ts b/app/handlers/medicine.handler.ts index a727852b..d550f11b 100644 --- a/app/handlers/medicine.handler.ts +++ b/app/handlers/medicine.handler.ts @@ -1,94 +1,21 @@ -import { ref } from 'vue' -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' +import { createCrudHandler } from '~/handlers/_handler' import { postMedicine, patchMedicine, removeMedicine } from '~/services/medicine.service' -export const recId = ref(0) -export const recAction = ref('') -export const recItem = ref(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); -} +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postMedicine, + patch: patchMedicine, + remove: removeMedicine, +}) diff --git a/app/handlers/uom.handler.ts b/app/handlers/uom.handler.ts index e24fbcfa..dd018400 100644 --- a/app/handlers/uom.handler.ts +++ b/app/handlers/uom.handler.ts @@ -1,92 +1,21 @@ -import { ref } from 'vue' - -// Handlers -import { type ToastFn, handleAsyncAction } from '~/handlers/_handler' - -// Services +import { createCrudHandler } from '~/handlers/_handler' import { postUom, patchUom, removeUom } from '~/services/uom.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: postUom, - 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: patchUom, - 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: removeUom, - 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 } +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postUom, + patch: patchUom, + remove: removeUom, +})