Added navigation functions.

This commit is contained in:
Lucid Kobold
2021-11-30 21:34:27 -06:00
parent 68949175e7
commit e82d9df088

View File

@@ -1,5 +1,5 @@
import React, { createContext, useState, ReactNode, useEffect } from "react"; import React, { createContext, useState, ReactNode, useEffect } from "react";
import { endOfMonth, getDate } from "date-fns"; import { endOfMonth, getDate, sub, add } from "date-fns";
// TODO: import types // TODO: import types
type days = type days =
@@ -46,6 +46,7 @@ const CalenderContextProvider = ({
const [daysOfMonth, setDaysOfMonth] = useState<[number] | [null]>([null]); const [daysOfMonth, setDaysOfMonth] = useState<[number] | [null]>([null]);
// Populate days of the month and update them when the month changes.
useEffect(() => { useEffect(() => {
if ( if (
lastDayOfSelectedMonth !== daysOfMonth[daysOfMonth.length - 1] || lastDayOfSelectedMonth !== daysOfMonth[daysOfMonth.length - 1] ||
@@ -64,6 +65,17 @@ const CalenderContextProvider = ({
} }
}, [selectedMonth, lastDayOfCurrMonth]); }, [selectedMonth, lastDayOfCurrMonth]);
// Update selected month sates when the selected month is updated.
useEffect(() => {
if (endOfSelectedMonth !== endOfMonth(selectedMonth)) {
SetEndOfSelectedDMonth(endOfMonth(selectedMonth));
}
if (lastDayOfSelectedMonth !== getDate(endOfSelectedMonth)) {
setLastDayOfSelectedMonth(getDate(endOfSelectedMonth));
}
}, [selectedMonth]);
// Calender Layout // Calender Layout
const daysOfWeek = { const daysOfWeek = {
startOfWeek: { startOfWeek: {
@@ -91,7 +103,21 @@ const CalenderContextProvider = ({
//TODO: Create an object of arrays that will align with the days on the week. Make two sets for each start of the week setting. //TODO: Create an object of arrays that will align with the days on the week. Make two sets for each start of the week setting.
// Navigation // Navigation
const prevMonth = (): void => {}; const prevMonth = (): void => {
const newMonth = sub(today, {
years: 1,
});
setSelectedMonth(newMonth);
};
const nextMonth = (): void => {
const newMonth = add(today, {
years: 1,
});
setSelectedMonth(newMonth);
};
const calenderContextValues = { const calenderContextValues = {
today, today,