Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/web/src/common/utils/web.date.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,48 @@ export const setEventStartEndDatesToCurrentMonth = (
endDate: monthEnd.format(),
};
};

export const setEventStartEndDates = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some unit tests for this, as I'm already seeing some bugs with its implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, but can you share what bugs did you stumble upon?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this so it doesn't start with set.... Since this isn't changing any React state, I think we should stay away from set... as the first part of a function to avoid confusion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes makes sense, thanks for pointing this out.

to: {
direction: "prev" | "next" | "current";
duration: "week" | "month";
},
event: Schema_Event,
): Schema_Event => {
const reference =
to.direction === "current" ? dayjs(new Date()) : dayjs(event.startDate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why use dayjs(new Date()) instead of dayjs()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhhhh no particular reason, I guess I was used to it but now after reading your comment I realized that the latter is the better practice, thanks for the headsup


let start: Dayjs;
let end: Dayjs;

if (to.duration === "week") {
start = reference.startOf("week");
end = reference.endOf("week");

if (to.direction === "prev") {
start = start.subtract(1, "week");
end = end.subtract(1, "week");
} else if (to.direction === "next") {
start = start.add(1, "week");
end = end.add(1, "week");
}
} else {
// duration is month
start = reference.startOf("month");
end = reference.endOf("month");

if (to.direction === "prev") {
start = start.subtract(1, "month");
end = end.subtract(1, "month");
} else if (to.direction === "next") {
start = start.add(1, "month");
end = end.add(1, "month");
}
}

return {
...event,
startDate: start.format(),
endDate: end.format(),
};
};
56 changes: 49 additions & 7 deletions packages/web/src/views/Forms/SomedayEventForm/SomedayEventForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { Key } from "ts-key-enum";
import { Categories_Event } from "@core/types/event.types";
import { ID_SOMEDAY_EVENT_FORM } from "@web/common/constants/web.constants";
import { colorByPriority } from "@web/common/styles/theme.util";
import {
setEventStartEndDatesToCurrentMonth,
setEventStartEndDatesToCurrentWeek,
} from "@web/common/utils/web.date.util";
import { setEventStartEndDates } from "@web/common/utils/web.date.util";
import { getSomedayEventsSlice } from "@web/ducks/events/slices/someday.slice";
import { useAppDispatch } from "@web/store/store.hooks";
import { useDraftContext } from "@web/views/Calendar/components/Draft/context/useDraftContext";
Expand Down Expand Up @@ -105,7 +102,6 @@ export const SomedayEventForm: React.FC<FormProps> = ({
useHotkeys(
"delete",
() => {
console.log("delete");
const confirmed = window.confirm(
`Delete ${event.title || "this event"}?`,
);
Expand Down Expand Up @@ -140,7 +136,13 @@ export const SomedayEventForm: React.FC<FormProps> = ({
"ctrl+meta+up",
(e) => {
e.preventDefault();
const updatedEvent = setEventStartEndDatesToCurrentWeek(event);
const updatedEvent = setEventStartEndDates(
{
direction: "current",
duration: "week",
},
event,
);
onSubmit(updatedEvent);
},
hotkeysOptions,
Expand All @@ -151,7 +153,47 @@ export const SomedayEventForm: React.FC<FormProps> = ({
"ctrl+meta+down",
async (e) => {
e.preventDefault();
const updatedEvent = setEventStartEndDatesToCurrentMonth(event);
const updatedEvent = setEventStartEndDates(
{
direction: "current",
duration: "month",
},
event,
);
onSubmit(updatedEvent);
},
hotkeysOptions,
[event],
);

useHotkeys(
"ctrl+meta+right",
async (e) => {
e.preventDefault();
const updatedEvent = setEventStartEndDates(
{
direction: "next",
duration: target,
},
event,
);
onSubmit(updatedEvent);
},
hotkeysOptions,
[event],
);

useHotkeys(
"ctrl+meta+left",
async (e) => {
e.preventDefault();
const updatedEvent = setEventStartEndDates(
{
direction: "prev",
duration: target,
},
event,
);
onSubmit(updatedEvent);
},
hotkeysOptions,
Expand Down