Destructring, comments, and typing.

This commit is contained in:
Lucid Kobold
2021-12-28 16:08:45 -06:00
parent 74fcc950c6
commit c56dbb96d4

View File

@@ -8,7 +8,7 @@ import {
sub, sub,
set, set,
isAfter, isAfter,
isBefore isBefore,
} from "date-fns"; } from "date-fns";
// TODO: import types // TODO: import types
@@ -147,9 +147,10 @@ const NewCalenderContextProvider = ({
}, },
}); });
//TODO: Add a function that will populate the "MONTH" layout for the context. It should take in the start of the week (Sunday, Monday) and output the appropriate layout based on that preference. //TODO Add a function that will populate the "MONTH" layout for the context. It should take in the start of the week (Sunday, Monday) and output the appropriate layout based on that preference.
const isOverflow = (selectedDate: Date, currDate: Date) => { // Checks if the date is before or after the current month.
const isOverflow = (selectedDate: Date, currDate: Date): boolean => {
let flag = false; let flag = false;
const start = startOfMonth(selectedDate); const start = startOfMonth(selectedDate);
const end = endOfMonth(selectedDate); const end = endOfMonth(selectedDate);
@@ -159,17 +160,18 @@ const NewCalenderContextProvider = ({
} }
return flag; return flag;
} };
// Populates the month.
const populateMonth = ( const populateMonth = (
selectedDate: Date, selectedDate: Date,
startOfMonth: string, startOfMonth: string,
prevMonth: { prevMonth: {
date: Date;
endDay: number; endDay: number;
days: number;
} }
): void => { ): void => {
const { endDay: endLastMonth } = prevMonth;
const sundays = { const sundays = {
week1: new Array(7).fill(null), week1: new Array(7).fill(null),
week2: new Array(7).fill(null), week2: new Array(7).fill(null),
@@ -179,8 +181,7 @@ const NewCalenderContextProvider = ({
week6: new Array(7).fill(null), week6: new Array(7).fill(null),
}; };
const sunStartDay = const sunStartDay = endLastMonth - (ISOToIndex.sunday[startOfMonth] - 1);
prevMonth.endDay - (ISOToIndex.sunday[startOfMonth] - 1);
let sunCurrDate = set(sub(selectedDate, { months: 1 }), { let sunCurrDate = set(sub(selectedDate, { months: 1 }), {
date: sunStartDay, date: sunStartDay,
@@ -194,7 +195,7 @@ const NewCalenderContextProvider = ({
isOverflow: isOverflow(selectedDate, sunCurrDate), isOverflow: isOverflow(selectedDate, sunCurrDate),
date: sunCurrDate, date: sunCurrDate,
}; };
sunCurrDate = add(sunCurrDate, { days: 1 }) sunCurrDate = add(sunCurrDate, { days: 1 });
sundays[week][i] = day; sundays[week][i] = day;
}); });
@@ -209,14 +210,11 @@ const NewCalenderContextProvider = ({
week6: new Array(7).fill(null), week6: new Array(7).fill(null),
}; };
const monStartDay = const monStartDay = endLastMonth - ISOToIndex.monday[startOfMonth];
prevMonth.endDay - (ISOToIndex.monday[startOfMonth]);
// console.log(`Start date: ${monStartDay}`);
let monCurrDate = set(sub(selectedDate, { months: 1 }), { let monCurrDate = set(sub(selectedDate, { months: 1 }), {
date: monStartDay, date: monStartDay,
}); });
// console.log(`Start date: ${currDate}`);
for (let week in mondays) { for (let week in mondays) {
const thisWeek = mondays[week]; const thisWeek = mondays[week];
@@ -226,14 +224,15 @@ const NewCalenderContextProvider = ({
isOverflow: isOverflow(selectedDate, monCurrDate), isOverflow: isOverflow(selectedDate, monCurrDate),
date: monCurrDate, date: monCurrDate,
}; };
monCurrDate = add(monCurrDate, { days: 1 }) monCurrDate = add(monCurrDate, { days: 1 });
mondays[week][i] = day; mondays[week][i] = day;
}); });
} }
console.log("mondays after loop and map", mondays);
}; };
//TODO: Add output typing and move the invocation into the monthInfo state, removing any unended info from the state.
populateMonth( populateMonth(
selectedDate, selectedDate,
format(startOfMonth(selectedDate), "iii"), format(startOfMonth(selectedDate), "iii"),