Skip to content

Commit

Permalink
Merge pull request #123 from uwblueprint/prasanna/calendar-week-funct…
Browse files Browse the repository at this point in the history
…ionality

Prasanna/calendar week functionality
  • Loading branch information
jeessh authored Nov 22, 2024
2 parents 16641db + 110c5e8 commit cdb5166
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
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;

0 comments on commit cdb5166

Please sign in to comment.