Skip to content

Commit

Permalink
Use date objects in helper functions
Browse files Browse the repository at this point in the history
Converted date/time to object types for clear context and enhanced strict typing, replacing string representations.
  • Loading branch information
kasperbirch1 committed Mar 26, 2024
1 parent 8c0bb1a commit 4785a51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/apps/opening-hours-editor/DialogFomularAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const DialogFomularAdd: React.FC<DialogFomularAddProps> = ({
const calendarApi = selectedEventInfo.view.calendar;

const handleSubmit: EventFormOnSubmitType = (title, startTime, endTime) => {
const startDate = updateEventTime(selectedEventInfo.startStr, startTime);
let endDate = updateEventTime(selectedEventInfo.endStr, endTime);
const startDate = updateEventTime(selectedEventInfo.start, startTime);
let endDate = updateEventTime(selectedEventInfo.end, endTime);

endDate = adjustEndDateToStartDay(startDate, endDate);

Expand All @@ -42,8 +42,8 @@ const DialogFomularAdd: React.FC<DialogFomularAddProps> = ({
return (
<EventForm
initialTitle=""
initialStartTime={extractTime(selectedEventInfo.startStr)}
initialEndTime={extractTime(selectedEventInfo.endStr)}
initialStartTime={extractTime(selectedEventInfo.start)}
initialEndTime={extractTime(selectedEventInfo.end)}
onSubmit={handleSubmit}
/>
);
Expand Down
22 changes: 14 additions & 8 deletions src/apps/opening-hours-editor/DialogFomularEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ const DialogFomularEdit: React.FC<DialogFomularEditProps> = ({
closeDialog
}) => {
const handleSubmit: EventFormOnSubmitType = (title, startTime, endTime) => {
eventInfo.setProp("title", title);
const startDate = updateEventTime(eventInfo.startStr, startTime);
if (eventInfo.start && eventInfo.end) {
eventInfo.setProp("title", title);
const startDate = updateEventTime(eventInfo.start, startTime);

const endDate = updateEventTime(eventInfo.endStr, endTime);
eventInfo.setDates(startDate, endDate);
const endDate = updateEventTime(eventInfo.end, endTime);
eventInfo.setDates(startDate, endDate);

handleEventEditing(eventInfo);
closeDialog();
handleEventEditing(eventInfo);
closeDialog();
}
};

if (!eventInfo.start || !eventInfo.end) {
return null;
}

return (
<EventForm
initialTitle={eventInfo.title}
initialStartTime={extractTime(eventInfo.startStr)}
initialEndTime={extractTime(eventInfo.endStr)}
initialStartTime={extractTime(eventInfo.start)}
initialEndTime={extractTime(eventInfo.end)}
onSubmit={handleSubmit}
/>
);
Expand Down
6 changes: 2 additions & 4 deletions src/apps/opening-hours-editor/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ export const adjustEndDateToStartDay = (startDay: Date, endDay: Date) => {
return adjustedEndDay;
};

export const extractTime = (dateStr: string) => {
const date = new Date(dateStr);
export const extractTime = (date: Date) => {
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
return `${hours}:${minutes}`;
};

export const updateEventTime = (dateStr: string, timeStr: string) => {
export const updateEventTime = (date: Date, timeStr: string) => {
const [hours, minutes] = timeStr.split(":").map(Number);
const date = new Date(dateStr);
date.setHours(hours, minutes);
return date;
};

0 comments on commit 4785a51

Please sign in to comment.