mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
155 lines
6.5 KiB
TypeScript
155 lines
6.5 KiB
TypeScript
import { useRouter } from "next/navigation"
|
|
import { useEffect, useState } from "react"
|
|
import { Form, Input, Radio, Select } from "antd"
|
|
import useAxiosAuth from "@/lib/axios/useAxiosAuth"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog"
|
|
import { Icons } from "@/components/Icons"
|
|
import { type IMenu } from "./type"
|
|
import { toast } from "sonner"
|
|
|
|
type Props = {
|
|
isUpdate?: boolean
|
|
data?: IMenu
|
|
menuList: IMenu[]
|
|
onCancel?: () => any
|
|
}
|
|
|
|
export default function ComponentForm({ data, menuList, isUpdate, onCancel }: Props) {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [form] = Form.useForm();
|
|
const router = useRouter()
|
|
const axios = useAxiosAuth()
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.setFieldsValue(data)
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [data, form])
|
|
|
|
const handleSubmit = (value: any) => {
|
|
console.log(value)
|
|
|
|
!isUpdate ? handleCreate(value) : handleUpdate(value)
|
|
}
|
|
|
|
const handleCreate = async (payload: IMenu) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.post('/menu', payload)
|
|
|
|
handleSuccess('Data successfully created.')
|
|
} catch (error: any) {
|
|
console.log(error)
|
|
toast.error(`Data failed to create`, { description: `(${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}` })
|
|
}
|
|
}
|
|
|
|
const handleUpdate = async (payload: IMenu) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.put('/menu/' + data?.id, payload)
|
|
|
|
handleSuccess('Data successfully updated.')
|
|
} catch (error: any) {
|
|
toast.error(`Data failed to update`, { description: `(${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}` })
|
|
}
|
|
}
|
|
|
|
const handleSuccess = (message: string) => {
|
|
toast.success(message)
|
|
setIsLoading(false)
|
|
onCancel!()
|
|
router.refresh()
|
|
form.resetFields()
|
|
}
|
|
|
|
const handleError = (message: string) => {
|
|
toast.error(message)
|
|
setIsLoading(false)
|
|
}
|
|
|
|
return (
|
|
<DialogContent>
|
|
<Form form={form} onFinish={handleSubmit} layout='vertical'>
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">{isUpdate ? 'Update menu' : 'Create menu'}</DialogTitle>
|
|
<DialogDescription>
|
|
Add a new team to manage products and customers
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid mt-8">
|
|
<Form.Item name='name' label='Menu name' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input className="input" id="name" placeholder="Dashboard" />
|
|
</Form.Item>
|
|
<div className="grid grid-cols-3 gap-x-4">
|
|
<Form.Item name='menu_parent_id' label='Parent Menu' className="col-span-2" >
|
|
<Select placeholder='Select parent' showSearch allowClear
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
>
|
|
<Select.Option value=''>-</Select.Option>
|
|
{menuList.map((item =>
|
|
<Select.Option key={item.id} value={item.id}>({item.id}) {item.name}</Select.Option>
|
|
))}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item name='icon' label='Icon' >
|
|
<Select placeholder='Select icon' showSearch allowClear
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
>
|
|
<Select.Option value=''>-</Select.Option>
|
|
<Select.Option value='ti ti-layout-dashboard'>
|
|
<i className="ti ti-layout-dashboard icon"></i>
|
|
</Select.Option>
|
|
|
|
<Select.Option value='ti ti-database'>
|
|
<i className="ti ti-database icon"></i>
|
|
</Select.Option>
|
|
|
|
<Select.Option value='ti ti-devices-2'>
|
|
<i className="ti ti-devices-2 icon"></i>
|
|
</Select.Option>
|
|
</Select>
|
|
</Form.Item>
|
|
</div>
|
|
<Form.Item name='path_url' label='Path URL' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input className="input" id="path_url" placeholder="dashboard" />
|
|
</Form.Item>
|
|
<Form.Item name='hidden_data' label='Hidden Data' >
|
|
<Radio.Group>
|
|
<Radio value={true}>Hidden</Radio>
|
|
<Radio value={false}>Visible</Radio>
|
|
</Radio.Group>
|
|
</Form.Item>
|
|
<Form.Item name='description' label='Description'>
|
|
<Input.TextArea className="input" id="description" placeholder="Halaman dashboard" />
|
|
</Form.Item>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" type="button" onClick={onCancel} disabled={isLoading} >
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
)
|
|
} |