From d041d8be6bbd92707e1ce802c8e311f452008be8 Mon Sep 17 00:00:00 2001 From: Anmol Singh Bhatia <121005188+anmolsinghbhatia@users.noreply.github.com> Date: Sat, 22 Apr 2023 18:17:17 +0530 Subject: [PATCH] fix: cycle date validation (#922) --- apps/app/components/cycles/form.tsx | 49 ++++++++++++++++++---------- apps/app/helpers/date-time.helper.ts | 6 ++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/apps/app/components/cycles/form.tsx b/apps/app/components/cycles/form.tsx index 168f4f2cd..a15547ddc 100644 --- a/apps/app/components/cycles/form.tsx +++ b/apps/app/components/cycles/form.tsx @@ -11,7 +11,11 @@ import useToast from "hooks/use-toast"; // ui import { DateSelect, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; // helpers -import { getDateRangeStatus, isDateRangeValid } from "helpers/date-time.helper"; +import { + getDateRangeStatus, + isDateGreaterThanToday, + isDateRangeValid, +} from "helpers/date-time.helper"; // types import { ICycle } from "types"; @@ -60,24 +64,33 @@ export const CycleForm: React.FC = ({ handleFormSubmit, handleClose, stat data?.start_date && data?.end_date ? getDateRangeStatus(data?.start_date, data?.end_date) : ""; 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); + if (isDateGreaterThanToday(payload.end_date)) { + 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); + }); + } else { + setIsDateValid(false); + setToastAlert({ + type: "error", + title: "Error!", + message: "Unable to create cycle in past date. Please enter a valid date.", }); + } }; const checkEmptyDate = diff --git a/apps/app/helpers/date-time.helper.ts b/apps/app/helpers/date-time.helper.ts index 65789fd2e..1aa7ca4cc 100644 --- a/apps/app/helpers/date-time.helper.ts +++ b/apps/app/helpers/date-time.helper.ts @@ -177,6 +177,12 @@ export const renderShortTime = (date: string | Date) => { export const isDateRangeValid = (startDate: string, endDate: string) => new Date(startDate) < new Date(endDate); +export const isDateGreaterThanToday = (dateStr: string) =>{ + const date = new Date(dateStr); + const today = new Date(); + return date > today; +} + export const renderLongDateFormat = (dateString: string) => { const date = new Date(dateString); const day = date.getDate();