🐛 Fix issue with applying times after midnight (#727)

This commit is contained in:
Luke Vella 2023-07-06 14:50:30 +01:00 committed by GitHub
parent 96e175e277
commit a45464bd57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 20 deletions

View file

@ -150,6 +150,7 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
: { : {
type: "timeSlot", type: "timeSlot",
start: formatDateWithoutTz(selectedDate), start: formatDateWithoutTz(selectedDate),
duration,
end: formatDateWithoutTz( end: formatDateWithoutTz(
dayjs(selectedDate) dayjs(selectedDate)
.add(duration, "minutes") .add(duration, "minutes")
@ -225,6 +226,7 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
return { return {
type: "timeSlot", type: "timeSlot",
start: formatDateWithoutTz(startDate), start: formatDateWithoutTz(startDate),
duration: duration,
end: formatDateWithoutTz(endDate), end: formatDateWithoutTz(endDate),
}; };
}), }),
@ -346,6 +348,7 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
{ {
type: "timeSlot", type: "timeSlot",
start: startTime, start: startTime,
duration,
end: formatDateWithoutTz( end: formatDateWithoutTz(
dayjs(new Date(startTime)) dayjs(new Date(startTime))
.add(duration, "minutes") .add(duration, "minutes")
@ -382,6 +385,7 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
startTime: option.start.substring( startTime: option.start.substring(
option.start.indexOf("T"), option.start.indexOf("T"),
), ),
duration: option.duration,
endTime: option.end.substring( endTime: option.end.substring(
option.end.indexOf("T"), option.end.indexOf("T"),
), ),
@ -392,10 +396,15 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
Object.keys(optionsByDay).forEach( Object.keys(optionsByDay).forEach(
(dateString) => { (dateString) => {
times.forEach((time) => { times.forEach((time) => {
const start =
dateString + time.startTime;
newOptions.push({ newOptions.push({
type: "timeSlot", type: "timeSlot",
start: dateString + time.startTime, start: start,
end: dateString + time.endTime, duration,
end: dayjs(start)
.add(duration, "minutes")
.format("YYYY-MM-DDTHH:mm"),
}); });
}); });
}, },

View file

@ -6,6 +6,7 @@ export type DateOption = {
export type TimeOption = { export type TimeOption = {
type: "timeSlot"; type: "timeSlot";
start: string; start: string;
duration: number;
end: string; end: string;
}; };

View file

@ -157,6 +157,7 @@ const WeekCalendar: React.FunctionComponent<DateTimePickerProps> = ({
const newEvent: DateTimeOption = { const newEvent: DateTimeOption = {
type: "timeSlot", type: "timeSlot",
start: formatDateWithoutTz(startDate), start: formatDateWithoutTz(startDate),
duration: dayjs(endDate).diff(endDate, "minutes"),
end: formatDateWithoutTz(endDate), end: formatDateWithoutTz(endDate),
}; };

View file

@ -67,6 +67,7 @@ const Page: NextPageWithLayout = () => {
? { ? {
type: "timeSlot", type: "timeSlot",
start: start.format("YYYY-MM-DDTHH:mm:ss"), start: start.format("YYYY-MM-DDTHH:mm:ss"),
duration: option.duration,
end: start end: start
.add(option.duration, "minute") .add(option.duration, "minute")
.format("YYYY-MM-DDTHH:mm:ss"), .format("YYYY-MM-DDTHH:mm:ss"),

View file

@ -40,8 +40,6 @@ export interface ParsedTimeSlotOption {
export type ParsedDateTimeOpton = ParsedDateOption | ParsedTimeSlotOption; export type ParsedDateTimeOpton = ParsedDateOption | ParsedTimeSlotOption;
const isTimeSlot = (value: string) => value.indexOf("/") !== -1;
export const getDuration = (startTime: dayjs.Dayjs, endTime: dayjs.Dayjs) => { export const getDuration = (startTime: dayjs.Dayjs, endTime: dayjs.Dayjs) => {
const hours = Math.floor(endTime.diff(startTime, "hours")); const hours = Math.floor(endTime.diff(startTime, "hours"));
const minutes = Math.floor(endTime.diff(startTime, "minute") - hours * 60); const minutes = Math.floor(endTime.diff(startTime, "minute") - hours * 60);
@ -157,19 +155,3 @@ export const expectTimeOption = (d: DateTimeOption): TimeOption => {
} }
return d; return d;
}; };
export const parseValue = (value: string): DateTimeOption => {
if (isTimeSlot(value)) {
const [start, end] = value.split("/");
return {
type: "timeSlot",
start,
end,
};
} else {
return {
type: "date",
date: value,
};
}
};