2023-05-05 10:15:53 +00:00
|
|
|
import { useEffect } from "react";
|
2023-02-20 05:53:04 +00:00
|
|
|
|
|
|
|
// react-hook-form
|
2023-01-26 18:12:20 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-05-05 10:15:53 +00:00
|
|
|
|
2023-03-17 05:10:38 +00:00
|
|
|
// ui
|
2023-03-30 13:58:04 +00:00
|
|
|
import { DateSelect, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-03-17 05:10:38 +00:00
|
|
|
// types
|
|
|
|
import { ICycle } from "types";
|
2023-02-20 05:53:04 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleFormSubmit: (values: Partial<ICycle>) => Promise<void>;
|
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
2023-05-28 21:08:16 +00:00
|
|
|
data?: ICycle | null;
|
2023-02-20 05:53:04 +00:00
|
|
|
};
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<ICycle> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
2023-03-01 06:25:04 +00:00
|
|
|
start_date: null,
|
|
|
|
end_date: null,
|
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-07-23 17:24:17 +00:00
|
|
|
watch,
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
2023-07-23 17:24:17 +00:00
|
|
|
const startDate = watch("start_date");
|
|
|
|
const endDate = watch("end_date");
|
|
|
|
|
|
|
|
const minDate = startDate ? new Date(startDate) : new Date();
|
|
|
|
minDate.setDate(minDate.getDate() + 1);
|
|
|
|
|
|
|
|
const maxDate = endDate ? new Date(endDate) : null;
|
|
|
|
maxDate?.setDate(maxDate.getDate() - 1);
|
|
|
|
|
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-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-custom-text-100">
|
2023-02-20 05:53:04 +00:00
|
|
|
{status ? "Update" : "Create"} Cycle
|
|
|
|
</h3>
|
2023-01-26 18:12:20 +00:00
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
2023-03-30 13:05:29 +00:00
|
|
|
autoComplete="off"
|
2023-01-26 18:12:20 +00:00
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
2023-03-30 13:05:29 +00:00
|
|
|
className="resize-none text-xl"
|
|
|
|
placeholder="Title"
|
2023-01-26 18:12:20 +00:00
|
|
|
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"
|
2023-03-30 13:05:29 +00:00
|
|
|
placeholder="Description"
|
|
|
|
className="h-32 resize-none text-sm"
|
2023-01-26 18:12:20 +00:00
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-02-28 09:25:19 +00:00
|
|
|
|
2023-03-30 13:05:29 +00:00
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="start_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2023-07-23 17:24:17 +00:00
|
|
|
<DateSelect
|
|
|
|
label="Start date"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => onChange(val)}
|
|
|
|
minDate={new Date()}
|
|
|
|
maxDate={maxDate ?? undefined}
|
|
|
|
/>
|
2023-02-20 05:53:04 +00:00
|
|
|
)}
|
2023-03-30 13:05:29 +00:00
|
|
|
/>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
2023-03-30 13:05:29 +00:00
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="end_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2023-07-23 17:24:17 +00:00
|
|
|
<DateSelect
|
|
|
|
label="End date"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => onChange(val)}
|
|
|
|
minDate={minDate}
|
|
|
|
/>
|
2023-02-20 05:53:04 +00:00
|
|
|
)}
|
2023-03-30 13:05:29 +00:00
|
|
|
/>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 10:58:23 +00:00
|
|
|
<div className="-mx-5 mt-5 flex justify-end gap-2 border-t border-custom-border-200 px-5 pt-5">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
2023-05-05 10:15:53 +00:00
|
|
|
<PrimaryButton type="submit" loading={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"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|