diff --git a/app/components/app/equipment/entry-form.vue b/app/components/app/equipment/entry-form.vue index 04fe56ea..31bf78e1 100644 --- a/app/components/app/equipment/entry-form.vue +++ b/app/components/app/equipment/entry-form.vue @@ -14,6 +14,7 @@ import Label from '~/components/pub/custom-ui/doc-entry/label.vue' interface Props { schema: z.ZodSchema uoms: any[] + values: any } const isLoading = ref(false) @@ -23,7 +24,7 @@ const emit = defineEmits<{ cancel: [resetForm: () => void] }>() -const { handleSubmit, defineField, errors, meta } = useForm({ +const { defineField, errors, meta } = useForm({ validationSchema: toTypedSchema(props.schema), initialValues: { code: '', @@ -38,6 +39,14 @@ const [name, nameAttrs] = defineField('name') const [uom, uomAttrs] = defineField('uom_code') const [stock, stockAttrs] = defineField('stock') +// Fill fields from props.values if provided +if (props.values) { + if (props.values.code !== undefined) code.value = props.values.code + if (props.values.name !== undefined) name.value = props.values.name + if (props.values.uom_code !== undefined) uom.value = props.values.uom_code + if (props.values.stock !== undefined) stock.value = props.values.stock +} + const resetForm = () => { code.value = '' name.value = '' diff --git a/app/components/content/equipment/list.vue b/app/components/content/equipment/list.vue index 52bb9a94..d7ffa76f 100644 --- a/app/components/content/equipment/list.vue +++ b/app/components/content/equipment/list.vue @@ -21,6 +21,7 @@ import { handleActionSave, handleActionRemove, handleCancelForm, + handleActionEdit, } from '~/handlers/material.handler' // Services @@ -32,7 +33,8 @@ const uoms = ref<{ value: string; label: string }[]>([]) const getEquipmentDetail = async (id: number | string) => { const result = await getSourceMaterialDetail(id) if (result.success) { - recItem.value = result.data + const currentMaterial = result.body?.data || {} + recItem.value = currentMaterial isFormEntryDialogOpen.value = true } } @@ -40,7 +42,8 @@ const getEquipmentDetail = async (id: number | string) => { const getUomList = async () => { const result = await getSourceUoms() if (result.success) { - uoms.value = result.data.map((uom: Uom) => ({ value: uom.code || uom.erp_id, label: uom.name })) + const currentUoms = result.body?.data || [] + uoms.value = currentUoms.map((uom: Uom) => ({ value: uom.code || uom.erp_id, label: uom.name })) } } @@ -55,7 +58,7 @@ const { } = usePaginatedList({ fetchFn: async ({ page }) => { const result = await getSourceMaterials({ page }) - return { success: result.success || false, body: { data: result.data || [], meta: result.meta || {} } } + return { success: result.success || false, body: result.body || {} } }, entityName: 'equipment', }) @@ -117,12 +120,26 @@ onMounted(async () => { - + diff --git a/app/services/material.service.ts b/app/services/material.service.ts index 2b9a6d3c..53868d9f 100644 --- a/app/services/material.service.ts +++ b/app/services/material.service.ts @@ -2,38 +2,34 @@ import { xfetch } from '~/composables/useXfetch' export async function getSourceMaterials(params: any = null) { try { - let url = '/api/v1/material'; + let url = '/api/v1/material' if (params && typeof params === 'object' && Object.keys(params).length > 0) { - const searchParams = new URLSearchParams(); + const searchParams = new URLSearchParams() for (const key in params) { if (params[key] !== null && params[key] !== undefined && params[key] !== '') { - searchParams.append(key, params[key]); + searchParams.append(key, params[key]) } } - const queryString = searchParams.toString(); - if (queryString) url += `?${queryString}`; + 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; + const resp = await xfetch(url, 'GET') + const result: any = {} + result.success = resp.success + result.body = (resp.body as Record) || {} + return result } catch (error) { - console.error('Error fetching source materials:', error); - throw new Error('Failed to fetch source materials'); + console.error('Error fetching source materials:', error) + throw new Error('Failed to fetch source materials') } } export async function getSourceMaterialDetail(id: number | string) { try { - const resp = await xfetch(`/api/v1/material/${id}`, 'GET'); + const resp = await xfetch(`/api/v1/material/${id}`, 'GET') const result: any = {} result.success = resp.success - if (resp.success) { - result.data = (resp.body as Record).data - } + result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error fetching source material detail:', error) @@ -43,12 +39,10 @@ export async function getSourceMaterialDetail(id: number | string) { export async function postSourceMaterial(record: any) { try { - const resp = await xfetch('/api/v1/material', 'POST', record); + const resp = await xfetch('/api/v1/material', 'POST', record) const result: any = {} result.success = resp.success - if (resp.success) { - result.data = (resp.body as Record).data - } + result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error posting source material:', error) @@ -58,12 +52,10 @@ export async function postSourceMaterial(record: any) { export async function putSourceMaterial(id: number | string, record: any) { try { - const resp = await xfetch(`/api/v1/material/${id}`, 'PUT', record); + const resp = await xfetch(`/api/v1/material/${id}`, 'PUT', record) const result: any = {} result.success = resp.success - if (resp.success) { - result.data = (resp.body as Record).data - } + result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error putting source material:', error) @@ -76,9 +68,7 @@ export async function removeSourceMaterial(id: number | string) { const resp = await xfetch(`/api/v1/material/${id}`, 'DELETE') const result: any = {} result.success = resp.success - if (resp.success) { - result.data = (resp.body as Record).data - } + result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error deleting record:', error) diff --git a/app/services/uom.service.ts b/app/services/uom.service.ts index 8ebe3e2a..18564f33 100644 --- a/app/services/uom.service.ts +++ b/app/services/uom.service.ts @@ -16,9 +16,7 @@ export async function getSourceUoms(params: any = null) { const resp = await xfetch(url, 'GET') const result: any = {} result.success = resp.success - if (resp.success) { - result.data = (resp.body as Record).data - } + result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error fetching source uoms:', error)