feat: Implement item management with CRUD operations, forms, and listing pages.

This commit is contained in:
riefive
2025-12-05 13:54:53 +07:00
parent 864979aa15
commit 615a7c4485
9 changed files with 499 additions and 109 deletions
+18 -9
View File
@@ -16,12 +16,15 @@ import { toTypedSchema } from '@vee-validate/zod'
interface Props {
schema: z.ZodSchema<any>
itemGroups: any[]
uoms: any[]
values: any
isLoading?: boolean
isReadonly?: boolean
}
const props = defineProps<Props>()
const isShowInfra = false;
const isLoading = props.isLoading !== undefined ? props.isLoading : false
const isReadonly = props.isReadonly !== undefined ? props.isReadonly : false
const emit = defineEmits<{
@@ -44,7 +47,7 @@ const { defineField, errors, meta } = useForm({
const [code, codeAttrs] = defineField('code')
const [name, nameAttrs] = defineField('name')
const [itemGroup_code, itemGroup_codeAttrs] = defineField('itemGroup_code')
const [uom_code, uom_codeAttrs] = defineField('uom_code')
const [uom, uomAttrs] = defineField('uom_code')
const [infra_code, infra_codeAttrs] = defineField('infra_code')
const [stock, stockAttrs] = defineField('stock')
@@ -52,7 +55,7 @@ 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.itemGroup_code !== undefined) itemGroup_code.value = props.values.itemGroup_code
if (props.values.uom_code !== undefined) uom_code.value = props.values.uom_code
if (props.values.uom_code !== undefined) uom.value = props.values.uom_code
if (props.values.infra_code !== undefined) infra_code.value = props.values.infra_code
if (props.values.stock !== undefined) stock.value = props.values.stock
}
@@ -61,7 +64,7 @@ const resetForm = () => {
code.value = ''
name.value = ''
itemGroup_code.value = ''
uom_code.value = ''
uom.value = ''
infra_code.value = ''
stock.value = 0
}
@@ -71,7 +74,7 @@ function onSubmitForm() {
code: code.value || '',
name: name.value || '',
itemGroup_code: itemGroup_code.value || '',
uom_code: uom_code.value || '',
uom_code: uom.value || '',
infra_code: infra_code.value || '',
stock: Number(stock.value) || 0,
}
@@ -118,10 +121,13 @@ function onCancelForm() {
<Cell>
<Label height="compact">Item Group</Label>
<Field :errMessage="errors.itemGroup_code">
<Input
<Select
id="itemGroup_code"
v-model="itemGroup_code"
icon-name="i-lucide-chevron-down"
placeholder="Pilih Item Group"
v-bind="itemGroup_codeAttrs"
:items="itemGroups"
:disabled="isLoading || isReadonly"
/>
</Field>
@@ -129,15 +135,18 @@ function onCancelForm() {
<Cell>
<Label height="compact">UOM</Label>
<Field :errMessage="errors.uom_code">
<Input
<Select
id="uom_code"
v-model="uom_code"
v-bind="uom_codeAttrs"
v-model="uom"
icon-name="i-lucide-chevron-down"
placeholder="Pilih satuan"
v-bind="uomAttrs"
:items="uoms"
:disabled="isLoading || isReadonly"
/>
</Field>
</Cell>
<Cell>
<Cell v-if="isShowInfra">
<Label height="compact">Infra</Label>
<Field :errMessage="errors.infra_code">
<Input
+18 -1
View File
@@ -25,7 +25,24 @@ export const config: Config = {
{ key: 'name', label: 'Nama' },
],
parses: {},
parses: {
itemGroup_code: (rec: unknown): unknown => {
const recX = rec as any
return recX.itemGroup_code || '-'
},
uom_code: (rec: unknown): unknown => {
const recX = rec as any
return recX.uom?.name || '-'
},
infra_code: (rec: unknown): unknown => {
const recX = rec as any
return recX.infra_code || '-'
},
stock: (rec: unknown): unknown => {
const recX = rec as any
return recX.stock || '-'
},
},
components: {
action(rec, idx) {
+172 -48
View File
@@ -1,70 +1,194 @@
<script setup lang="ts">
import type { DataTableLoader } from '~/components/pub/my-ui/data-table/type'
import type { HeaderPrep, RefSearchNav } from '~/components/pub/my-ui/data/types'
import Modal from '~/components/pub/my-ui/modal/modal.vue'
// Components
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import Header from '~/components/pub/my-ui/nav-header/prep.vue'
import AppItemList from '~/components/app/item-price/list.vue'
import AppItemEntryForm from '~/components/app/item-price/entry-form.vue'
import RecordConfirmation from '~/components/pub/my-ui/confirmation/record-confirmation.vue'
const data = ref([])
const entry = ref<any>({})
// Constants
import { infraGroupCodesKeys } from '~/lib/constants'
const refSearchNav: RefSearchNav = {
onClick: () => {
// open filter modal
},
onInput: (_val: string) => {
// filter patient list
},
onClear: () => {
// clear url param
},
}
// Helpers
import { usePaginatedList } from '~/composables/usePaginatedList'
import { toast } from '~/components/pub/ui/toast'
// Loading state management
const isLoading = reactive<DataTableLoader>({
summary: false,
isTableLoading: false,
// Types
import { ActionEvents, type HeaderPrep } from '~/components/pub/my-ui/data/types'
import { ItemPriceSchema, type ItemPriceFormData } from '~/schemas/item-price.schema'
// Handlers
import {
recId,
recAction,
recItem,
isReadonly,
isProcessing,
isFormEntryDialogOpen,
isRecordConfirmationOpen,
onResetState,
handleActionSave,
handleActionEdit,
handleActionRemove,
handleCancelForm,
} from '~/handlers/item-price.handler'
// Services
import { getList, getDetail } from '~/services/item-price.service'
const title = ref('')
const {
data,
isLoading,
paginationMeta,
searchInput,
handlePageChange,
handleSearch,
fetchData: getItemList,
} = usePaginatedList({
fetchFn: async (params: any) => {
const result = await getList({
search: params.search,
sort: 'createdAt:asc',
'page-number': params['page-number'] || 0,
'page-size': params['page-size'] || 10,
})
return { success: result.success || false, body: result.body || {} }
},
entityName: 'item-price',
})
const isOpen = ref(false)
const recId = ref<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(null)
const hreaderPrep: HeaderPrep = {
title: 'Golongan Obat',
icon: 'i-lucide-users',
const headerPrep: HeaderPrep = {
title: 'Harga Item',
icon: 'i-lucide-layout-list',
refSearchNav: {
placeholder: 'Cari (min. 3 karakter)...',
minLength: 3,
debounceMs: 500,
showValidationFeedback: true,
onInput: (val: string) => {
searchInput.value = val
},
onClick: () => {},
onClear: () => {},
},
addNav: {
label: 'Tambah',
onClick: () => (isOpen.value = true),
icon: 'i-lucide-plus',
onClick: () => {
recItem.value = null
recId.value = 0
isFormEntryDialogOpen.value = true
isReadonly.value = false
},
},
}
async function getPatientList() {
isLoading.isTableLoading = true
const resp = await xfetch('/api/v1/medicine-group')
if (resp.success) {
data.value = (resp.body as Record<string, any>).data
}
isLoading.isTableLoading = false
}
onMounted(() => {
getPatientList()
})
provide('rec_id', recId)
provide('rec_action', recAction)
provide('rec_item', recItem)
provide('table_data_loader', isLoading)
const getCurrentDetail = async (id: number | string) => {
const result = await getDetail(id)
if (result.success) {
const currentValue = result.body?.data || {}
recItem.value = currentValue
isFormEntryDialogOpen.value = true
}
}
watch([recId, recAction], () => {
const currentId = recId.value
switch (recAction.value) {
case ActionEvents.showDetail:
getCurrentDetail(currentId)
title.value = 'Detail Harga Item'
isReadonly.value = true
break
case ActionEvents.showEdit:
getCurrentDetail(currentId)
title.value = 'Edit Harga Item'
isReadonly.value = false
break
case ActionEvents.showConfirmDelete:
isRecordConfirmationOpen.value = true
break
}
})
onMounted(async () => {
await getItemList()
})
</script>
<template>
<Header :prep="{ ...hreaderPrep }" :ref-search-nav="refSearchNav" />
<Header
v-model="searchInput"
:prep="headerPrep"
:ref-search-nav="headerPrep.refSearchNav"
@search="handleSearch"
/>
<AppMedicineGroupList :data="data" />
<AppItemList
:data="data"
:pagination-meta="paginationMeta"
@page-change="handlePageChange"
/>
<Modal v-model:open="isOpen" title="Tambah Golongan Obat" size="lg" prevent-outside>
<AppMedicineGroupEntryForm v-model="entry" />
</Modal>
<Dialog
v-model:open="isFormEntryDialogOpen"
:title="!!recItem ? title : 'Tambah Harga Item'"
size="lg"
prevent-outside
@update:open="
(value: any) => {
onResetState()
isFormEntryDialogOpen = value
}
"
>
<AppItemEntryForm
:schema="ItemPriceSchema"
:values="recItem"
:is-loading="isProcessing"
:is-readonly="isReadonly"
@submit="
(values: ItemPriceFormData | Record<string, any>, resetForm: () => void) => {
if (recId > 0) {
handleActionEdit(recId, values, getItemList, resetForm, toast)
return
}
handleActionSave(values, getItemList, resetForm, toast)
}
"
@cancel="handleCancelForm"
/>
</Dialog>
<RecordConfirmation
v-model:open="isRecordConfirmationOpen"
action="delete"
:record="recItem"
@confirm="() => handleActionRemove(recId, getItemList, toast)"
@cancel=""
>
<template #default="{ record }">
<div class="text-sm">
<p>
<strong>ID:</strong>
{{ record?.id }}
</p>
<p v-if="record?.name">
<strong>Nama:</strong>
{{ record.name }}
</p>
<p v-if="record?.code">
<strong>Kode:</strong>
{{ record.code }}
</p>
</div>
</template>
</RecordConfirmation>
</template>
+185 -47
View File
@@ -1,70 +1,208 @@
<script setup lang="ts">
import type { DataTableLoader } from '~/components/pub/my-ui/data-table/type'
import type { HeaderPrep, RefSearchNav } from '~/components/pub/my-ui/data/types'
import Modal from '~/components/pub/my-ui/modal/modal.vue'
// Components
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import Header from '~/components/pub/my-ui/nav-header/prep.vue'
import AppItemList from '~/components/app/item/list.vue'
import AppItemEntryForm from '~/components/app/item/entry-form.vue'
import RecordConfirmation from '~/components/pub/my-ui/confirmation/record-confirmation.vue'
const data = ref([])
const entry = ref<any>({})
// Constants
import { infraGroupCodesKeys } from '~/lib/constants'
const refSearchNav: RefSearchNav = {
onClick: () => {
// open filter modal
},
onInput: (_val: string) => {
// filter patient list
},
onClear: () => {
// clear url param
},
}
// Helpers
import { usePaginatedList } from '~/composables/usePaginatedList'
import { toast } from '~/components/pub/ui/toast'
// Loading state management
const isLoading = reactive<DataTableLoader>({
summary: false,
isTableLoading: false,
// Types
import { ActionEvents, type HeaderPrep } from '~/components/pub/my-ui/data/types'
import { ItemSchema, type ItemFormData } from '~/schemas/item.schema'
// Handlers
import {
recId,
recAction,
recItem,
isReadonly,
isProcessing,
isFormEntryDialogOpen,
isRecordConfirmationOpen,
onResetState,
handleActionSave,
handleActionEdit,
handleActionRemove,
handleCancelForm,
} from '~/handlers/item.handler'
// Constants
import { itemGroupCodes } from '~/lib/constants'
// Services
import { getList, getDetail } from '~/services/item.service'
import { getValueLabelList as getUomList } from '~/services/uom.service'
const itemGroups = ref<{ value: string | number; label: string }[]>([])
const uoms = ref<{ value: string | number; label: string }[]>([])
const title = ref('')
const {
data,
isLoading,
paginationMeta,
searchInput,
handlePageChange,
handleSearch,
fetchData: getItemList,
} = usePaginatedList({
fetchFn: async (params: any) => {
const result = await getList({
search: params.search,
sort: 'createdAt:asc',
'page-number': params['page-number'] || 0,
'page-size': params['page-size'] || 10,
includes: 'uom',
})
return { success: result.success || false, body: result.body || {} }
},
entityName: 'item',
})
const isOpen = ref(false)
const recId = ref<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(null)
const hreaderPrep: HeaderPrep = {
const headerPrep: HeaderPrep = {
title: 'Item',
icon: 'i-lucide-users',
icon: 'i-lucide-layout-list',
refSearchNav: {
placeholder: 'Cari (min. 3 karakter)...',
minLength: 3,
debounceMs: 500,
showValidationFeedback: true,
onInput: (val: string) => {
searchInput.value = val
},
onClick: () => {},
onClear: () => {},
},
addNav: {
label: 'Tambah',
onClick: () => (isOpen.value = true),
icon: 'i-lucide-plus',
onClick: () => {
recItem.value = null
recId.value = 0
isFormEntryDialogOpen.value = true
isReadonly.value = false
},
},
}
async function getPatientList() {
isLoading.isTableLoading = true
const resp = await xfetch('/api/v1/medicine-group')
if (resp.success) {
data.value = (resp.body as Record<string, any>).data
}
isLoading.isTableLoading = false
}
onMounted(() => {
getPatientList()
})
provide('rec_id', recId)
provide('rec_action', recAction)
provide('rec_item', recItem)
provide('table_data_loader', isLoading)
const getCurrentDetail = async (id: number | string) => {
const result = await getDetail(id)
if (result.success) {
const currentValue = result.body?.data || {}
recItem.value = currentValue
isFormEntryDialogOpen.value = true
}
}
watch([recId, recAction], () => {
const currentId = recItem.value?.code ? recItem.value.code : recId.value
switch (recAction.value) {
case ActionEvents.showDetail:
getCurrentDetail(currentId)
title.value = 'Detail Item'
isReadonly.value = true
break
case ActionEvents.showEdit:
getCurrentDetail(currentId)
title.value = 'Edit Item'
isReadonly.value = false
break
case ActionEvents.showConfirmDelete:
isRecordConfirmationOpen.value = true
break
}
})
onMounted(async () => {
itemGroups.value = Object.keys(itemGroupCodes).map((key) => ({
value: key,
label: itemGroupCodes[key],
})) as any
uoms.value = await getUomList()
await getItemList()
})
</script>
<template>
<Header :prep="{ ...hreaderPrep }" :ref-search-nav="refSearchNav" />
<Header
v-model="searchInput"
:prep="headerPrep"
:ref-search-nav="headerPrep.refSearchNav"
@search="handleSearch"
/>
<AppItemList :data="data" />
<AppItemList
:data="data"
:pagination-meta="paginationMeta"
@page-change="handlePageChange"
/>
<Modal v-model:open="isOpen" title="Tambah Golongan Obat" size="xl" prevent-outside>
<AppItemEntryForm v-model="entry" />
</Modal>
<Dialog
v-model:open="isFormEntryDialogOpen"
:title="!!recItem ? title : 'Tambah Item'"
size="lg"
prevent-outside
@update:open="
(value: any) => {
onResetState()
isFormEntryDialogOpen = value
}
"
>
<AppItemEntryForm
:schema="ItemSchema"
:values="recItem"
:item-groups="itemGroups"
:uoms="uoms"
:is-loading="isProcessing"
:is-readonly="isReadonly"
@submit="
(values: ItemFormData | Record<string, any>, resetForm: () => void) => {
if (recId > 0) {
handleActionEdit(recId, values, getItemList, resetForm, toast)
return
}
handleActionSave(values, getItemList, resetForm, toast)
}
"
@cancel="handleCancelForm"
/>
</Dialog>
<RecordConfirmation
v-model:open="isRecordConfirmationOpen"
action="delete"
:record="recItem"
@confirm="() => handleActionRemove(recItem?.code ? recItem.code : recId, getItemList, toast)"
@cancel=""
>
<template #default="{ record }">
<div class="text-sm">
<p>
<strong>ID:</strong>
{{ record?.id }}
</p>
<p v-if="record?.name">
<strong>Nama:</strong>
{{ record.name }}
</p>
<p v-if="record?.code">
<strong>Kode:</strong>
{{ record.code }}
</p>
</div>
</template>
</RecordConfirmation>
</template>
+29
View File
@@ -0,0 +1,29 @@
<script setup lang="ts">
import Error from '~/components/pub/my-ui/error/error.vue'
import Content from '~/components/content/item-price/list.vue'
const route = useRoute()
definePageMeta({
middleware: ['rbac'],
roles: ['emp|reg', 'emp|nur', 'emp|doc', 'emp|miw', 'emp|thr', 'emp|nut', 'emp|pha', 'emp|lab'],
title: 'Daftar Item Harga',
contentFrame: 'cf-container-lg',
})
useHead({
title: () => route.meta.title as string,
})
const canRead = true
</script>
<template>
<template v-if="canRead">
<Content />
</template>
<Error
v-else
:status-code="403"
/>
</template>
+29
View File
@@ -0,0 +1,29 @@
<script setup lang="ts">
import Error from '~/components/pub/my-ui/error/error.vue'
import Content from '~/components/content/item/list.vue'
const route = useRoute()
definePageMeta({
middleware: ['rbac'],
roles: ['emp|reg', 'emp|nur', 'emp|doc', 'emp|miw', 'emp|thr', 'emp|nut', 'emp|pha', 'emp|lab'],
title: 'Daftar Item',
contentFrame: 'cf-container-lg',
})
useHead({
title: () => route.meta.title as string,
})
const canRead = true
</script>
<template>
<template v-if="canRead">
<Content />
</template>
<Error
v-else
:status-code="403"
/>
</template>
+2 -2
View File
@@ -5,8 +5,8 @@ const ItemSchema = z.object({
name: z.string({ required_error: 'Nama harus diisi' }).min(1, 'Nama minimum 1 karakter'),
itemGroup_code: z.string({ required_error: 'Item Group harus diisi' }).min(1, 'Item Group harus diisi'),
uom_code: z.string({ required_error: 'UOM harus diisi' }).min(1, 'UOM harus diisi'),
infra_code: z.string({ required_error: 'Infra harus diisi' }).min(1, 'Infra harus diisi'),
stock: z.number({ required_error: 'Stok harus diisi' }).min(0, 'Stok tidak boleh kurang dari 0'),
infra_code: z.string({ required_error: 'Infra harus diisi' }).optional(),
stock: z.number({ required_error: 'Stok harus diisi' }).min(0, 'Stok tidak boleh kurang dari 0').optional(),
})
type ItemFormData = z.infer<typeof ItemSchema>
+41
View File
@@ -0,0 +1,41 @@
// Base
import * as base from './_crud-base'
const path = '/api/v1/item-group'
const name = 'item-group'
export function create(data: any) {
return base.create(path, data, name)
}
export function getList(params: any = null) {
return base.getList(path, params, name)
}
export function getDetail(id: number | string) {
return base.getDetail(path, id, name)
}
export function update(id: number | string, data: any) {
return base.update(path, id, data, name)
}
export function remove(id: number | string) {
return base.remove(path, id, name)
}
export async function getValueLabelList(
params: any = null,
useCodeAsValue = false,
): Promise<{ value: string; label: string }[]> {
let data: { value: string; label: string }[] = []
const result = await getList(params)
if (result.success) {
const resultData = result.body?.data || []
data = resultData.map((item: any) => ({
value: useCodeAsValue ? item.code : item.id ? Number(item.id) : item.code,
label: item.name,
}))
}
return data
}
+5 -2
View File
@@ -24,13 +24,16 @@ export function remove(id: number | string) {
return base.remove(path, id, name)
}
export async function getValueLabelList(params: any = null): Promise<{ value: string; label: string }[]> {
export async function getValueLabelList(
params: any = null,
useCodeAsValue = false,
): Promise<{ value: string; label: string }[]> {
let data: { value: string; label: string }[] = []
const result = await getList(params)
if (result.success) {
const resultData = result.body?.data || []
data = resultData.map((item: any) => ({
value: item.id ? Number(item.id) : item.code,
value: useCodeAsValue ? item.code : item.id ? Number(item.id) : item.code,
label: item.name,
}))
}