From 4ce04553e5d3f0263cd78fcf67f02dcbafc72b04 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Sat, 19 Aug 2023 02:43:30 +0530 Subject: [PATCH] fix: module start and target date validations --- apps/app/components/issues/select/date.tsx | 4 +- apps/app/components/modules/form.tsx | 47 ++++++---------------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/apps/app/components/issues/select/date.tsx b/apps/app/components/issues/select/date.tsx index 2b601f5dce8..01fbacda3ac 100644 --- a/apps/app/components/issues/select/date.tsx +++ b/apps/app/components/issues/select/date.tsx @@ -17,7 +17,7 @@ type Props = { export const IssueDateSelect: React.FC = ({ label, maxDate, minDate, onChange, value }) => ( - {({ open }) => ( + {({ close }) => ( <> @@ -52,6 +52,8 @@ export const IssueDateSelect: React.FC = ({ label, maxDate, minDate, onCh onChange={(val) => { if (!val) onChange(""); else onChange(renderDateFormat(val)); + + close(); }} dateFormat="dd-MM-yyyy" minDate={minDate} diff --git a/apps/app/components/modules/form.tsx b/apps/app/components/modules/form.tsx index 0b36176c536..2715ba26621 100644 --- a/apps/app/components/modules/form.tsx +++ b/apps/app/components/modules/form.tsx @@ -1,15 +1,11 @@ -import { useEffect, useState } from "react"; +import { useEffect } from "react"; // react-hook-form import { Controller, useForm } from "react-hook-form"; -// hooks -import useToast from "hooks/use-toast"; // components import { ModuleLeadSelect, ModuleMembersSelect, ModuleStatusSelect } from "components/modules"; // ui import { DateSelect, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; -// helper -import { isDateRangeValid } from "helpers/date-time.helper"; // types import { IModule } from "types"; @@ -29,8 +25,6 @@ const defaultValues: Partial = { }; export const ModuleForm: React.FC = ({ handleFormSubmit, handleClose, status, data }) => { - const [isDateValid, setIsDateValid] = useState(true); - const { setToastAlert } = useToast(); const { register, formState: { errors, isSubmitting }, @@ -57,6 +51,15 @@ export const ModuleForm: React.FC = ({ handleFormSubmit, handleClose, sta }); }, [data, reset]); + const startDate = watch("start_date"); + const targetDate = watch("target_date"); + + const minDate = startDate ? new Date(startDate) : null; + minDate?.setDate(minDate.getDate()); + + const maxDate = targetDate ? new Date(targetDate) : null; + maxDate?.setDate(maxDate.getDate()); + return (
@@ -103,20 +106,8 @@ export const ModuleForm: React.FC = ({ handleFormSubmit, handleClose, sta value={value} onChange={(val) => { onChange(val); - if (val && watch("target_date")) { - if (isDateRangeValid(val, `${watch("target_date")}`)) { - setIsDateValid(true); - } else { - setIsDateValid(false); - setToastAlert({ - type: "error", - title: "Error!", - message: - "The date you have entered is invalid. Please check and enter a valid date.", - }); - } - } }} + maxDate={maxDate ?? undefined} /> )} /> @@ -129,20 +120,8 @@ export const ModuleForm: React.FC = ({ handleFormSubmit, handleClose, sta value={value} onChange={(val) => { onChange(val); - if (watch("start_date") && val) { - if (isDateRangeValid(`${watch("start_date")}`, val)) { - setIsDateValid(true); - } else { - setIsDateValid(false); - setToastAlert({ - type: "error", - title: "Error!", - message: - "The date you have entered is invalid. Please check and enter a valid date.", - }); - } - } }} + minDate={minDate ?? undefined} /> )} /> @@ -166,7 +145,7 @@ export const ModuleForm: React.FC = ({ handleFormSubmit, handleClose, sta
Cancel - + {status ? isSubmitting ? "Updating Module..."