chore: modify handlers to reusable function

This commit is contained in:
riefive
2025-09-29 10:17:33 +07:00
parent 9f950d552d
commit b221239c32
7 changed files with 220 additions and 569 deletions
+106
View File
@@ -1,3 +1,109 @@
import { ref } from 'vue'
// Factory for CRUD handler state and actions
export function createCrudHandler<T = any>(crud: {
post: (...args: any[]) => Promise<any>
patch: (...args: any[]) => Promise<any>
remove: (...args: any[]) => Promise<any>
}) {
const recId = ref<number>(0)
const recAction = ref<string>('')
const recItem = ref<T | null>(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
+19 -99
View File
@@ -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<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(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,
})
+19 -90
View File
@@ -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<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(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,
})
+19 -99
View File
@@ -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<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(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,
})
+19 -99
View File
@@ -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<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(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,
})
+19 -92
View File
@@ -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<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);
}
export const {
recId,
recAction,
recItem,
isReadonly,
isProcessing,
isFormEntryDialogOpen,
isRecordConfirmationOpen,
onResetState,
handleActionSave,
handleActionEdit,
handleActionRemove,
handleCancelForm,
} = createCrudHandler({
post: postMedicine,
patch: patchMedicine,
remove: removeMedicine,
})
+19 -90
View File
@@ -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<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(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,
})