Skip to content

Commit 52a9e2a

Browse files
committed
🐛 fix(checkpoint): prevent runtime error when moving end before start
remove unnecessary adjustComplimentDateIfNeeded, preferring to do this logic on the onSelect handlers
1 parent 69fd997 commit 52a9e2a

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

packages/web/src/views/Forms/EventForm/DateTimeSection/DatePickers/DatePickers.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,6 @@ export const DatePickers: FC<Props> = ({
4646
setSelectedEndDate,
4747
setSelectedStartDate,
4848
}) => {
49-
const adjustComplimentDateIfNeeded = (
50-
changed: "start" | "end",
51-
value: Date,
52-
) => {
53-
const start = changed === "start" ? value : selectedStartDate;
54-
const end = changed === "end" ? value : selectedEndDate;
55-
56-
const { shouldAdjust, compliment } = shouldAdjustComplimentDate(changed, {
57-
start,
58-
end,
59-
});
60-
61-
if (shouldAdjust) {
62-
if (changed === "start") {
63-
const endDisplay = dayjs(compliment).add(1, "day").toDate();
64-
setSelectedEndDate(compliment);
65-
setDisplayEndDate(endDisplay);
66-
}
67-
68-
if (changed === "end") {
69-
setSelectedStartDate(compliment);
70-
}
71-
}
72-
73-
return { shouldAdjust, compliment };
74-
};
75-
7649
const closeEndDatePicker = () => {
7750
setIsEndDatePickerOpen(false);
7851
};
@@ -150,12 +123,15 @@ export const DatePickers: FC<Props> = ({
150123
const onSelectStartDate = (start: Date) => {
151124
setIsStartDatePickerOpen(false);
152125
setSelectedStartDate(start);
153-
const { shouldAdjust, compliment } = adjustComplimentDateIfNeeded(
154-
"start",
126+
const { shouldAdjust, compliment } = shouldAdjustComplimentDate("start", {
155127
start,
156-
);
128+
end: selectedEndDate,
129+
});
157130

158131
if (shouldAdjust) {
132+
const endDisplay = dayjs(compliment).add(1, "day").toDate();
133+
setSelectedEndDate(compliment);
134+
setDisplayEndDate(endDisplay);
159135
onSetEventField({
160136
startDate: dayjs(start).format(MONTH_DAY_YEAR),
161137
endDate: dayjs(compliment).format(MONTH_DAY_YEAR),
@@ -167,8 +143,16 @@ export const DatePickers: FC<Props> = ({
167143
};
168144

169145
const onSelectEndDate = (end: Date) => {
146+
const { shouldAdjust, compliment } = shouldAdjustComplimentDate("end", {
147+
start: selectedStartDate,
148+
end,
149+
});
150+
151+
if (shouldAdjust) {
152+
setSelectedStartDate(compliment);
153+
}
154+
170155
setIsEndDatePickerOpen(false);
171-
adjustComplimentDateIfNeeded("end", end);
172156

173157
setDisplayEndDate(end);
174158

packages/web/src/views/Forms/EventForm/DateTimeSection/form.datetime.util.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ export const adjustEndDate = (end: Date) => {
1616

1717
export const getFormDates = (startDate: string, endDate: string) => {
1818
const start = dayjs(startDate);
19+
const startDateFormatted = start.format(YEAR_MONTH_DAY_FORMAT);
1920
const startTime = getTimeOptionByValue(start);
20-
const _startDate = start.toDate();
2121

2222
const end = dayjs(endDate);
23-
const isOneDay =
24-
start.format(YEAR_MONTH_DAY_FORMAT) ===
25-
dayjs(endDate).format(YEAR_MONTH_DAY_FORMAT);
23+
const isOneDay = startDateFormatted === end.format(YEAR_MONTH_DAY_FORMAT);
2624
const displayEndDate = isOneDay
27-
? start.format(YEAR_MONTH_DAY_FORMAT)
25+
? startDateFormatted
2826
: end.subtract(1, "day").format(YEAR_MONTH_DAY_FORMAT);
2927
const _endDate = isOneDay ? start.toDate() : end.toDate();
3028
const endTime = getTimeOptionByValue(end);
3129

3230
return {
33-
startDate: _startDate,
31+
startDate: start.toDate(),
3432
startTime,
3533
endDate: _endDate,
3634
displayEndDate,

0 commit comments

Comments
 (0)