Set default date for new events to the next full hour

This commit is contained in:
Remmy Cat Stock 2022-11-04 23:23:30 +01:00 committed by Remmy Cat Stock
parent b4f8e4402e
commit 88e0649e79
2 changed files with 15 additions and 2 deletions

View File

@ -34,6 +34,7 @@ import {
dateToDatabaseTimestamp,
databaseTimestampToDate,
getDateWithHoursOffset,
getDateAtNextFullHour,
} from "~/utils/dates";
import {
badRequestIfFalsy,
@ -312,7 +313,9 @@ function DatesInput() {
date: databaseTimestampToDate(t),
}));
}
return [{ key: getKey(), date: new Date() }];
// Initial date rounded to next full hour from now
return [{ key: getKey(), date: getDateAtNextFullHour(new Date()) }];
});
const datesCount = datesInputState.length;
@ -334,7 +337,7 @@ function DatesInput() {
const addedDate = lastValidDate
? getDateWithHoursOffset(lastValidDate, NEW_CALENDAR_EVENT_HOURS_OFFSET)
: new Date();
: getDateAtNextFullHour(new Date());
return [...current, { key: getKey(), date: addedDate }];
});

View File

@ -79,3 +79,13 @@ export function getDateWithHoursOffset(date: Date, hoursOffset: number) {
copiedDate.setHours(date.getHours() + hoursOffset);
return copiedDate;
}
export function getDateAtNextFullHour(date: Date) {
const copiedDate = new Date(date.getTime());
if (date.getMinutes() > 0) {
copiedDate.setHours(date.getHours() + 1);
copiedDate.setMinutes(0);
}
copiedDate.setSeconds(0);
return copiedDate;
}