Fix sticker seeder.
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
|||||||
isBefore,
|
isBefore,
|
||||||
compareAsc
|
compareAsc
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
|
import stickersSeeder from "../data/stickerSeeder";
|
||||||
|
|
||||||
const CalenderContext = createContext({} as CalenderContextState);
|
const CalenderContext = createContext({} as CalenderContextState);
|
||||||
|
|
||||||
@@ -40,8 +41,6 @@ const CalenderContextProvider = ({
|
|||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
//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.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Using date-fns, this function checks if currDate is within the month of selectedDate or not.
|
* Using date-fns, this function checks if currDate is within the month of selectedDate or not.
|
||||||
* @param {Date} selectedDate The current month.
|
* @param {Date} selectedDate The current month.
|
||||||
@@ -74,6 +73,10 @@ const CalenderContextProvider = ({
|
|||||||
return { isOverflow: flag, overflowDirection: direction };
|
return { isOverflow: flag, overflowDirection: direction };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const [stickersMonth, setStickersMonth] = useState<StickerDays>(
|
||||||
|
stickersSeeder()
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function that will return a month layout when given a date. It produces
|
* A function that will return a month layout when given a date. It produces
|
||||||
* an object with 6 weeks that include overflow from the previous and next month
|
* an object with 6 weeks that include overflow from the previous and next month
|
||||||
@@ -84,6 +87,9 @@ const CalenderContextProvider = ({
|
|||||||
const endLastMonth = getDate(endOfMonth(sub(selectedDate, { months: 1 })));
|
const endLastMonth = getDate(endOfMonth(sub(selectedDate, { months: 1 })));
|
||||||
const startOfSelectedMonth = format(startOfMonth(selectedDate), "iii");
|
const startOfSelectedMonth = format(startOfMonth(selectedDate), "iii");
|
||||||
|
|
||||||
|
// console.log(stickersMonth);
|
||||||
|
console.log(stickersSeeder());
|
||||||
|
|
||||||
const ISOToIndex = {
|
const ISOToIndex = {
|
||||||
sunday: {
|
sunday: {
|
||||||
Sun: 0,
|
Sun: 0,
|
||||||
@@ -129,8 +135,10 @@ const CalenderContextProvider = ({
|
|||||||
|
|
||||||
const day: MonthDay = {
|
const day: MonthDay = {
|
||||||
...overflowInfo,
|
...overflowInfo,
|
||||||
date: sunCurrDate
|
date: sunCurrDate,
|
||||||
|
// sticker: stickersMonth[getDate(sunCurrDate)].sticker
|
||||||
};
|
};
|
||||||
|
|
||||||
sunCurrDate = add(sunCurrDate, {
|
sunCurrDate = add(sunCurrDate, {
|
||||||
days: 1
|
days: 1
|
||||||
});
|
});
|
||||||
@@ -162,8 +170,10 @@ const CalenderContextProvider = ({
|
|||||||
|
|
||||||
const day: MonthDay = {
|
const day: MonthDay = {
|
||||||
...overflowInfo,
|
...overflowInfo,
|
||||||
date: monCurrDate
|
date: monCurrDate,
|
||||||
|
// sticker: stickersMonth[getDate(monCurrDate)].sticker
|
||||||
};
|
};
|
||||||
|
|
||||||
monCurrDate = add(monCurrDate, {
|
monCurrDate = add(monCurrDate, {
|
||||||
days: 1
|
days: 1
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import { getDaysInMonth, getYear, getMonth, isBefore } from "date-fns";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const generateSticker = (): -2 | -1 | 0 | 1 | 2 => {
|
const generateSticker = (): -2 | -1 | 0 | 1 | 2 => {
|
||||||
const stickerPossibility = [-2, -1, 0, 1, 2];
|
const sticker = Math.floor(Math.random() * (2 - -2 + 1)) + -2;
|
||||||
const sticker = Math.floor(Math.random() * stickerPossibility.length);
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
sticker === -2 ||
|
sticker === -2 ||
|
||||||
@@ -21,14 +20,14 @@ const generateSticker = (): -2 | -1 | 0 | 1 | 2 => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const stickersSeeder = (): Sticker[] => {
|
const stickersSeeder = (): StickerDays => {
|
||||||
const stickers = [] as Sticker[];
|
const stickers = [] as Sticker[];
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const daysOfThisMonth = getDaysInMonth(now);
|
const daysOfThisMonth = getDaysInMonth(now);
|
||||||
|
|
||||||
for (let i = 0; i <= daysOfThisMonth; i++) {
|
for (let i = 1; i <= daysOfThisMonth; i++) {
|
||||||
const currDate = new Date(getYear(now), getMonth(now) - 1, i);
|
const currDate = new Date(getYear(now), getMonth(now), i);
|
||||||
|
|
||||||
const newSticker: Sticker = {
|
const newSticker: Sticker = {
|
||||||
date: currDate,
|
date: currDate,
|
||||||
|
|||||||
4
types/CalenderContext.d.ts
vendored
4
types/CalenderContext.d.ts
vendored
@@ -19,6 +19,8 @@ interface Sticker {
|
|||||||
sticker: -2 | -1 | 0 | 1 | 2 | null;
|
sticker: -2 | -1 | 0 | 1 | 2 | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StickerDays = Sticker[];
|
||||||
|
|
||||||
interface MonthSticker {
|
interface MonthSticker {
|
||||||
date: Date;
|
date: Date;
|
||||||
month: Sticker[];
|
month: Sticker[];
|
||||||
@@ -69,4 +71,4 @@ interface CalenderContextState {
|
|||||||
title: string;
|
title: string;
|
||||||
layout: MonthLayout;
|
layout: MonthLayout;
|
||||||
updateDate: (input: UpdateCalendarProps) => void;
|
updateDate: (input: UpdateCalendarProps) => void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user