Calender nav #42

Merged
LucidKobold merged 20 commits from calender-nav into main 2022-03-27 02:48:04 -04:00
8 changed files with 170 additions and 130 deletions
Showing only changes of commit 5071d5a6ba - Show all commits

View File

@@ -3,11 +3,14 @@ import { useRouter } from "next/router";
import { HStack, IconButton } from "@chakra-ui/react"; import { HStack, IconButton } from "@chakra-ui/react";
import { Icon } from "@iconify/react"; import { Icon } from "@iconify/react";
import { sub, add, format, isSameMonth } from "date-fns"; import { sub, add, format, isSameMonth } from "date-fns";
import findValidDateRange from "../../lib/findValidDateRange"
import DatePicker from "./DatePicker"; import DatePicker from "./DatePicker";
import { CalenderContext } from "../../contexts/CalenderContext"; import { CalenderContext } from "../../contexts/CalenderContext";
const CalenderNav = (): JSX.Element => { const CalenderNav = (): JSX.Element => {
const { selectedDate, validDateRange } = useContext(CalenderContext); const { selectedDate } = useContext(CalenderContext);
const validDateRange = findValidDateRange();
const { start: validStart, end: validEnd } = validDateRange; const { start: validStart, end: validEnd } = validDateRange;
const router = useRouter(); const router = useRouter();

View File

@@ -206,19 +206,6 @@ const CalenderContextProvider = ({
setSelectedMonthInfo(output); setSelectedMonthInfo(output);
}; };
const setValidDateRange = (): ValidDateRange => {
const currDate = new Date(); // Current date.
const startDate = startOfMonth(currDate); // Will eventually be the creation date of the account or the creation date of the oldest chart within the account. Whichever is older.
const endDate = endOfMonth(currDate); // Always needs to be the last day on the current month within the current year.
return {
start: startDate,
end: endDate
};
};
const [validDateRange] = useState<ValidDateRange>(setValidDateRange());
// TODO: Add a function that validated if a date has at least one sticker in it. Use that within the nav function (when filter is enabled). // TODO: Add a function that validated if a date has at least one sticker in it. Use that within the nav function (when filter is enabled).
// TODO: Add a function that will give the closest date, if available, when the nav func detects an empty month. // TODO: Add a function that will give the closest date, if available, when the nav func detects an empty month.
@@ -257,7 +244,6 @@ const CalenderContextProvider = ({
selectedDate, selectedDate,
title: selectedDateInfo.title, title: selectedDateInfo.title,
layout: selectedDateInfo.layout, layout: selectedDateInfo.layout,
validDateRange,
updateDate updateDate
}; };

22
lib/findValidDateRange.ts Normal file
View File

@@ -0,0 +1,22 @@
import {
startOfMonth,
endOfMonth
} from "date-fns";
interface ValidDateRange {
start: Date;
end: Date;
}
const validDateRange = (): ValidDateRange => {
const currDate = new Date(); // Current date.
const startDate = startOfMonth(currDate); // Will eventually be the creation date of the account or the creation date of the oldest chart within the account. Whichever is older.
const endDate = endOfMonth(currDate); // Always needs to be the last day on the current month within the current year.
return {
start: startDate,
end: endDate
};
};
export default validDateRange

View File

@@ -1,7 +1,8 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState, useContext } from "react";
import { Box } from "@chakra-ui/react"; import { Box } from "@chakra-ui/react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { endOfMonth, getDay } from "date-fns"; import { endOfMonth, getDay } from "date-fns";
import findValidDateRange from "../../lib/findValidDateRange"
import ErrorPage from "next/error"; import ErrorPage from "next/error";
import Calender from "../../components/calender"; import Calender from "../../components/calender";
import { CalenderContextProvider } from "../../contexts/CalenderContext"; import { CalenderContextProvider } from "../../contexts/CalenderContext";
@@ -11,6 +12,9 @@ const DateRoute: React.FC<unknown> = () => {
const router = useRouter(); const router = useRouter();
const { date: slug } = router.query; const { date: slug } = router.query;
const validDateRange = findValidDateRange();
const { start: validStart, end: validEnd } = validDateRange;
const [date, setDate] = useState<UpdateCalendarProps | null>(null); const [date, setDate] = useState<UpdateCalendarProps | null>(null);
const [error, setError] = useState<boolean>(false); const [error, setError] = useState<boolean>(false);
@@ -54,7 +58,11 @@ const DateRoute: React.FC<unknown> = () => {
return date; return date;
}; };
useEffect(() => { // const validateDateRange = () => {
// }
useEffect(() => {2
if (slug && slug.length === 1 && slug[0] !== "now") { if (slug && slug.length === 1 && slug[0] !== "now") {
setError(true); setError(true);
return console.warn("improper date input"); return console.warn("improper date input");
@@ -75,7 +83,9 @@ const DateRoute: React.FC<unknown> = () => {
}); });
} }
} }
}, [slug]);
console.info("Context:", calenderContext)
}, [slug, calenderContext]);
if (router.isFallback) { if (router.isFallback) {
return <ErrorPage statusCode={404} />; return <ErrorPage statusCode={404} />;

View File

@@ -48,11 +48,6 @@ interface MonthContext extends MonthInfo {
layout: MonthLayout; layout: MonthLayout;
} }
interface ValidDateRange {
start: Date;
end: Date;
}
interface UpdateCalendarProps { interface UpdateCalendarProps {
year: number; year: number;
month: number; month: number;