mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:46:27 +00:00
137 lines
5.1 KiB
TypeScript
137 lines
5.1 KiB
TypeScript
import { useRouter } from "next/navigation"
|
|
import { useEffect, useState } from "react"
|
|
import { DatePicker, Form, Input } 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 { type IDayOff } from "./type"
|
|
import dayjs from "dayjs"
|
|
import { toast } from "sonner"
|
|
|
|
type Props = {
|
|
isUpdate?: boolean
|
|
data?: IDayOff
|
|
onCancel?: () => any
|
|
}
|
|
|
|
export default function ComponentForm({ data, isUpdate, onCancel }: Props) {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [form] = Form.useForm();
|
|
const router = useRouter()
|
|
const axios = useAxiosAuth()
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.setFieldsValue(data)
|
|
if (data.date) form.setFieldValue('date', dayjs(data.date as string))
|
|
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [data, form])
|
|
|
|
const handleSubmit = (value: any) => {
|
|
if (isUpdate) {
|
|
value['date'] = dayjs(value.date).format('YYYY-MM-DD')
|
|
} else {
|
|
value['date'] = value.date.split('|')
|
|
}
|
|
console.log(value)
|
|
|
|
!isUpdate ? handleCreate(value) : handleUpdate(value)
|
|
}
|
|
|
|
const handleCreate = async (payload: IDayOff) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.post('/dayoff', payload)
|
|
|
|
handleSuccess('Data successfully created.')
|
|
} catch (error: any) {
|
|
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: IDayOff) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.put('/dayoff/' + 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}>
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">{isUpdate ? 'Update day off' : 'Create day off'}</DialogTitle>
|
|
<DialogDescription>
|
|
Add a new day off calendar
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid mt-8">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="date">Date</Label>
|
|
{!isUpdate ?
|
|
<Form.Item name='date' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input.TextArea className="input" id="date" placeholder="2023-10-01|2023-10-08|2023-10-15|2023-10-22|2023-10-29" />
|
|
</Form.Item>
|
|
:
|
|
<Form.Item name='date' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<DatePicker className="input" id="date" getPopupContainer={(triggerNode: any) => triggerNode.parentNode} />
|
|
</Form.Item>
|
|
}
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="name">Day Off name</Label>
|
|
<Form.Item name='name' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input className="input" id="name" placeholder="Weekend October" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="description">Description</Label>
|
|
<Form.Item name='description' >
|
|
<Input.TextArea className="input" id="description" placeholder="" />
|
|
</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>
|
|
)
|
|
} |