chore: modify handlers to reusable function
This commit is contained in:
@@ -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
|
// Reusable async handler for CRUD actions with toast and state management
|
||||||
export type ToastFn = (params: { title: string; description: string; variant: 'default' | 'destructive' }) => void
|
export type ToastFn = (params: { title: string; description: string; variant: 'default' | 'destructive' }) => void
|
||||||
|
|
||||||
|
|||||||
@@ -1,101 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
|
|
||||||
// Services
|
|
||||||
import { postDevice, patchDevice, removeDevice } from '~/services/device.service'
|
import { postDevice, patchDevice, removeDevice } from '~/services/device.service'
|
||||||
|
|
||||||
const recId = ref<number>(0)
|
export const {
|
||||||
const recAction = ref<string>('')
|
recId,
|
||||||
const recItem = ref<any>(null)
|
recAction,
|
||||||
const isReadonly = ref(false)
|
recItem,
|
||||||
const isProcessing = ref(false)
|
isReadonly,
|
||||||
const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(
|
post: postDevice,
|
||||||
values: any,
|
patch: patchDevice,
|
||||||
refresh: () => void,
|
remove: removeDevice,
|
||||||
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 }
|
|
||||||
|
|||||||
@@ -1,92 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
|
|
||||||
// Services
|
|
||||||
import { postMaterial, patchMaterial, removeMaterial } from '~/services/material.service'
|
import { postMaterial, patchMaterial, removeMaterial } from '~/services/material.service'
|
||||||
|
|
||||||
const recId = ref<number>(0)
|
export const {
|
||||||
const recAction = ref<string>('')
|
recId,
|
||||||
const recItem = ref<any>(null)
|
recAction,
|
||||||
const isReadonly = ref(false)
|
recItem,
|
||||||
const isProcessing = ref(false)
|
isReadonly,
|
||||||
const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(values: any, refresh: () => void, reset: () => void, toast: ToastFn) {
|
post: postMaterial,
|
||||||
isProcessing.value = true
|
patch: patchMaterial,
|
||||||
await handleAsyncAction<[any], any>({
|
remove: removeMaterial,
|
||||||
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 }
|
|
||||||
|
|||||||
@@ -1,101 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
|
|
||||||
// Services
|
|
||||||
import { postMedicineGroup, patchMedicineGroup, removeMedicineGroup } from '~/services/medicine-group.service'
|
import { postMedicineGroup, patchMedicineGroup, removeMedicineGroup } from '~/services/medicine-group.service'
|
||||||
|
|
||||||
const recId = ref<number>(0)
|
export const {
|
||||||
const recAction = ref<string>('')
|
recId,
|
||||||
const recItem = ref<any>(null)
|
recAction,
|
||||||
const isReadonly = ref(false)
|
recItem,
|
||||||
const isProcessing = ref(false)
|
isReadonly,
|
||||||
const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(
|
post: postMedicineGroup,
|
||||||
values: any,
|
patch: patchMedicineGroup,
|
||||||
refresh: () => void,
|
remove: removeMedicineGroup,
|
||||||
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 }
|
|
||||||
|
|||||||
@@ -1,101 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
|
|
||||||
// Services
|
|
||||||
import { postMedicineMethod, patchMedicineMethod, removeMedicineMethod } from '~/services/medicine-method.service'
|
import { postMedicineMethod, patchMedicineMethod, removeMedicineMethod } from '~/services/medicine-method.service'
|
||||||
|
|
||||||
const recId = ref<number>(0)
|
export const {
|
||||||
const recAction = ref<string>('')
|
recId,
|
||||||
const recItem = ref<any>(null)
|
recAction,
|
||||||
const isReadonly = ref(false)
|
recItem,
|
||||||
const isProcessing = ref(false)
|
isReadonly,
|
||||||
const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(
|
post: postMedicineMethod,
|
||||||
values: any,
|
patch: patchMedicineMethod,
|
||||||
refresh: () => void,
|
remove: removeMedicineMethod,
|
||||||
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 }
|
|
||||||
|
|||||||
@@ -1,94 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
import { postMedicine, patchMedicine, removeMedicine } from '~/services/medicine.service'
|
import { postMedicine, patchMedicine, removeMedicine } from '~/services/medicine.service'
|
||||||
|
|
||||||
export const recId = ref<number>(0)
|
export const {
|
||||||
export const recAction = ref<string>('')
|
recId,
|
||||||
export const recItem = ref<any>(null)
|
recAction,
|
||||||
export const isReadonly = ref(false)
|
recItem,
|
||||||
export const isProcessing = ref(false)
|
isReadonly,
|
||||||
export const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
export const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
export function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(
|
post: postMedicine,
|
||||||
values: any,
|
patch: patchMedicine,
|
||||||
refresh: () => void,
|
remove: removeMedicine,
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|||||||
+19
-90
@@ -1,92 +1,21 @@
|
|||||||
import { ref } from 'vue'
|
import { createCrudHandler } from '~/handlers/_handler'
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
||||||
|
|
||||||
// Services
|
|
||||||
import { postUom, patchUom, removeUom } from '~/services/uom.service'
|
import { postUom, patchUom, removeUom } from '~/services/uom.service'
|
||||||
|
|
||||||
const recId = ref<number>(0)
|
export const {
|
||||||
const recAction = ref<string>('')
|
recId,
|
||||||
const recItem = ref<any>(null)
|
recAction,
|
||||||
const isReadonly = ref(false)
|
recItem,
|
||||||
const isProcessing = ref(false)
|
isReadonly,
|
||||||
const isFormEntryDialogOpen = ref(false)
|
isProcessing,
|
||||||
const isRecordConfirmationOpen = ref(false)
|
isFormEntryDialogOpen,
|
||||||
|
isRecordConfirmationOpen,
|
||||||
function onResetState() {
|
onResetState,
|
||||||
recId.value = 0
|
handleActionSave,
|
||||||
recAction.value = ''
|
handleActionEdit,
|
||||||
recItem.value = null
|
handleActionRemove,
|
||||||
}
|
handleCancelForm,
|
||||||
|
} = createCrudHandler({
|
||||||
export async function handleActionSave(values: any, refresh: () => void, reset: () => void, toast: ToastFn) {
|
post: postUom,
|
||||||
isProcessing.value = true
|
patch: patchUom,
|
||||||
await handleAsyncAction<[any], any>({
|
remove: removeUom,
|
||||||
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 }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user