Calender nav #42
@@ -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");
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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,30 +62,64 @@ 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") {
|
|
||||||
setError(true);
|
|
||||||
return console.warn("improper date input");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (slug && Array.isArray(slug) && slug.length >= 2 && slug.length <= 3) {
|
if (slug && Array.isArray(slug)) {
|
||||||
const parsedSlug = slug.map((e) => {
|
const length = slug.length;
|
||||||
return parseInt(e);
|
if (length === 1 && slug[0] !== "now") {
|
||||||
});
|
|
||||||
|
|
||||||
const newDate = validateDateInput(parsedSlug);
|
|
||||||
|
|
||||||
if (newDate.year === 0 || newDate.month === 0 || newDate.day === 0) {
|
|
||||||
setError(true);
|
setError(true);
|
||||||
} else {
|
return console.warn("improper date input:", slug);
|
||||||
setDate({
|
}
|
||||||
...validateDateInput(parsedSlug)
|
|
||||||
|
if (length >= 2 && slug.length <= 3) {
|
||||||
|
const parsedSlug = slug.map((e) => {
|
||||||
|
return parseInt(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const newDate = validateDateInput(parsedSlug);
|
||||||
|
|
||||||
|
if (newDate.year === 0 || newDate.month === 0 || newDate.day === 0) {
|
||||||
|
setError(true);
|
||||||
|
} else {
|
||||||
|
// const slugDate = new Date(
|
||||||
|
// newDate.year,
|
||||||
|
// newDate.month - 1,
|
||||||
|
// newDate.day
|
||||||
|
// );
|
||||||
|
// console.info("Slug date:", slugDate);
|
||||||
|
// validateDateRange(slugDate);
|
||||||
|
|
||||||
|
setDate({
|
||||||
|
...validateDateInput(parsedSlug)
|
||||||
|
});
|
||||||
|
2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [slug]);
|
}, [slug]);
|
||||||
|
|||||||
1
types/CalenderContext.d.ts
vendored
1
types/CalenderContext.d.ts
vendored
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user