2023-02-28 09:25:19 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-03-17 05:10:38 +00:00
|
|
|
|
2023-02-28 09:25:19 +00:00
|
|
|
import { useRouter } from "next/router";
|
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-02-28 09:25:19 +00:00
|
|
|
// services
|
|
|
|
import cyclesService from "services/cycles.service";
|
2023-03-17 05:10:38 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// ui
|
|
|
|
import { CustomDatePicker, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
|
|
|
// helpers
|
2023-03-07 17:08:49 +00:00
|
|
|
import { getDateRangeStatus } from "helpers/date-time.helper";
|
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;
|
|
|
|
data?: ICycle;
|
|
|
|
};
|
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-02-28 09:25:19 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const [isDateValid, setIsDateValid] = useState(true);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
2023-02-28 09:25:19 +00:00
|
|
|
watch,
|
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,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-07 17:08:49 +00:00
|
|
|
const cycleStatus =
|
2023-03-15 12:11:24 +00:00
|
|
|
data?.start_date && data?.end_date ? getDateRangeStatus(data?.start_date, data?.end_date) : "";
|
2023-03-07 17:08:49 +00:00
|
|
|
|
2023-02-28 09:25:19 +00:00
|
|
|
const dateChecker = async (payload: any) => {
|
|
|
|
await cyclesService
|
|
|
|
.cycleDateCheck(workspaceSlug as string, projectId as string, payload)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.status) {
|
|
|
|
setIsDateValid(true);
|
|
|
|
} else {
|
|
|
|
setIsDateValid(false);
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message:
|
|
|
|
"You have a cycle already on the given dates, if you want to create your draft cycle you can do that by removing dates",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkEmptyDate =
|
|
|
|
(watch("start_date") === "" && watch("end_date") === "") ||
|
2023-03-01 06:25:04 +00:00
|
|
|
(!watch("start_date") && !watch("end_date"));
|
2023-02-28 09:25:19 +00:00
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
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>
|
2023-02-28 09:25:19 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
<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"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
2023-02-28 09:25:19 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
2023-03-15 12:11:24 +00:00
|
|
|
val && watch("end_date") && cycleStatus != "current"
|
2023-02-28 09:25:19 +00:00
|
|
|
? dateChecker({
|
|
|
|
start_date: val,
|
|
|
|
end_date: watch("end_date"),
|
|
|
|
})
|
|
|
|
: "";
|
|
|
|
}}
|
2023-02-20 05:53:04 +00:00
|
|
|
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"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
2023-02-28 09:25:19 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
2023-03-15 12:11:24 +00:00
|
|
|
val && watch("start_date") && cycleStatus != "current"
|
2023-02-28 09:25:19 +00:00
|
|
|
? dateChecker({
|
|
|
|
start_date: watch("start_date"),
|
|
|
|
end_date: val,
|
|
|
|
})
|
|
|
|
: "";
|
|
|
|
}}
|
2023-02-20 05:53:04 +00:00
|
|
|
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-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton
|
2023-02-28 09:25:19 +00:00
|
|
|
type="submit"
|
|
|
|
className={
|
|
|
|
checkEmptyDate
|
|
|
|
? "cursor-pointer"
|
|
|
|
: isDateValid
|
|
|
|
? "cursor-pointer"
|
|
|
|
: "cursor-not-allowed"
|
|
|
|
}
|
2023-03-17 05:10:38 +00:00
|
|
|
loading={isSubmitting || checkEmptyDate ? false : isDateValid ? false : true}
|
2023-02-28 09:25:19 +00:00
|
|
|
>
|
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>
|
|
|
|
);
|
|
|
|
};
|