Merge branch 'dev' into refactor/mv-flow-to-content

This commit is contained in:
Khafid Prayoga
2025-09-08 13:41:33 +07:00
9 changed files with 171 additions and 125 deletions
+23 -53
View File
@@ -1,64 +1,34 @@
<script setup lang="ts">
import { z, ZodError } from 'zod'
import Action from '~/components/pub/custom-ui/nav-footer/ba-dr-su.vue'
// types
import type { MaterialFormData } from '~/schemas/material'
import { MaterialSchema } from '~/schemas/material'
const errors = ref({})
const data = ref({
code: '',
name: '',
type: '',
stock: 0,
})
const isLoading = ref(false)
const uoms = [
{ value: 'uom-1', label: 'Satuan 1' },
{ value: 'uom-2', label: 'Satuan 2' },
{ value: 'uom-3', label: 'Satuan 3' },
]
const items = [
{ value: 'item-1', label: 'Item 1' },
{ value: 'item-2', label: 'Item 2' },
{ value: 'item-3', label: 'Item 3' },
]
const schema = z.object({
code: z.string().min(1, 'Code must be at least 1 characters long'),
name: z.string().min(1, 'Name must be at least 1 characters long'),
type: z.string(),
stock: z.preprocess((val) => Number(val), z.number({ invalid_type_error: 'Stok harus berupa angka' })),
})
function onBack() {
navigateTo('/tools-equipment-src/equipment')
}
function onClick(type: string) {
if (type === 'cancel') {
navigateTo('/tools-equipment-src/material')
} else if (type === 'draft') {
// do something
} else if (type === 'submit') {
// do something
const input = data.value
console.log(input)
const errorsParsed: any = {}
try {
const result = schema.safeParse(input)
if (!result.success) {
// You can handle the error here, e.g. show a message
const errorsCaptures = result?.error?.errors || []
const errorMessage = result.error.errors[0]?.message ?? 'Validation error occurred'
errorsCaptures.forEach((value: any) => {
const keyName = value?.path?.length > 0 ? value.path[0] : 'key'
errorsParsed[keyName as string] = value.message || ''
})
console.log(errorMessage)
}
} catch (e) {
if (e instanceof ZodError) {
const jsonError = e.flatten()
console.log(JSON.stringify(jsonError, null, 2))
}
}
setTimeout(() => {
errors.value = errorsParsed
}, 0)
}
async function onSubmit(data: MaterialFormData) {
console.log(data)
}
</script>
<template>
<div class="mb-5 border-b border-b-slate-300 pb-3 text-lg xl:text-xl">
<Icon name="i-lucide-paint-bucket" class="me-2" />
<span class="font-semibold">Tambah</span> Alat Kesehatan
</div>
<AppMaterialEntryForm v-model="data" :errors="errors" />
<div class="my-2 flex justify-end py-2">
<Action @click="onClick" />
<Icon name="i-lucide-panel-bottom" class="me-2" />
<span class="font-semibold">Tambah</span> Perlengkapan (BMHP)
</div>
<AppMaterialEntryForm :is-loading="isLoading" :schema="MaterialSchema" :uoms="uoms" :items="items" @back="onBack"
@submit="onSubmit" />
</template>
+7 -7
View File
@@ -26,21 +26,21 @@ const recAction = ref<string>('')
const recItem = ref<any>(null)
const headerPrep: HeaderPrep = {
title: 'BMHP',
icon: 'i-lucide-paint-bucket',
title: 'Perlengkapan (BMHP)',
icon: 'i-lucide-panel-bottom',
addNav: {
label: 'Tambah',
onClick: () => navigateTo('/tools-equipment-src/material/add'),
onClick: () => navigateTo('/tools-equipment-src/equipment/add'),
},
}
async function getMaterialList() {
isLoading.dataListLoading = true
const resp = await xfetch('/api/v1/material')
if (resp.success) {
data.value = (resp.body as Record<string, any>).data
}
// const resp = await xfetch('/api/v1/material')
// if (resp.success) {
// data.value = (resp.body as Record<string, any>).data
// }
isLoading.dataListLoading = false
}