mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 10:36:29 +00:00
145 lines
6.1 KiB
TypeScript
145 lines
6.1 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 { type IGlobalParameter } from "./type"
|
|
import { toast } from "sonner"
|
|
|
|
type Props = {
|
|
isUpdate?: boolean
|
|
data?: IGlobalParameter
|
|
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)
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [data, form])
|
|
|
|
const handleSubmit = (value: any) => {
|
|
console.log(value)
|
|
|
|
!isUpdate ? handleCreate(value) : handleUpdate(value)
|
|
}
|
|
|
|
const handleCreate = async (payload: IGlobalParameter) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.post('/global-parameter', 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: IGlobalParameter) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.put('/global-parameter/' + 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 global parameter' : 'Create global parameter'}</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: 'This field is required!' }]} >
|
|
<Input className="input" id="code" placeholder="AUTH_DEVICE_USERNAME" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="name">Name</Label>
|
|
<Form.Item name='name' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input className="input" id="name" placeholder="USERNAME LOGIN DEVICE" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="value">Value</Label>
|
|
<Form.Item name='value' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Input className="input" id="value" placeholder="device" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="type">Type</Label>
|
|
<Form.Item name='type' rules={[{ required: true, message: 'This field is required!' }]} >
|
|
<Select placeholder='Select type' showSearch allowClear
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
>
|
|
<Select.Option value='TEXT'>Text</Select.Option>
|
|
<Select.Option value='NUMBER'>Number</Select.Option>
|
|
<Select.Option value='DATE'>Date</Select.Option>
|
|
<Select.Option value='DATETIME'>Datetime</Select.Option>
|
|
<Select.Option value='TIMESTAMP'>Timestamp</Select.Option>
|
|
</Select>
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="description">Description</Label>
|
|
<Form.Item name='description' >
|
|
<Input.TextArea className="input" id="description" placeholder="Awesome description for this global parameter.." />
|
|
</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>
|
|
)
|
|
} |