import React, { useContext, useEffect, useState } from "react"; import { Box, HStack, SimpleGrid, Text, VStack } from "@chakra-ui/react"; import CalenderNav from "./CalenderNav"; import { CalenderContext } from "../../contexts/CalenderContext"; import { getDate, sub, add, getYear, getMonth } from "date-fns"; import { useRouter } from "next/router"; // TODO: import types interface UpdateCalendarProps { year: number; month: number; day: number; } const Calender = (newDate?: UpdateCalendarProps): JSX.Element => { const { selectedDate, layout, updateDate } = useContext(CalenderContext); const router = useRouter(); useEffect(() => { if (newDate) { const { year, month, day } = newDate; if (year > 0 && month > 0 && day > 0) { updateDate(newDate); } else { console.warn("Invalid date format: ", newDate); } } }, [newDate, updateDate]); // Simulated user settings context const userSettings = { theme: "default", startOfWeek: "Sunday" }; const currMonth = layout[`${userSettings.startOfWeek.toLowerCase()}`]; const { month, weekdays } = currMonth; return ( {weekdays.map((weekDay) => { return ( {weekDay} ); })} {Object.keys(month).map((week) => { const thisWeek = month[week]; return thisWeek.map((day) => { const { date, isOverflow, overflowDirection } = day; return ( { if (overflowDirection === "next") { console.log(overflowDirection); const newMonth = add(selectedDate, { months: 1 }); const year = getYear(newMonth); const month = getMonth(newMonth) + 1; router.push(`/calendar/${year}/${month}`); } else if (overflowDirection === "prev") { const newMonth = sub(selectedDate, { months: 1 }); const year = getYear(newMonth); const month = getMonth(newMonth) + 1; router.push(`/calendar/${year}/${month}`); } } })} > {`Day ${getDate(date)}`} ); }); })} ); }; export default Calender;