Skip to content

Commit

Permalink
🐛 Invalidate isSameOrBefore for enddate wave (#1632)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 authored Dec 18, 2023
1 parent 2de18b7 commit 7cd5647
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
useUpdateMigrationWaveMutation,
} from "@app/queries/migration-waves";
import dayjs from "dayjs";

import {
Stakeholder,
StakeholderGroup,
Expand Down Expand Up @@ -268,10 +269,16 @@ export const WaveForm: React.FC<WaveFormProps> = ({
};

const endDateRangeValidator = (date: Date) => {
const sDate = startDate || new Date();
if (sDate >= date) {
return "Date is before allowable range.";
const selectedEndDate = dayjs(date);
const selectedStartDate = startDate ? dayjs(startDate) : null;

if (
!selectedStartDate ||
selectedEndDate.isSameOrBefore(selectedStartDate, "day")
) {
return "End date must be at least one day after the start date.";
}

return "";
};

Expand All @@ -281,7 +288,8 @@ export const WaveForm: React.FC<WaveFormProps> = ({
const stakeholderGroupsToRefs = (names: string[] | undefined | null) =>
matchItemsToRefs(stakeholderGroups, (i) => i.name, names);

const dateRef = useRef<HTMLDivElement | null>(null);
const startDateRef = useRef<HTMLDivElement | null>(null);
const endDateRef = useRef<HTMLDivElement | null>(null);

return (
<Form onSubmit={handleSubmit(onSubmit)}>
Expand All @@ -295,7 +303,7 @@ export const WaveForm: React.FC<WaveFormProps> = ({
/>
</GridItem>
<GridItem span={3}>
<div ref={dateRef}>
<div ref={startDateRef}>
<HookFormPFGroupController
control={control}
name="startDateStr"
Expand All @@ -307,14 +315,14 @@ export const WaveForm: React.FC<WaveFormProps> = ({
aria-label={name}
onChange={(e, val) => {
onChange(val);
trigger("endDateStr"); // Validation of endDateStr depends on startDateStr
trigger("endDateStr");
}}
placeholder="MM/DD/YYYY"
value={value}
dateFormat={(val) => dayjs(val).format("MM/DD/YYYY")}
dateParse={(val) => dayjs(val).toDate()}
validators={[startDateRangeValidator]}
appendTo={() => dateRef.current || document.body}
appendTo={() => startDateRef.current || document.body}
/>
)}
/>
Expand All @@ -330,30 +338,31 @@ export const WaveForm: React.FC<WaveFormProps> = ({
>
to
</GridItem>

<GridItem span={8}>
<HookFormPFGroupController
control={control}
name="endDateStr"
label="Potential End Date"
fieldId="endDateStr"
isRequired
renderInput={({ field: { value, name, onChange } }) => (
<DatePicker
aria-label={name}
onChange={(e, val) => {
onChange(val);
}}
placeholder="MM/DD/YYYY"
value={value}
dateFormat={(val) => dayjs(val).format("MM/DD/YYYY")}
dateParse={(val) => dayjs(val).toDate()}
validators={[endDateRangeValidator]}
appendTo={() => document.body}
/>
)}
/>
<div ref={endDateRef}>
<HookFormPFGroupController
control={control}
name="endDateStr"
label="Potential End Date"
fieldId="endDateStr"
isRequired
renderInput={({ field: { value, name, onChange } }) => (
<DatePicker
isDisabled={!startDate}
aria-label={name}
onChange={(e, val) => onChange(val)}
placeholder="MM/DD/YYYY"
value={value && startDate ? value : ""}
dateFormat={(val) => dayjs(val).format("MM/DD/YYYY")}
dateParse={(val) => dayjs(val).toDate()}
validators={[endDateRangeValidator]}
appendTo={() => endDateRef.current || document.body}
/>
)}
/>
</div>
</GridItem>

<GridItem span={12}>
<HookFormPFGroupController
control={control}
Expand Down
2 changes: 2 additions & 0 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import App from "@app/App";
import reportWebVitals from "@app/reportWebVitals";
import { KeycloakProvider } from "@app/components/KeycloakProvider";
import dayjs from "dayjs";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import customParseFormat from "dayjs/plugin/customParseFormat";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);

const queryClient = new QueryClient();

Expand Down

0 comments on commit 7cd5647

Please sign in to comment.