Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prasanna/calendar week functionality #123

Merged
merged 2 commits into from
Nov 22, 2024
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
31 changes: 27 additions & 4 deletions frontend/src/components/pages/schedule/SchedulePage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useImperativeHandle, useRef, useState } from "react";
import {
Flex,
Tabs,
Expand All @@ -19,15 +19,33 @@ import {
CalendarMonth,
} from "@mui/icons-material";

import FullCalendar from "@fullcalendar/react";
import { CalendarApi } from "@fullcalendar/core";
import { ScheduleType } from "../../../types/ScheduleTypes";
import ScheduleListView from "./listView/ScheduleListView";
import { ScheduleCalendar } from "./calendarView/ScheduleCalendar";
import ScheduleCalendar from "./calendarView/ScheduleCalendar";

const SchedulePage = (): React.ReactElement => {
const [rooms, setRooms] = useState<number[]>([]);
const [scheduleType, setScheduleType] = useState<ScheduleType>("LIST");
const [scheduleData, setScheduleData] = useState<string>("");
const [active, setActive] = useState<string>("List");
const [dateRange, setDateRange] = useState("Jan 1 - 7");

const calendarRef = useRef<CalendarApi | null>(null);

const handleNext = () => {
console.log(scheduleType);
if (scheduleType === "CALENDAR") {
calendarRef.current?.next();
}
};

const handlePrev = () => {
if (scheduleType === "CALENDAR") {
calendarRef.current?.prev();
}
};

useEffect(() => {
// TODO: Fetch occupied rooms from API?
Expand Down Expand Up @@ -80,6 +98,7 @@ const SchedulePage = (): React.ReactElement => {

<Flex w="200px" flexDir="row" height="100px" ml={5}>
<IconButton
onClick={handlePrev}
_hover={{
cursor: "pointer",
}}
Expand All @@ -96,9 +115,10 @@ const SchedulePage = (): React.ReactElement => {
size="md"
fontSize="lg"
>
Jan 1 - 7
{dateRange}
</Button>
<IconButton
onClick={handleNext}
_hover={{
cursor: "pointer",
}}
Expand Down Expand Up @@ -161,7 +181,10 @@ const SchedulePage = (): React.ReactElement => {
</Flex>
<Box padding="40px">
{scheduleType === "CALENDAR" ? (
<ScheduleCalendar />
<ScheduleCalendar
ref={calendarRef}
setDateRange={(range: string) => setDateRange(range)}
/>
) : (
<ScheduleListView />
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import FullCalendar from "@fullcalendar/react";
import timeGridPlugin from "@fullcalendar/timegrid";
import { DayHeaderContentArg, EventContentArg } from "@fullcalendar/core";
import React, { useEffect, useRef } from "react";
import React, {
forwardRef,
useEffect,
useImperativeHandle,
useRef,
} from "react";
import ModeCommentOutlinedIcon from "@mui/icons-material/ModeCommentOutlined";
import "./ScheduleCalendar.css";
import { ConnectingAirportsOutlined } from "@mui/icons-material";

const events = [
{
Expand Down Expand Up @@ -63,14 +69,54 @@ function renderHeaderContent(date: DayHeaderContentArg) {
);
}

export function ScheduleCalendar() {
const calendarRef = useRef<any>(null);
interface ScheduleCalendarProps {
setDateRange: (range: string) => void;
}

interface ScheduleCalendarHandle {
next: () => void;
prev: () => void;
}

const ScheduleCalendar = forwardRef<
ScheduleCalendarHandle,
ScheduleCalendarProps
>((props, ref) => {
const calendarRef = useRef<FullCalendar | null>(null);

const handleAllDayContent = (arg: any) => {
return <span>{arg.text ? "" : ""}</span>;
};

const formatDateRange = (startDate: Date, endDate: Date) => {
const startFormat = startDate.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
});
const endFormat = endDate.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
});

if (startDate.getMonth() === endDate.getMonth()) {
return `${startFormat} - ${endFormat.split(" ")[1]}`;
}
return `${startFormat} - ${endFormat}`;
};

useImperativeHandle(ref, () => ({
next: () => {
if (calendarRef.current) calendarRef.current.getApi().next();
},
prev: () => {
if (calendarRef.current) calendarRef.current.getApi().prev();
},
}));

return (
<div>
<FullCalendar
ref={calendarRef}
plugins={[timeGridPlugin]}
initialView="timeGridWeek"
weekends
Expand All @@ -88,9 +134,15 @@ export function ScheduleCalendar() {
eventBackgroundColor="transparent"
eventBorderColor="transparent"
slotEventOverlap={false}
datesSet={(dateInfo) => {
props.setDateRange(formatDateRange(dateInfo.start, dateInfo.end));
}}
headerToolbar={false}
/>
</div>
);
}
});

ScheduleCalendar.displayName = "ScheduleCalendar";

export default ScheduleCalendar;
Loading