2023-01-26 18:12:20 +00:00
|
|
|
import { Fragment } from "react";
|
2023-02-10 12:32:18 +00:00
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
import { mutate } from "swr";
|
2023-02-10 12:32:18 +00:00
|
|
|
|
|
|
|
// headless ui
|
2023-01-26 18:12:20 +00:00
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
|
|
|
// services
|
|
|
|
import cycleService from "services/cycles.service";
|
2023-02-10 12:32:18 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-01-26 18:12:20 +00:00
|
|
|
// components
|
|
|
|
import { CycleForm } from "components/cycles";
|
2023-03-07 19:37:00 +00:00
|
|
|
// helper
|
2023-05-05 10:15:53 +00:00
|
|
|
import { getDateRangeStatus, isDateGreaterThanToday } from "helpers/date-time.helper";
|
2023-01-26 18:12:20 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import type { ICurrentUserResponse, ICycle } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
// fetch keys
|
2023-03-07 19:37:00 +00:00
|
|
|
import {
|
2023-05-28 21:08:16 +00:00
|
|
|
COMPLETED_CYCLES_LIST,
|
|
|
|
CURRENT_CYCLE_LIST,
|
|
|
|
CYCLES_LIST,
|
|
|
|
DRAFT_CYCLES_LIST,
|
|
|
|
INCOMPLETE_CYCLES_LIST,
|
|
|
|
UPCOMING_CYCLES_LIST,
|
2023-03-07 19:37:00 +00:00
|
|
|
} from "constants/fetch-keys";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
type CycleModalProps = {
|
2023-01-26 18:12:20 +00:00
|
|
|
isOpen: boolean;
|
|
|
|
handleClose: () => void;
|
2023-05-28 20:44:40 +00:00
|
|
|
data?: ICycle | null;
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-02-20 05:53:04 +00:00
|
|
|
};
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
export const CreateUpdateCycleModal: React.FC<CycleModalProps> = ({
|
2023-02-10 12:32:18 +00:00
|
|
|
isOpen,
|
|
|
|
handleClose,
|
2023-02-20 05:53:04 +00:00
|
|
|
data,
|
2023-06-06 16:06:00 +00:00
|
|
|
user,
|
2023-02-10 12:32:18 +00:00
|
|
|
}) => {
|
2023-02-20 05:53:04 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-02-10 12:32:18 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
const createCycle = async (payload: Partial<ICycle>) => {
|
2023-05-28 21:08:16 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
await cycleService
|
2023-06-06 16:06:00 +00:00
|
|
|
.createCycle(workspaceSlug.toString(), projectId.toString(), payload, user)
|
2023-01-26 18:12:20 +00:00
|
|
|
.then((res) => {
|
2023-03-07 19:37:00 +00:00
|
|
|
switch (getDateRangeStatus(res.start_date, res.end_date)) {
|
|
|
|
case "completed":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(COMPLETED_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
case "current":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(CURRENT_CYCLE_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
case "upcoming":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(UPCOMING_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(DRAFT_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
}
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(INCOMPLETE_CYCLES_LIST(projectId.toString()));
|
|
|
|
mutate(CYCLES_LIST(projectId.toString()));
|
2023-01-26 18:12:20 +00:00
|
|
|
handleClose();
|
2023-02-20 13:49:46 +00:00
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Cycle created successfully.",
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
})
|
2023-05-28 21:08:16 +00:00
|
|
|
.catch(() => {
|
2023-02-10 12:32:18 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-02-20 13:49:46 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Error in creating cycle. Please try again.",
|
2023-02-10 12:32:18 +00:00
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
const updateCycle = async (cycleId: string, payload: Partial<ICycle>) => {
|
2023-05-28 21:08:16 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
await cycleService
|
2023-06-06 16:06:00 +00:00
|
|
|
.updateCycle(workspaceSlug.toString(), projectId.toString(), cycleId, payload, user)
|
2023-01-26 18:12:20 +00:00
|
|
|
.then((res) => {
|
2023-03-31 13:01:21 +00:00
|
|
|
switch (getDateRangeStatus(data?.start_date, data?.end_date)) {
|
2023-03-07 19:37:00 +00:00
|
|
|
case "completed":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(COMPLETED_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
case "current":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(CURRENT_CYCLE_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
case "upcoming":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(UPCOMING_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(DRAFT_CYCLES_LIST(projectId.toString()));
|
2023-03-07 19:37:00 +00:00
|
|
|
}
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(CYCLES_LIST(projectId.toString()));
|
2023-03-31 13:01:21 +00:00
|
|
|
if (
|
|
|
|
getDateRangeStatus(data?.start_date, data?.end_date) !=
|
|
|
|
getDateRangeStatus(res.start_date, res.end_date)
|
|
|
|
) {
|
|
|
|
switch (getDateRangeStatus(res.start_date, res.end_date)) {
|
|
|
|
case "completed":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(COMPLETED_CYCLES_LIST(projectId.toString()));
|
2023-03-31 13:01:21 +00:00
|
|
|
break;
|
|
|
|
case "current":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(CURRENT_CYCLE_LIST(projectId.toString()));
|
2023-03-31 13:01:21 +00:00
|
|
|
break;
|
|
|
|
case "upcoming":
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(UPCOMING_CYCLES_LIST(projectId.toString()));
|
2023-03-31 13:01:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-28 21:08:16 +00:00
|
|
|
mutate(DRAFT_CYCLES_LIST(projectId.toString()));
|
2023-03-31 13:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
handleClose();
|
2023-02-20 13:49:46 +00:00
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Cycle updated successfully.",
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-02-10 12:32:18 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-02-20 13:49:46 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Error in updating cycle. Please try again.",
|
2023-02-10 12:32:18 +00:00
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-05-05 10:15:53 +00:00
|
|
|
const dateChecker = async (payload: any) => {
|
|
|
|
try {
|
|
|
|
const res = await cycleService.cycleDateCheck(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
payload
|
|
|
|
);
|
|
|
|
console.log(res);
|
|
|
|
return res.status;
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
const handleFormSubmit = async (formData: Partial<ICycle>) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
const payload: Partial<ICycle> = {
|
|
|
|
...formData,
|
|
|
|
};
|
2023-02-02 13:03:46 +00:00
|
|
|
|
2023-05-05 10:15:53 +00:00
|
|
|
if (payload.start_date && payload.end_date) {
|
|
|
|
if (!isDateGreaterThanToday(payload.end_date)) {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Unable to create cycle in past date. Please enter a valid date.",
|
|
|
|
});
|
2023-05-18 13:37:01 +00:00
|
|
|
handleClose();
|
2023-05-05 10:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data?.start_date && data?.end_date) {
|
|
|
|
const isDateValidForExistingCycle = await dateChecker({
|
|
|
|
start_date: payload.start_date,
|
|
|
|
end_date: payload.end_date,
|
|
|
|
cycle_id: data.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isDateValidForExistingCycle) {
|
|
|
|
await updateCycle(data.id, payload);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
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",
|
|
|
|
});
|
2023-05-18 13:37:01 +00:00
|
|
|
handleClose();
|
2023-05-05 10:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:37:01 +00:00
|
|
|
const isDateValid = await dateChecker({
|
|
|
|
start_date: payload.start_date,
|
|
|
|
end_date: payload.end_date,
|
|
|
|
});
|
|
|
|
|
2023-05-05 10:15:53 +00:00
|
|
|
if (isDateValid) {
|
|
|
|
if (data) {
|
|
|
|
await updateCycle(data.id, payload);
|
|
|
|
} else {
|
|
|
|
await createCycle(payload);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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",
|
|
|
|
});
|
2023-05-18 13:37:01 +00:00
|
|
|
handleClose();
|
2023-05-05 10:15:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (data) {
|
|
|
|
await updateCycle(data.id, payload);
|
|
|
|
} else {
|
|
|
|
await createCycle(payload);
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transition.Root show={isOpen} as={Fragment}>
|
|
|
|
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="fixed inset-0 bg-custom-backdrop bg-opacity-50 transition-opacity" />
|
2023-01-26 18:12:20 +00:00
|
|
|
</Transition.Child>
|
|
|
|
<div className="fixed inset-0 z-20 overflow-y-auto">
|
|
|
|
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
|
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-300"
|
|
|
|
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leave="ease-in duration-200"
|
|
|
|
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
|
|
|
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<Dialog.Panel className="relative transform rounded-lg border border-custom-border-100 bg-custom-background-100 px-5 py-8 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-2xl sm:p-6">
|
2023-02-20 05:53:04 +00:00
|
|
|
<CycleForm
|
|
|
|
handleFormSubmit={handleFormSubmit}
|
|
|
|
handleClose={handleClose}
|
|
|
|
status={data ? true : false}
|
|
|
|
data={data}
|
|
|
|
/>
|
2023-01-26 18:12:20 +00:00
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
);
|
|
|
|
};
|