refactor(division): extract entry form to separate component and improve form handling
- Move form logic from list component to dedicated entry-form component - Implement proper form submission and cancellation handlers - Add type safety with DivisionFormData interface - Improve form validation using vee-validate - Refresh data after successful form submission
This commit is contained in:
No files matched your search
@@ -1,13 +1,18 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { FormErrors } from '~/types/error'
|
import type { FormErrors } from '~/types/error'
|
||||||
import Block from '~/components/pub/custom-ui/form/block.vue'
|
import { toTypedSchema } from '@vee-validate/zod'
|
||||||
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
|
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
|
||||||
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
|
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
|
||||||
import Field from '~/components/pub/custom-ui/form/field.vue'
|
import Field from '~/components/pub/custom-ui/form/field.vue'
|
||||||
import Label from '~/components/pub/custom-ui/form/label.vue'
|
import Label from '~/components/pub/custom-ui/form/label.vue'
|
||||||
|
|
||||||
|
interface DivisionFormData {
|
||||||
|
name: string
|
||||||
|
code: string
|
||||||
|
parentId: string
|
||||||
|
}
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
modelValue: any
|
|
||||||
division: {
|
division: {
|
||||||
msg: {
|
msg: {
|
||||||
placeholder: string
|
placeholder: string
|
||||||
@@ -20,47 +25,95 @@ const props = defineProps<{
|
|||||||
code: string
|
code: string
|
||||||
}[]
|
}[]
|
||||||
}
|
}
|
||||||
|
schema: any
|
||||||
|
initialValues?: Partial<DivisionFormData>
|
||||||
errors?: FormErrors
|
errors?: FormErrors
|
||||||
}>()
|
}>()
|
||||||
const emit = defineEmits(['update:modelValue', 'event'])
|
|
||||||
|
|
||||||
const data = computed({
|
const emit = defineEmits<{
|
||||||
get: () => props.modelValue,
|
'submit': [values: DivisionFormData, resetForm: () => void]
|
||||||
set: (val) => emit('update:modelValue', val),
|
'cancel': [resetForm: () => void]
|
||||||
})
|
}>()
|
||||||
|
|
||||||
|
const formSchema = toTypedSchema(props.schema)
|
||||||
|
|
||||||
|
// Form submission handler
|
||||||
|
function onSubmitForm(values: any, { resetForm }: { resetForm: () => void }) {
|
||||||
|
const formData: DivisionFormData = {
|
||||||
|
name: values.name || '',
|
||||||
|
code: values.code || '',
|
||||||
|
parentId: values.parentId || '',
|
||||||
|
}
|
||||||
|
emit('submit', formData, resetForm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Form cancel handler
|
||||||
|
function onCancelForm({ resetForm }: { resetForm: () => void }) {
|
||||||
|
emit('cancel', resetForm)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<form id="entry-form">
|
<Form v-slot="{ handleSubmit, resetForm }" as="" keep-values :validation-schema="formSchema"
|
||||||
<div class="mb-5 border-b border-b-slate-300 pb-3 text-lg xl:text-xl">
|
:initial-values="initialValues">
|
||||||
<div class="flex flex-col justify-between">
|
<form id="entry-form" @submit="handleSubmit($event, (values) => onSubmitForm(values, { resetForm }))">
|
||||||
<Block>
|
<div class="mb-5 border-b border-b-slate-300 pb-3 text-lg xl:text-xl">
|
||||||
<FieldGroup :column="2">
|
<div class="flex flex-col justify-between">
|
||||||
|
<FieldGroup>
|
||||||
<Label>Nama</Label>
|
<Label>Nama</Label>
|
||||||
<Field id="name" :errors="errors">
|
<Field id="name" :errors="errors">
|
||||||
<Input v-model="data.name" />
|
<FormField v-slot="{ componentField }" name="name">
|
||||||
|
<FormItem>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="text" placeholder="Masukkan nama divisi" autocomplete="organization"
|
||||||
|
v-bind="componentField" />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
</FormField>
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
<FieldGroup :column="2">
|
|
||||||
|
<FieldGroup>
|
||||||
<Label>Kode</Label>
|
<Label>Kode</Label>
|
||||||
<Field id="code" :errors="errors">
|
<Field id="code" :errors="errors">
|
||||||
<Input v-model="data.code" />
|
<FormField v-slot="{ componentField }" name="code">
|
||||||
|
<FormItem>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="text" placeholder="Masukkan kode divisi" autocomplete="off" v-bind="componentField" />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
</FormField>
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
|
|
||||||
<FieldGroup :column="2">
|
<FieldGroup :column="2">
|
||||||
<Label>Divisi</Label>
|
<Label>Kelompok</Label>
|
||||||
<Field id="parentId" :errors="errors">
|
<Field id="parentId" :errors="errors">
|
||||||
<Combobox
|
<FormField v-slot="{ componentField }" name="parentId">
|
||||||
v-model="data.parentId"
|
<FormItem>
|
||||||
:items="props.division.items"
|
<FormControl>
|
||||||
:placeholder="props.division.msg.placeholder"
|
<Combobox id="parentId" v-bind="componentField" :items="props.division.items"
|
||||||
:search-placeholder="props.division.msg.search"
|
:placeholder="props.division.msg.placeholder" :search-placeholder="props.division.msg.search"
|
||||||
:empty-message="props.division.msg.empty"
|
:empty-message="props.division.msg.empty" />
|
||||||
/>
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
</FormField>
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
</Block>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</form>
|
<div class="flex justify-end gap-2 mt-4">
|
||||||
|
<Button type="button" variant="outline" @click="onCancelForm({ resetForm })">
|
||||||
|
Batal
|
||||||
|
</Button>
|
||||||
|
<Button type="submit">
|
||||||
|
Simpan
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
</template>
|
</template>
|
||||||
@@ -2,9 +2,8 @@
|
|||||||
import type { DataTableLoader } from '~/components/pub/base/data-table/type'
|
import type { DataTableLoader } from '~/components/pub/base/data-table/type'
|
||||||
import type { HeaderPrep } from '~/components/pub/custom-ui/data/types'
|
import type { HeaderPrep } from '~/components/pub/custom-ui/data/types'
|
||||||
import type { PaginationMeta } from '~/components/pub/custom-ui/pagination/pagination.type'
|
import type { PaginationMeta } from '~/components/pub/custom-ui/pagination/pagination.type'
|
||||||
import { toTypedSchema } from '@vee-validate/zod'
|
|
||||||
import { refDebounced, useUrlSearchParams } from '@vueuse/core'
|
import { refDebounced, useUrlSearchParams } from '@vueuse/core'
|
||||||
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
|
import AppDivisonEntryForm from '~/components/app/divison/entry-form.vue'
|
||||||
import Header from '~/components/pub/custom-ui/nav-header/header.vue'
|
import Header from '~/components/pub/custom-ui/nav-header/header.vue'
|
||||||
import { division as divisionConf, schema as schemaConf } from './entry'
|
import { division as divisionConf, schema as schemaConf } from './entry'
|
||||||
import { defaultQuery, querySchema } from './schema.query'
|
import { defaultQuery, querySchema } from './schema.query'
|
||||||
@@ -73,8 +72,6 @@ const headerPrep: HeaderPrep = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const formSchema = toTypedSchema(schemaConf)
|
|
||||||
|
|
||||||
provide('rec_id', recId)
|
provide('rec_id', recId)
|
||||||
provide('rec_action', recAction)
|
provide('rec_action', recAction)
|
||||||
provide('rec_item', recItem)
|
provide('rec_item', recItem)
|
||||||
@@ -141,25 +138,16 @@ function handlePageChange(page: number) {
|
|||||||
|
|
||||||
// #endregion region
|
// #endregion region
|
||||||
|
|
||||||
// #region Utilities & event handlers
|
// #region Form event handlers
|
||||||
|
|
||||||
function clearForm(setValues: (values: Record<string, any>) => void) {
|
function onCancelForm(resetForm: () => void) {
|
||||||
// Manually clear all form fields
|
|
||||||
setValues({
|
|
||||||
name: '',
|
|
||||||
code: '',
|
|
||||||
parentId: '',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function onCancelForm(setValues: (values: Record<string, any>) => void) {
|
|
||||||
isDialogOpen.value = false
|
isDialogOpen.value = false
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
clearForm(setValues)
|
resetForm()
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onSubmitForm(values: any, setValues: (values: Record<string, any>) => void) {
|
async function onSubmitForm(values: any, resetForm: () => void) {
|
||||||
let isSuccess = false
|
let isSuccess = false
|
||||||
try {
|
try {
|
||||||
// TODO: Implement form submission logic
|
// TODO: Implement form submission logic
|
||||||
@@ -175,6 +163,9 @@ async function onSubmitForm(values: any, setValues: (values: Record<string, any>
|
|||||||
isDialogOpen.value = false
|
isDialogOpen.value = false
|
||||||
isSuccess = true
|
isSuccess = true
|
||||||
|
|
||||||
|
// Refresh data after successful submission
|
||||||
|
await getDivisionList()
|
||||||
|
|
||||||
// TODO: Show success message
|
// TODO: Show success message
|
||||||
console.log('Division created successfully')
|
console.log('Division created successfully')
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
@@ -185,7 +176,7 @@ async function onSubmitForm(values: any, setValues: (values: Record<string, any>
|
|||||||
} finally {
|
} finally {
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
clearForm(setValues)
|
resetForm()
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,70 +212,30 @@ watch(debouncedSearch, (newValue) => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="rounded-md border p-4">
|
<div class="rounded-md border p-4">
|
||||||
<Header v-model="searchInput" :prep="headerPrep" @search="handleSearch" />
|
<Header v-model="searchInput" :prep="headerPrep" @search="handleSearch" />
|
||||||
<AppDivisonList :data="data" :pagination-meta="paginationMeta" @page-change="handlePageChange" />
|
<AppDivisonList :data="data" :pagination-meta="paginationMeta" @page-change="handlePageChange" />
|
||||||
<Form v-slot="{ handleSubmit, setValues }" as="" keep-values :validation-schema="formSchema">
|
|
||||||
<Dialog v-model:open="isDialogOpen">
|
|
||||||
<DialogContent
|
|
||||||
class="sm:max-w-[425px]" @interact-outside="(e) => e.preventDefault()"
|
|
||||||
@pointer-down-outside="(e) => e.preventDefault()"
|
|
||||||
>
|
|
||||||
<DialogHeader>
|
|
||||||
<DialogTitle>Tambah Divisi</DialogTitle>
|
|
||||||
<DialogDescription></DialogDescription>
|
|
||||||
</DialogHeader>
|
|
||||||
|
|
||||||
<form id="dialogForm" @submit="handleSubmit($event, (values) => onSubmitForm(values, setValues))">
|
<Dialog v-model:open="isDialogOpen">
|
||||||
<FormField v-slot="{ componentField }" name="name">
|
<DialogContent
|
||||||
<FormItem>
|
class="sm:max-w-[425px]"
|
||||||
<FormLabel>Nama</FormLabel>
|
@interact-outside="(e) => e.preventDefault()"
|
||||||
<FormControl>
|
@pointer-down-outside="(e) => e.preventDefault()"
|
||||||
<Input
|
>
|
||||||
type="text" placeholder="Masukkan nama divisi" autocomplete="organization"
|
<DialogHeader>
|
||||||
v-bind="componentField"
|
<DialogTitle>Tambah Divisi</DialogTitle>
|
||||||
/>
|
<DialogDescription></DialogDescription>
|
||||||
</FormControl>
|
</DialogHeader>
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
</FormField>
|
|
||||||
|
|
||||||
<FormField v-slot="{ componentField }" name="code">
|
<AppDivisonEntryForm
|
||||||
<FormItem>
|
:division="divisionConf"
|
||||||
<FormLabel>Kode</FormLabel>
|
:schema="schemaConf"
|
||||||
<FormControl>
|
:initial-values="{ name: '', code: '', parentId: '' }"
|
||||||
<Input type="text" placeholder="Masukkan kode divisi" autocomplete="off" v-bind="componentField" />
|
@submit="onSubmitForm"
|
||||||
</FormControl>
|
@cancel="onCancelForm"
|
||||||
<FormMessage />
|
/>
|
||||||
</FormItem>
|
</DialogContent>
|
||||||
</FormField>
|
</Dialog>
|
||||||
|
|
||||||
<FormField v-slot="{ componentField }" name="parentId">
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Kelompok</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Combobox
|
|
||||||
v-bind="componentField" :items="divisionConf.items"
|
|
||||||
:placeholder="divisionConf.msg.placeholder" :search-placeholder="divisionConf.msg.search"
|
|
||||||
:empty-message="divisionConf.msg.empty"
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
</FormField>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<DialogFooter>
|
|
||||||
<Button variant="outline" @click="onCancelForm(setValues)">
|
|
||||||
Batal
|
|
||||||
</Button>
|
|
||||||
<Button type="submit" form="dialogForm">
|
|
||||||
Simpan
|
|
||||||
</Button>
|
|
||||||
</DialogFooter>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
</Form>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user