Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/components/calendar-layout.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can use the Weeks enum here as well

On top -
import type { CalendarEvent, Weeks } from "@/types/calendar";

startOfWeek = Weeks.SUNDAY,

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function Eventar({
showClock = false,
resources = [],
specialDays = [],
startOfWeek = "Sun"
}: EventarProps) {
const [selectedDate, setSelectedDate] = useState<Date | null>(null);
const [selectedEvent, setSelectedEvent] = useState<CalendarEvent | null>(
Expand Down Expand Up @@ -155,6 +156,7 @@ export function Eventar({
setIsEventModalOpen,
agendaView,
specialDays,
startOfWeek
})}
</ErrorBoundary>
</motion.main>
Expand Down
2 changes: 2 additions & 0 deletions src/components/render-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const renderView = ({
setIsEventModalOpen,
agendaView,
specialDays,
startOfWeek
}: RenderViewProps) => {
if (error) {
return (
Expand Down Expand Up @@ -95,6 +96,7 @@ export const renderView = ({
showPastDates={showPastDates}
handleEventClick={handleEventClick}
specialDays={specialDays}
startOfWeek={startOfWeek}
/>
);
case "day":
Expand Down
19 changes: 13 additions & 6 deletions src/hooks/use-week-view-calculations.ts
Copy link
Collaborator

Choose a reason for hiding this comment

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

In place of explicitly mentioning "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun" everywhere, create a enum for that in calendar.ts file, then you can use it here like

On top -
import type { CalendarEvent, Weeks } from "@/types/calendar";

startOfWeek: Weeks

Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import type { CalendarEvent } from "@/types/calendar";
export function useWeekViewCalculations(
date: Date,
events: CalendarEvent[],
showPastDates: boolean
showPastDates: boolean,
startOfWeek: "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun"
) {
const weekDays = useMemo(() => {
const startOfWeek = new Date(date);
startOfWeek.setDate(date.getDate() - date.getDay());
const weekDaysMap = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
const startIdx = startOfWeek
? weekDaysMap.indexOf(startOfWeek.trim().toLowerCase())
: 1;
const todayIdx = date.getDay();
const diff = (todayIdx - startIdx + 7) % 7;
const startOfWeekDate = new Date(date);
startOfWeekDate.setDate(date.getDate() - diff);

return Array.from({ length: 7 }, (_, i) => {
const day = new Date(startOfWeek);
day.setDate(startOfWeek.getDate() + i);
const day = new Date(startOfWeekDate);
day.setDate(startOfWeekDate.getDate() + i);
return day;
});
}, [date]);
}, [date, startOfWeek]);

const hours = useMemo(() => Array.from({ length: 24 }, (_, i) => i), []);

Expand Down
2 changes: 2 additions & 0 deletions src/types/calendar.ts
Copy link
Collaborator

Choose a reason for hiding this comment

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

Create a enum for Weeks here, reference below -

export enum Weeks {
MONDAY = "Mon",
TUESDAY = "Tue",
WEDNESDAY = "Wed",
THURSDAY = "Thu",
FRIDAY = "Fri",
SATURDAY = "Sat",
SUNDAY = "Sun",
}

Then everywhere -
startOfWeek?: "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun"; --> startOfWeek?: Weeks;

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface EventarProps {
showClock?: boolean;
resources?: Resource[];
specialDays?: SpecialDay[];
startOfWeek?: "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun";
}

export interface RenderViewProps {
Expand All @@ -95,6 +96,7 @@ export interface RenderViewProps {
setIsEventModalOpen: (isOpen: boolean) => void;
agendaView: boolean;
specialDays: SpecialDay[];
startOfWeek?: "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun";
}

export interface CalendarHeaderProps {
Expand Down
1 change: 1 addition & 0 deletions src/types/week.ts
Copy link
Collaborator

Choose a reason for hiding this comment

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

same goes for here - startOfWeek?: Weeks;

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface WeekViewProps {
handleEventClick?: (e: React.MouseEvent, event: CalendarEvent) => void;
isLoading?: boolean;
specialDays?: SpecialDay[];
startOfWeek?: "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun";
}

export interface WeekHeaderProps {
Expand Down
3 changes: 2 additions & 1 deletion src/views/week-view/week-view.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

same goes for here - startOfWeek ?? Weeks.SUNDAY

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export function WeekView({
handleEventClick,
isLoading,
specialDays,
startOfWeek
}: WeekViewProps) {
const { weekDays, hours, fullDayEvents, isPastDate } =
useWeekViewCalculations(date, events, showPastDates);
useWeekViewCalculations(date, events, showPastDates, startOfWeek);

if (isLoading) {
return <WeekViewSkeleton />;
Expand Down