Files
hats-app/app/admin/administration/company/component/Form.tsx
T
2025-12-19 13:57:55 +07:00

143 lines
5.2 KiB
TypeScript

import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"
import { useSession } from "next-auth/react"
import { Form, Input } from "antd"
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 { type ICompany } from "./type"
import { toast } from "sonner"
type Props = {
isUpdate?: boolean
data?: ICompany
onCancel?: () => any
}
export default function CompanyForm({ data, isUpdate, onCancel }: Props) {
const [isLoading, setIsLoading] = useState(false)
const BASE_URL = process.env.BASE_URL_API
const [form] = Form.useForm();
const { data: session } = useSession()
const router = useRouter()
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: ICompany) => {
try {
setIsLoading(true)
const res = await fetch(BASE_URL + '/company', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session?.user.access_token}`,
},
body: JSON.stringify(payload)
})
const data = await res.json()
setIsLoading(false)
if (!res.ok) {
toast.error(`Data failed to create`, { description: `(${data.code || res.status}) ${data.message || res.statusText}` })
return
}
toast.success('Data successfully created.')
router.refresh()
onCancel!()
} catch (error: any) {
toast.error(`Data failed to create, ${error.message}`)
setIsLoading(false)
}
}
const handleUpdate = async (payload: ICompany) => {
try {
setIsLoading(true)
const res = await fetch(BASE_URL + '/company/' + data?.id, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session?.user.access_token}`,
},
body: JSON.stringify(payload)
})
const resData = await res.json()
setIsLoading(false)
if (!res.ok) {
toast.error(`Data failed to update`, { description: `(${resData.code || res.status}) ${resData.message || res.statusText}` })
return
}
toast.success('Data successfully updated.')
router.refresh()
onCancel!()
} catch (error: any) {
toast.error(`Data failed to update`, { description: `${error.message}` })
setIsLoading(false)
}
}
return (
<DialogContent>
<Form form={form} onFinish={handleSubmit}>
<DialogHeader>
<DialogTitle className="dark:text-white">{isUpdate ? 'Update company' : 'Create company'}</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="code">Code</Label>
<Form.Item name='code' rules={[{ required: true, message: 'Please input company code!' }]} >
<Input className="input" id="code" />
</Form.Item>
</div> */}
<div className="grid gap-2">
<Label htmlFor="name">Company name</Label>
<Form.Item name='name' rules={[{ required: true, message: 'Please input company name!' }]} >
<Input className="input" id="name" placeholder="PT KCI" />
</Form.Item>
</div>
<div className="grid gap-2">
<Label htmlFor="description">Description</Label>
<Form.Item name='description' >
<Input.TextArea className="input" id="description" placeholder="PT Kereta Commuter 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>
)
}