2023-02-20 05:53:04 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
// react-hook-form
|
2023-01-26 18:12:20 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-02-20 05:53:04 +00:00
|
|
|
// ui
|
|
|
|
import { Button, CustomDatePicker, CustomSelect, Input, TextArea } from "components/ui";
|
2023-01-26 18:12:20 +00:00
|
|
|
// types
|
2023-02-20 05:53:04 +00:00
|
|
|
import { ICycle } from "types";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleFormSubmit: (values: Partial<ICycle>) => Promise<void>;
|
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
|
|
|
data?: ICycle;
|
|
|
|
};
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<ICycle> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
status: "draft",
|
2023-02-20 05:53:04 +00:00
|
|
|
start_date: "",
|
|
|
|
end_date: "",
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
export const CycleForm: React.FC<Props> = ({ handleFormSubmit, handleClose, status, data }) => {
|
2023-01-26 18:12:20 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
2023-02-20 05:53:04 +00:00
|
|
|
reset,
|
2023-01-26 18:12:20 +00:00
|
|
|
} = useForm<ICycle>({
|
2023-02-20 05:53:04 +00:00
|
|
|
defaultValues,
|
2023-01-26 18:12:20 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
const handleCreateUpdateCycle = async (formData: Partial<ICycle>) => {
|
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
2023-02-20 05:53:04 +00:00
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdateCycle)}>
|
2023-01-26 18:12:20 +00:00
|
|
|
<div className="space-y-5">
|
2023-02-20 05:53:04 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900">
|
|
|
|
{status ? "Update" : "Create"} Cycle
|
|
|
|
</h3>
|
2023-01-26 18:12:20 +00:00
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
label="Name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
|
|
|
placeholder="Enter name"
|
|
|
|
autoComplete="off"
|
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Name is required",
|
2023-02-20 05:53:04 +00:00
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
|
|
|
message: "Name should be less than 255 characters",
|
|
|
|
},
|
2023-01-26 18:12:20 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
label="Description"
|
|
|
|
placeholder="Enter description"
|
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<h6 className="text-gray-500">Status</h6>
|
|
|
|
<Controller
|
|
|
|
name="status"
|
|
|
|
control={control}
|
|
|
|
render={({ field }) => (
|
|
|
|
<CustomSelect
|
|
|
|
{...field}
|
|
|
|
label={<span className="capitalize">{field.value ?? "Select Status"}</span>}
|
|
|
|
input
|
|
|
|
>
|
|
|
|
{[
|
|
|
|
{ label: "Draft", value: "draft" },
|
|
|
|
{ label: "Started", value: "started" },
|
|
|
|
{ label: "Completed", value: "completed" },
|
|
|
|
].map((item) => (
|
|
|
|
<CustomSelect.Option key={item.value} value={item.value}>
|
|
|
|
{item.label}
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex gap-x-2">
|
|
|
|
<div className="w-full">
|
2023-02-20 05:53:04 +00:00
|
|
|
<h6 className="text-gray-500">Start Date</h6>
|
|
|
|
<div className="w-full">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="start_date"
|
|
|
|
rules={{ required: "Start date is required" }}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
error={errors.start_date ? true : false}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{errors.start_date && (
|
|
|
|
<h6 className="text-sm text-red-500">{errors.start_date.message}</h6>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
<div className="w-full">
|
2023-02-20 05:53:04 +00:00
|
|
|
<h6 className="text-gray-500">End Date</h6>
|
|
|
|
<div className="w-full">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="end_date"
|
|
|
|
rules={{ required: "End date is required" }}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
error={errors.end_date ? true : false}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{errors.end_date && (
|
|
|
|
<h6 className="text-sm text-red-500">{errors.end_date.message}</h6>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
2023-02-20 05:53:04 +00:00
|
|
|
<Button theme="secondary" onClick={handleClose}>
|
2023-01-26 18:12:20 +00:00
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
<Button type="submit" disabled={isSubmitting}>
|
2023-02-20 05:53:04 +00:00
|
|
|
{status
|
2023-01-26 18:12:20 +00:00
|
|
|
? isSubmitting
|
|
|
|
? "Updating Cycle..."
|
|
|
|
: "Update Cycle"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating Cycle..."
|
|
|
|
: "Create Cycle"}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|