diff --git a/app/components/app/tools/entry-form.vue b/app/components/app/tools/entry-form.vue index f1413f62..b8e3fe89 100644 --- a/app/components/app/tools/entry-form.vue +++ b/app/components/app/tools/entry-form.vue @@ -9,7 +9,6 @@ import type { DeviceFormData } from '~/schemas/device' interface Props { schema: z.ZodSchema uoms: any[] - items: any[] } const isLoading = ref(false) @@ -32,13 +31,11 @@ const { handleSubmit, defineField, errors } = useForm({ const [code, codeAttrs] = defineField('code') const [name, nameAttrs] = defineField('name') const [uom, uomAttrs] = defineField('uom_code') -const [item, itemAttrs] = defineField('item_id') const resetForm = () => { code.value = '' name.value = '' uom.value = '' - item.value = '' } // Form submission handler @@ -47,7 +44,6 @@ function onSubmitForm(values: any) { name: values.name || '', code: values.code || '', uom_code: values.uom_code || '', - item_id: values.item_id || '', } emit('submit', formData, resetForm) } @@ -59,7 +55,7 @@ function onCancelForm() { \ No newline at end of file diff --git a/app/handlers/device.handler.ts b/app/handlers/device.handler.ts new file mode 100644 index 00000000..936fcbe1 --- /dev/null +++ b/app/handlers/device.handler.ts @@ -0,0 +1,87 @@ +import { ref } from 'vue' + +// Services +import { postSourceDevice, putSourceDevice, removeSourceDevice } from '~/services/device.service' + +const recId = ref(0) +const recAction = ref('') +const recItem = ref(null) +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) { + let isSuccess = false + isProcessing.value = true + try { + const result = await postSourceDevice(values) + if (result.success) { + isFormEntryDialogOpen.value = false + isSuccess = true + if (refresh) refresh() + } + } catch (error) { + console.warn('Error saving form:', error) + isSuccess = false + } finally { + if (isSuccess) { + setTimeout(() => { + reset() + }, 500) + } + isProcessing.value = false + } +} + +export async function handleActionEdit(id: number | string, values: any, refresh: () => void, reset: () => void) { + let isSuccess = false + isProcessing.value = true + try { + const result = await putSourceDevice(id, values) + if (result.success) { + isFormEntryDialogOpen.value = false + isSuccess = true + if (refresh) refresh() + } + } catch (error) { + console.warn('Error editing form:', error) + isSuccess = false + } finally { + if (isSuccess) { + setTimeout(() => { + reset() + }, 500) + } + isProcessing.value = false + } +} + +export async function handleActionRemove(id: number | string, refresh: () => void) { + isProcessing.value = true + try { + const result = await removeSourceDevice(id) + if (result.success) { + if (refresh) refresh() + } + } catch (error) { + console.error('Error deleting record:', error) + } finally { + onResetState() + isProcessing.value = false + } +} + +export function handleCancelForm(reset: () => void) { + isFormEntryDialogOpen.value = false + setTimeout(() => { + reset() + }, 500) +} + +export { recId, recAction, recItem, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen } diff --git a/app/models/device.ts b/app/models/device.ts new file mode 100644 index 00000000..ec7f7443 --- /dev/null +++ b/app/models/device.ts @@ -0,0 +1,5 @@ +export interface Device { + code: string + name: string + uom_code: string +} diff --git a/app/schemas/device.ts b/app/schemas/device.ts index 036f3bc7..0dfffd3e 100644 --- a/app/schemas/device.ts +++ b/app/schemas/device.ts @@ -1,13 +1,13 @@ import { z } from 'zod' +import type { Device } from '~/models/device' -const schema = z.object({ +const DeviceSchema = z.object({ code: z.string({ required_error: 'Kode harus diisi' }).min(1, 'Kode minimum 1 karakter'), name: z.string({ required_error: 'Nama harus diisi' }).min(1, 'Nama minimum 1 karakter'), uom_code: z.string({ required_error: 'Kode unit harus diisi' }).min(1, 'Kode unit harus diisi'), - item_id: z.string({ required_error: 'Tipe harus diisi' }).min(1, 'Tipe harus diisi'), }) -type formData = z.infer +type DeviceFormData = z.infer & Device -export { schema as DeviceSchema } -export type { formData as DeviceFormData } +export { DeviceSchema } +export type { DeviceFormData } diff --git a/app/services/device.service.ts b/app/services/device.service.ts new file mode 100644 index 00000000..0f5c7ddb --- /dev/null +++ b/app/services/device.service.ts @@ -0,0 +1,72 @@ +import { xfetch } from '~/composables/useXfetch' + +export async function getSourceDevices(params: any = null) { + try { + let url = '/api/v1/device' + if (params && typeof params === 'object' && Object.keys(params).length > 0) { + const searchParams = new URLSearchParams() + for (const key in params) { + if (params[key] !== null && params[key] !== undefined && params[key] !== '') { + searchParams.append(key, params[key]) + } + } + const queryString = searchParams.toString() + if (queryString) url += `?${queryString}` + } + const resp = await xfetch(url, 'GET') + const result: any = {} + result.success = resp.success + if (resp.success) { + result.data = (resp.body as Record).data + } + return result + } catch (error) { + console.error('Error fetching source devices:', error) + throw new Error('Failed to fetch source devices') + } +} + +export async function getSourceDeviceDetail(id: string | number) { + try { + const resp = await xfetch(`/api/v1/device/${id}`, 'GET') + const result: any = {} + result.success = resp.success + if (resp.success) { + result.data = (resp.body as Record).data + } + return result + } catch (error) { + console.error('Error fetching device detail:', error) + throw new Error('Failed to fetch device detail') + } +} + +export async function postSourceDevice(data: any) { + try { + const resp = await xfetch('/api/v1/device', 'POST', data) + return resp + } catch (error) { + console.error('Error creating device:', error) + throw new Error('Failed to create device') + } +} + +export async function putSourceDevice(id: string | number, data: any) { + try { + const resp = await xfetch(`/api/v1/device/${id}`, 'PUT', data) + return resp + } catch (error) { + console.error('Error updating device:', error) + throw new Error('Failed to update device') + } +} + +export async function removeSourceDevice(id: string | number) { + try { + const resp = await xfetch(`/api/v1/device/${id}`, 'DELETE') + return resp + } catch (error) { + console.error('Error deleting device:', error) + throw new Error('Failed to delete device') + } +}