diff --git a/components/calender/CalenderNav.tsx b/components/calender/CalenderNav.tsx index 9fbd69f..1aa5ccb 100644 --- a/components/calender/CalenderNav.tsx +++ b/components/calender/CalenderNav.tsx @@ -1,20 +1,45 @@ import React, { useContext } from "react"; +import { useRouter } from "next/router"; import { Heading, HStack, IconButton } from "@chakra-ui/react"; import { Icon } from "@iconify/react"; -import { format } from "date-fns"; +import { sub, add, format } from "date-fns"; import { CalenderContext } from "../../contexts/CalenderContext"; const CalenderNav = (): JSX.Element => { - const { selectedMonth, prevMonth, nextMonth } = useContext(CalenderContext); + const { selectedDate } = useContext(CalenderContext); - const currentMonth = format(selectedMonth, "LLLL uuuu"); + const currentMonth = format(selectedDate, "LLLL uuuu"); + + const router = useRouter(); + + const handleNavButtons = (direction: "next" | "prev") => { + if (direction === "next") { + const newMonth = add(selectedDate, { + months: 1, + }); + + const year = format(newMonth, "y"); + const month = format(newMonth, "L"); + + router.push(`/calendar/${year}/${month}`); + } else if (direction === "prev") { + const newMonth = sub(selectedDate, { + months: 1, + }); + + const year = format(newMonth, "y"); + const month = format(newMonth, "L"); + + router.push(`/calendar/${year}/${month}`); + } + } return ( } - onClick={() => prevMonth()} + onClick={() => handleNavButtons("prev")} /> { } - onClick={() => nextMonth()} + onClick={() => handleNavButtons("next")} + /> );