Calender nav #42

Merged
LucidKobold merged 20 commits from calender-nav into main 2022-03-27 02:48:04 -04:00
7 changed files with 214 additions and 141 deletions
Showing only changes of commit d8c9db9475 - Show all commits

View File

@@ -2,7 +2,7 @@ import React, { useContext } from "react";
import { useRouter } from "next/router"; 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 { format, isSameMonth, addMonths, subMonths } from "date-fns";
import findValidDateRange from "../../lib/findValidDateRange"; import findValidDateRange from "../../lib/findValidDateRange";
import DatePicker from "./DatePicker"; import DatePicker from "./DatePicker";
import { CalenderContext } from "../../contexts/CalenderContext"; import { CalenderContext } from "../../contexts/CalenderContext";
@@ -17,18 +17,14 @@ const CalenderNav = (): JSX.Element => {
const handleNavButtons = (direction: "next" | "prev") => { const handleNavButtons = (direction: "next" | "prev") => {
if (direction === "next") { if (direction === "next") {
const newMonth = add(selectedDate, { const newMonth = addMonths(selectedDate, 1);
months: 1
});
const year = format(newMonth, "y"); const year = format(newMonth, "y");
const month = format(newMonth, "L"); const month = format(newMonth, "L");
router.push(`/calendar/${year}/${month}`); router.push(`/calendar/${year}/${month}`);
} else if (direction === "prev") { } else if (direction === "prev") {
const newMonth = sub(selectedDate, { const newMonth = subMonths(selectedDate, 1);
months: 1
});
const year = format(newMonth, "y"); const year = format(newMonth, "y");
const month = format(newMonth, "L"); const month = format(newMonth, "L");

View File

@@ -5,9 +5,13 @@ interface ValidDateRange {
end: Date; end: Date;
} }
const validDateRange = (): ValidDateRange => { /**
* A function that will determine the valid date range for the navigation of the charts.
* @returns An object with a start and end key with the given date for the start and end of the range.
*/
const findValidDateRange = (): ValidDateRange => {
const currDate = new Date(); // Current date. 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 startDate = startOfMonth(currDate); // Will eventually be the creation date of the account or the creation date the selected chart. Whichever is older.
const endDate = endOfMonth(currDate); // Always needs to be the last day on the current month within the current year. const endDate = endOfMonth(currDate); // Always needs to be the last day on the current month within the current year.
return { return {
@@ -16,4 +20,4 @@ const validDateRange = (): ValidDateRange => {
}; };
}; };
export default validDateRange; export default findValidDateRange;

View File

@@ -1,7 +1,14 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } 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
// getMonth,
// getYear,
// isAfter,
// isBefore
} from "date-fns";
// import findValidDateRange from "../../lib/findValidDateRange"; // 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";
@@ -12,9 +19,6 @@ 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);
@@ -58,18 +62,42 @@ const DateRoute: React.FC<unknown> = () => {
return date; return date;
}; };
// const validateDateRange = () => { /**
* ! This function does not work as is. It is causing infinite loops whe used within the useEffect.
*/
// const validateDateRange = (slugDate: Date): void => {
// const { start: validStart, end: validEnd } = validDateRange;
// // Check if the slug date is beyond the valid end date.
// if (isAfter(slugDate, validEnd)) {
// // router.push("/calender/now");
// console.warn(
// "Slug date is after the valid date range for this calendar!!!"
// );
// // Check if the slug is before the valid start date.
// } else if (isBefore(slugDate, validStart)) {
// console.warn(
// "Slug date is before the valid date range for this calendar!!!"
// );
// router.push(`/${getYear(validStart)}/${getMonth(validStart) + 1}`);
// } else {
// console.info(
// "Slug date is within the valid date range for this calendar."
// );
// } // }
// };
useEffect(() => { useEffect(() => {
2; console.log("Use Effect");
if (slug && slug.length === 1 && slug[0] !== "now") {
if (slug && Array.isArray(slug)) {
const length = slug.length;
if (length === 1 && slug[0] !== "now") {
setError(true); setError(true);
return console.warn("improper date input"); return console.warn("improper date input:", slug);
} }
if (slug && Array.isArray(slug) && slug.length >= 2 && slug.length <= 3) { if (length >= 2 && slug.length <= 3) {
const parsedSlug = slug.map((e) => { const parsedSlug = slug.map((e) => {
return parseInt(e); return parseInt(e);
}); });
@@ -79,9 +107,19 @@ const DateRoute: React.FC<unknown> = () => {
if (newDate.year === 0 || newDate.month === 0 || newDate.day === 0) { if (newDate.year === 0 || newDate.month === 0 || newDate.day === 0) {
setError(true); setError(true);
} else { } else {
// const slugDate = new Date(
// newDate.year,
// newDate.month - 1,
// newDate.day
// );
// console.info("Slug date:", slugDate);
// validateDateRange(slugDate);
setDate({ setDate({
...validateDateInput(parsedSlug) ...validateDateInput(parsedSlug)
}); });
2;
}
} }
} }
}, [slug]); }, [slug]);

View File

@@ -58,6 +58,5 @@ interface CalenderContextState {
selectedDate: Date; selectedDate: Date;
title: string; title: string;
layout: MonthLayout; layout: MonthLayout;
validDateRange: ValidDateRange;
updateDate: (input: UpdateCalendarProps) => void; updateDate: (input: UpdateCalendarProps) => void;
} }