Date align #23
@@ -3,11 +3,11 @@ 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 } from "date-fns";
|
import { sub, add, format } from "date-fns";
|
||||||
import { CalenderContext } from "../../contexts/CalenderContext";
|
|
||||||
import DatePicker from "./DatePicker";
|
import DatePicker from "./DatePicker";
|
||||||
|
import { NewCalenderContext } from "../../contexts/NewCalenderContext";
|
||||||
|
|
||||||
const CalenderNav = (): JSX.Element => {
|
const CalenderNav = (): JSX.Element => {
|
||||||
const { selectedDate } = useContext(CalenderContext);
|
const { selectedDate } = useContext(NewCalenderContext);
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ import {
|
|||||||
FieldProps
|
FieldProps
|
||||||
} from "formik";
|
} from "formik";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
import { CalenderContext } from "../../contexts/CalenderContext";
|
|
||||||
import FormValidateEmoji from "./FormValidateEmoji";
|
import FormValidateEmoji from "./FormValidateEmoji";
|
||||||
|
import { NewCalenderContext } from "../../contexts/NewCalenderContext";
|
||||||
|
|
||||||
interface UpdateCalendarProps {
|
interface UpdateCalendarProps {
|
||||||
year: number;
|
year: number;
|
||||||
@@ -35,9 +35,7 @@ interface UpdateCalendarProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const DatePicker = (): JSX.Element => {
|
const DatePicker = (): JSX.Element => {
|
||||||
const { selectedDate } = useContext(CalenderContext);
|
const { title } = useContext(NewCalenderContext);
|
||||||
|
|
||||||
const currentMonth = format(selectedDate, "LLLL uuuu");
|
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
@@ -134,7 +132,7 @@ const DatePicker = (): JSX.Element => {
|
|||||||
<PopoverTrigger>
|
<PopoverTrigger>
|
||||||
<Button border="none" variant="outline">
|
<Button border="none" variant="outline">
|
||||||
<Heading w="100%" h="auto">
|
<Heading w="100%" h="auto">
|
||||||
{currentMonth}
|
{title}
|
||||||
</Heading>
|
</Heading>
|
||||||
</Button>
|
</Button>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import React, { useContext, useEffect } from "react";
|
import React, { useContext, useEffect } from "react";
|
||||||
import { Box, HStack, SimpleGrid, Text, VStack } from "@chakra-ui/react";
|
import { Box, HStack, SimpleGrid, Text, VStack } from "@chakra-ui/react";
|
||||||
import CalenderNav from "./CalenderNav";
|
import CalenderNav from "./CalenderNav";
|
||||||
import { CalenderContext } from "../../contexts/CalenderContext";
|
|
||||||
import { NewCalenderContext } from "../../contexts/NewCalenderContext";
|
import { NewCalenderContext } from "../../contexts/NewCalenderContext";
|
||||||
import { getDate } from "date-fns";
|
import { getDate } from "date-fns";
|
||||||
// TODO: import types
|
// TODO: import types
|
||||||
@@ -13,20 +12,19 @@ interface UpdateCalendarProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
|
const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
|
||||||
const { daysOfMonth, daysOfWeek, setDate } = useContext(CalenderContext);
|
const { layout, updateDate } = useContext(NewCalenderContext);
|
||||||
const { layout } = useContext(NewCalenderContext);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (newDate) {
|
if (newDate) {
|
||||||
const { year, month, day } = newDate;
|
const { year, month, day } = newDate;
|
||||||
|
|
||||||
if (year > 0 && month > 0 && day > 0) {
|
if (year > 0 && month > 0 && day > 0) {
|
||||||
setDate(newDate);
|
updateDate(newDate);
|
||||||
} else {
|
} else {
|
||||||
console.warn("Invalid date format: ", newDate);
|
console.warn("Invalid date format: ", newDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [daysOfMonth, daysOfWeek, newDate, setDate]);
|
}, [newDate, updateDate]);
|
||||||
|
|
||||||
// Simulated user settings context
|
// Simulated user settings context
|
||||||
const userSettings = {
|
const userSettings = {
|
||||||
@@ -39,7 +37,6 @@ const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<VStack h="100vh" w="100%">
|
<VStack h="100vh" w="100%">
|
||||||
{/* <NewContext /> */}
|
|
||||||
<CalenderNav />
|
<CalenderNav />
|
||||||
<HStack
|
<HStack
|
||||||
px={6}
|
px={6}
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ import {
|
|||||||
startOfMonth,
|
startOfMonth,
|
||||||
endOfMonth,
|
endOfMonth,
|
||||||
getDate,
|
getDate,
|
||||||
|
getMonth,
|
||||||
|
getYear,
|
||||||
add,
|
add,
|
||||||
sub,
|
sub,
|
||||||
set,
|
set,
|
||||||
isAfter,
|
isAfter,
|
||||||
isBefore
|
isBefore,
|
||||||
|
compareAsc
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
// TODO: import types
|
// TODO: import types
|
||||||
|
|
||||||
@@ -62,10 +65,17 @@ interface MonthContext extends MonthInfo {
|
|||||||
layout: MonthLayout;
|
layout: MonthLayout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface UpdateCalendarProps {
|
||||||
|
year: number;
|
||||||
|
month: number;
|
||||||
|
day: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface CalenderContextState {
|
interface CalenderContextState {
|
||||||
selectedDate: Date;
|
selectedDate: Date;
|
||||||
title: string;
|
title: string;
|
||||||
layout: MonthLayout;
|
layout: MonthLayout;
|
||||||
|
updateDate: (input: UpdateCalendarProps) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NewCalenderContext = createContext({} as CalenderContextState);
|
const NewCalenderContext = createContext({} as CalenderContextState);
|
||||||
@@ -163,10 +173,10 @@ const NewCalenderContextProvider = ({
|
|||||||
date: sunStartDay
|
date: sunStartDay
|
||||||
});
|
});
|
||||||
|
|
||||||
for (let week in sundays) {
|
for (const week in sundays) {
|
||||||
const thisWeek = sundays[week];
|
const thisWeek = sundays[week];
|
||||||
|
|
||||||
thisWeek.forEach((e, i, a) => {
|
thisWeek.forEach((e, i) => {
|
||||||
const day: MonthDay = {
|
const day: MonthDay = {
|
||||||
isOverflow: isOverflow(selectedDate, sunCurrDate),
|
isOverflow: isOverflow(selectedDate, sunCurrDate),
|
||||||
date: sunCurrDate
|
date: sunCurrDate
|
||||||
@@ -194,10 +204,10 @@ const NewCalenderContextProvider = ({
|
|||||||
date: monStartDay
|
date: monStartDay
|
||||||
});
|
});
|
||||||
|
|
||||||
for (let week in mondays) {
|
for (const week in mondays) {
|
||||||
const thisWeek = mondays[week];
|
const thisWeek = mondays[week];
|
||||||
|
|
||||||
thisWeek.forEach((e, i, a) => {
|
thisWeek.forEach((e, i) => {
|
||||||
const day: MonthDay = {
|
const day: MonthDay = {
|
||||||
isOverflow: isOverflow(selectedDate, monCurrDate),
|
isOverflow: isOverflow(selectedDate, monCurrDate),
|
||||||
date: monCurrDate
|
date: monCurrDate
|
||||||
@@ -224,33 +234,47 @@ const NewCalenderContextProvider = ({
|
|||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO Add output typing and move the invocation into the monthInfo state, removing any unended info from the state.
|
const [selectedDate, setSelectedDate] = useState<Date>(new Date());
|
||||||
|
|
||||||
// populateMonth(
|
|
||||||
// selectedDate,
|
|
||||||
// format(startOfMonth(selectedDate), "iii"),
|
|
||||||
// selectedMonthInfo.prevMonth
|
|
||||||
// );
|
|
||||||
|
|
||||||
const [selectedDate, setSelectedMonth] = useState<Date>(new Date());
|
|
||||||
const [selectedDateInfo, setSelectedMonthInfo] = useState<MonthContext>({
|
const [selectedDateInfo, setSelectedMonthInfo] = useState<MonthContext>({
|
||||||
date: selectedDate,
|
date: selectedDate,
|
||||||
title: format(selectedDate, "LLLL uuuu"),
|
title: format(selectedDate, "LLLL uuuu"),
|
||||||
layout: populateMonth(selectedDate)
|
layout: populateMonth(selectedDate)
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO: Update the MonthInfo to use the new month population function on first render.
|
//TODO Update the MonthInfo to use the new month population function on first render.
|
||||||
|
|
||||||
|
//TODO Add a function that will update the MonthInfo state when the selected month changes. This should use the populate month function that will be made above.
|
||||||
|
const updateDateInfo = (newDate: Date) => {
|
||||||
|
const output = { ...selectedDateInfo };
|
||||||
|
output.date = newDate;
|
||||||
|
output.title = format(newDate, "LLLL uuuu");
|
||||||
|
output.layout = populateMonth(newDate);
|
||||||
|
|
||||||
|
setSelectedMonthInfo(output);
|
||||||
|
};
|
||||||
|
|
||||||
//TODO: Add a new navigation function that will take in either a direction (next, prev) or a date to go directly to. That will update the selected month and trigger the use effects below.
|
//TODO: Add a new navigation function that will take in either a direction (next, prev) or a date to go directly to. That will update the selected month and trigger the use effects below.
|
||||||
|
const updateDate = (input: UpdateCalendarProps) => {
|
||||||
|
const { year, month: inputMonth, day } = input;
|
||||||
|
|
||||||
//TODO: Add a function that will update the MonthInfo state when the selected month changes. This should use the populate month function that will be made above.
|
if (!year || !inputMonth || day < 0 || day > 31) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
const month = inputMonth - 1;
|
||||||
|
const customDate: Date = new Date(year, month, day);
|
||||||
|
|
||||||
//TODO: Add a useEffect that will trigger the update function(s) to run when the selected date is updated.
|
if (compareAsc(customDate, selectedDate) !== 0) {
|
||||||
|
setSelectedDate(customDate);
|
||||||
|
updateDateInfo(customDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const calenderContextValues = {
|
const calenderContextValues = {
|
||||||
selectedDate: selectedDate,
|
selectedDate,
|
||||||
title: selectedDateInfo.title,
|
title: selectedDateInfo.title,
|
||||||
layout: selectedDateInfo.layout
|
layout: selectedDateInfo.layout,
|
||||||
|
updateDate
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { useRouter } from "next/router";
|
|||||||
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";
|
||||||
|
import { NewCalenderContextProvider } from "../../contexts/NewCalenderContext";
|
||||||
|
|
||||||
interface UpdateCalendarProps {
|
interface UpdateCalendarProps {
|
||||||
year: number;
|
year: number;
|
||||||
@@ -71,7 +72,9 @@ const DateRoute: React.FC<unknown> = () => {
|
|||||||
return (
|
return (
|
||||||
<Box textAlign="center" w="100%" h="auto" pt="50px" pb="10vh">
|
<Box textAlign="center" w="100%" h="auto" pt="50px" pb="10vh">
|
||||||
<CalenderContextProvider>
|
<CalenderContextProvider>
|
||||||
<Calender {...date} />
|
<NewCalenderContextProvider>
|
||||||
|
<Calender {...date} />
|
||||||
|
</NewCalenderContextProvider>
|
||||||
</CalenderContextProvider>
|
</CalenderContextProvider>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -84,8 +84,8 @@ const Header = (): JSX.Element => {
|
|||||||
open
|
open
|
||||||
? "brand.main"
|
? "brand.main"
|
||||||
: transparentNavbar
|
: transparentNavbar
|
||||||
? "rgba(49, 56, 220, 0.9)"
|
? "rgba(49, 56, 220, 0.9)"
|
||||||
: "brand.main"
|
: "brand.main"
|
||||||
}
|
}
|
||||||
transition=".5s ease"
|
transition=".5s ease"
|
||||||
borderRadius="0px 0px 10px 10px"
|
borderRadius="0px 0px 10px 10px"
|
||||||
|
|||||||
Reference in New Issue
Block a user