Mondays and sundays populating correctly in the new populateDays function.

This commit is contained in:
Lucid Kobold
2021-12-28 16:01:18 -06:00
parent 13d643fa56
commit 74fcc950c6

View File

@@ -4,8 +4,11 @@ import {
startOfMonth, startOfMonth,
endOfMonth, endOfMonth,
getDate, getDate,
add,
sub, sub,
compareAsc, set,
isAfter,
isBefore
} from "date-fns"; } from "date-fns";
// TODO: import types // TODO: import types
@@ -97,7 +100,6 @@ const NewCalenderContextProvider = ({
}; };
const ISOToIndex = { const ISOToIndex = {
startOfWeek: {
sunday: { sunday: {
Sun: 0, Sun: 0,
Mon: 1, Mon: 1,
@@ -108,14 +110,13 @@ const NewCalenderContextProvider = ({
Sat: 6, Sat: 6,
}, },
monday: { monday: {
Mon: 0, Mon: -1,
Tue: 1, Tue: 0,
Wed: 2, Wed: 1,
Thu: 3, Thu: 2,
Fri: 4, Fri: 3,
Sat: 5, Sat: 4,
Sun: 6, Sun: 5,
},
}, },
}; };
@@ -148,6 +149,97 @@ 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) => {
let flag = false;
const start = startOfMonth(selectedDate);
const end = endOfMonth(selectedDate);
if (isBefore(currDate, start) || isAfter(currDate, end)) {
flag = true;
}
return flag;
}
const populateMonth = (
selectedDate: Date,
startOfMonth: string,
prevMonth: {
date: Date;
endDay: number;
days: number;
}
): void => {
const sundays = {
week1: new Array(7).fill(null),
week2: new Array(7).fill(null),
week3: new Array(7).fill(null),
week4: new Array(7).fill(null),
week5: new Array(7).fill(null),
week6: new Array(7).fill(null),
};
const sunStartDay =
prevMonth.endDay - (ISOToIndex.sunday[startOfMonth] - 1);
let sunCurrDate = set(sub(selectedDate, { months: 1 }), {
date: sunStartDay,
});
for (let week in sundays) {
const thisWeek = sundays[week];
thisWeek.forEach((e, i, a) => {
const day = {
isOverflow: isOverflow(selectedDate, sunCurrDate),
date: sunCurrDate,
};
sunCurrDate = add(sunCurrDate, { days: 1 })
sundays[week][i] = day;
});
}
const mondays = {
week1: new Array(7).fill(null),
week2: new Array(7).fill(null),
week3: new Array(7).fill(null),
week4: new Array(7).fill(null),
week5: new Array(7).fill(null),
week6: new Array(7).fill(null),
};
const monStartDay =
prevMonth.endDay - (ISOToIndex.monday[startOfMonth]);
// console.log(`Start date: ${monStartDay}`);
let monCurrDate = set(sub(selectedDate, { months: 1 }), {
date: monStartDay,
});
// console.log(`Start date: ${currDate}`);
for (let week in mondays) {
const thisWeek = mondays[week];
thisWeek.forEach((e, i, a) => {
const day = {
isOverflow: isOverflow(selectedDate, monCurrDate),
date: monCurrDate,
};
monCurrDate = add(monCurrDate, { days: 1 })
mondays[week][i] = day;
});
}
console.log("mondays after loop and map", mondays);
};
populateMonth(
selectedDate,
format(startOfMonth(selectedDate), "iii"),
selectedMonthInfo.prevMonth
);
//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 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.