mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
156 lines
6.5 KiB
TypeScript
156 lines
6.5 KiB
TypeScript
import { useRouter } from "next/navigation"
|
|
import { useEffect, useState } from "react"
|
|
import { Form, Input, 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 { Label } from "@/components/ui/label"
|
|
import { Icons } from "@/components/Icons"
|
|
import useFetch from "@/hooks/useFetch"
|
|
import { type IArea } from "./type"
|
|
import { type ICompany } from "../../company/component/type"
|
|
import { toast } from "sonner"
|
|
|
|
type Props = {
|
|
isUpdate?: boolean
|
|
data?: IArea
|
|
onCancel?: () => any
|
|
}
|
|
|
|
export default function AreaForm({ data, isUpdate, onCancel }: Props) {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [form] = Form.useForm();
|
|
const router = useRouter()
|
|
const axios = useAxiosAuth()
|
|
|
|
const { data: companyList, isLoading: loadingCompany, error } = useFetch('/company')
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.setFieldsValue(data)
|
|
if (data.company) form.setFieldValue('company_id', data.company.id)
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [data, form])
|
|
|
|
const handleSubmit = (value: any) => {
|
|
console.log(value)
|
|
|
|
!isUpdate ? handleCreate(value) : handleUpdate(value)
|
|
}
|
|
|
|
const handleCreate = async (payload: IArea) => {
|
|
try {
|
|
if (payload.latitude) payload['latitude'] = Number(payload.latitude)
|
|
if (payload.longitude) payload['longitude'] = Number(payload.longitude)
|
|
|
|
setIsLoading(true)
|
|
await axios.post('/area', payload)
|
|
|
|
handleSuccess('Data successfully created.')
|
|
} catch (error: any) {
|
|
handleError(`Data failed to create, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
}
|
|
}
|
|
|
|
const handleUpdate = async (payload: IArea) => {
|
|
try {
|
|
if (payload.latitude) payload['latitude'] = Number(payload.latitude)
|
|
if (payload.longitude) payload['longitude'] = Number(payload.longitude)
|
|
|
|
setIsLoading(true)
|
|
await axios.put('/area/' + data?.id, payload)
|
|
|
|
handleSuccess('Data successfully updated.')
|
|
} catch (error: any) {
|
|
handleError(`Data failed to update, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
}
|
|
}
|
|
|
|
const handleSuccess = (message: string) => {
|
|
toast.info(message)
|
|
setIsLoading(false)
|
|
onCancel!()
|
|
router.refresh()
|
|
form.resetFields()
|
|
}
|
|
|
|
const handleError = (message: string) => {
|
|
toast.error(message)
|
|
setIsLoading(false)
|
|
}
|
|
|
|
return (
|
|
<DialogContent>
|
|
<Form form={form} onFinish={handleSubmit}>
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">{isUpdate ? 'Update area' : 'Create area'}</DialogTitle>
|
|
<DialogDescription>
|
|
Add a new team to manage products and customers
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid mt-8">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="name">Area name</Label>
|
|
<Form.Item name='name' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Input className="input" id="name" placeholder="RESEARCH AND DEVELOPMENT" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="company_id">Company</Label>
|
|
<Form.Item name='company_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select company' showSearch allowClear loading={loadingCompany}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
>
|
|
{companyList?.company?.length > 0 && companyList.company.map((item: ICompany, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.code} - {item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-x-4">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="latitude">latitude</Label>
|
|
<Form.Item name='latitude' >
|
|
<Input className="input" id="latitude" placeholder="-6.200000" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="longitude">longitude</Label>
|
|
<Form.Item name='longitude' >
|
|
<Input className="input" id="longitude" placeholder="106.816666" />
|
|
</Form.Item>
|
|
</div>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="description">Description</Label>
|
|
<Form.Item name='description' >
|
|
<Input.TextArea className="input" id="description" placeholder="R&D Department PT Nutech Integrasi by Telkom Indonesia" />
|
|
</Form.Item>
|
|
</div>
|
|
</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>
|
|
)
|
|
} |