Skip to content

Commit

Permalink
fix: changing dates will now change end date too.
Browse files Browse the repository at this point in the history
fix: update all dates work
  • Loading branch information
ImJustChew committed Oct 3, 2024
1 parent 3628fdd commit 216a864
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
63 changes: 45 additions & 18 deletions src/components/Calendar/AddEventButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
startOfDay,
} from "date-fns";
import { cn } from "@/lib/utils";
import { PropsWithChildren, useEffect, useMemo, useState } from "react";
import {
PropsWithChildren,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { UseFormReturn, useForm, useWatch } from "react-hook-form";
Expand Down Expand Up @@ -70,6 +76,23 @@ export const AddEventButton = ({
const { currentColors } = useUserTimetable();
const { labels } = useCalendar();

const generateEmptyEvent = useCallback(
() => ({
id: uuidv4(),
title: undefined,
details: undefined,
allDay: true,
start: startOfDay(new Date()),
end: endOfDay(new Date()),
repeat: {
type: null,
},
color: currentColors[0],
tag: labels[0],
}),
[currentColors, labels],
);

const minuteStep = 15;
const form = useForm<z.infer<typeof eventFormSchema>>({
resolver: zodResolver(eventFormSchema),
Expand All @@ -80,27 +103,22 @@ export const AddEventButton = ({
...defaultEvent,
repeat: defaultEvent.repeat ?? { type: null },
}
: {
id: uuidv4(),
title: undefined,
details: undefined,
allDay: true,
start: startOfDay(new Date()),
end: endOfDay(new Date()),
repeat: {
type: null,
},
color: currentColors[0],
tag: labels[0],
},
[defaultEvent, currentColors, labels],
: generateEmptyEvent(),
[defaultEvent, generateEmptyEvent],
),
mode: "onChange",
});

useEffect(() => {
form.trigger();
}, [defaultEvent, form.trigger]);
}, [defaultEvent, form, open]);

// if form is opened, and defaultEvent is not provided, reset form to default values
useEffect(() => {
if (open && !defaultEvent) {
form.reset(generateEmptyEvent());
}
}, [defaultEvent, form, open, generateEmptyEvent]);

const onSubmit = (data: z.infer<typeof eventFormSchema>) => {
const eventDef: CalendarEvent = {
Expand Down Expand Up @@ -155,7 +173,7 @@ export const AddEventButton = ({
form.trigger(["start", "end"]);
setDelayed(false);
}
}, [delayed, defaultEvent]);
}, [delayed, defaultEvent, form]);

//if from normal to all day, set start and end to 00:00 and 23:59
useEffect(() => {
Expand All @@ -166,7 +184,7 @@ export const AddEventButton = ({
} else {
setDelayed(true);
}
}, [allDay]);
}, [allDay, form]);

const renderNormalDatePicker = () => {
return (
Expand Down Expand Up @@ -210,6 +228,15 @@ export const AddEventButton = ({
minutes: originalDate.getMinutes(),
});
field.onChange(newDate);
// update end date as well
const diff =
form.getValues("end").getTime() -
originalDate.getTime();
form.setValue(
"end",
new Date(newDate.getTime() + diff),
);
form.trigger(["end"]);
}}
initialFocus
/>
Expand Down
9 changes: 2 additions & 7 deletions src/components/Calendar/calendar_hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,10 @@ export const useCalendarProvider = () => {
break;
case UpdateType.ALL:
//just update the event
const newEvent4 = {
...newEvent,
start: oldEvent.start,
end: oldEvent.end,
};
await eventsCol!.findOne(newEvent.id).update({
$set: {
...serializeEvent(newEvent4),
actualEnd: getActualEndDate(newEvent4),
...serializeEvent(newEvent),
actualEnd: getActualEndDate(newEvent),
},
});
break;
Expand Down

0 comments on commit 216a864

Please sign in to comment.