Add addEditSticker fuction. Still not fully working.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import React, { createContext, useState, ReactNode } from "react";
|
import React, { createContext, useState, ReactNode } from "react";
|
||||||
|
import { format, getDate, isBefore, startOfDay } from "date-fns";
|
||||||
import stickersSeeder from "../data/stickerSeeder";
|
import stickersSeeder from "../data/stickerSeeder";
|
||||||
|
|
||||||
const StickersContext = createContext({} as StickersContextState);
|
const StickersContext = createContext({} as StickersContextState);
|
||||||
@@ -13,11 +14,39 @@ const StickersContextProvider = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Add stickers functions here. (Add and edit stickers).
|
// TODO: Add stickers functions here. (Add and edit stickers).
|
||||||
|
const addEditSticker = (date: Date, sticker: ValidStickerVal): void => {
|
||||||
|
const newStickersMonth = [...stickersMonth];
|
||||||
|
const index = getDate(date) - 1;
|
||||||
|
const currDate = newStickersMonth[index];
|
||||||
|
|
||||||
|
const edited = currDate.edited
|
||||||
|
? true
|
||||||
|
: isBefore(currDate.date, startOfDay(new Date()))
|
||||||
|
? true
|
||||||
|
: false;
|
||||||
|
currDate.edited = edited;
|
||||||
|
// Add manual here when necessary.
|
||||||
|
|
||||||
|
const id = format(date, "yyyyddLL") + sticker;
|
||||||
|
|
||||||
|
const newSticker: Sticker = {
|
||||||
|
id: id,
|
||||||
|
date: date,
|
||||||
|
sticker: sticker,
|
||||||
|
edited: edited,
|
||||||
|
manual: false
|
||||||
|
}
|
||||||
|
|
||||||
|
newStickersMonth[index] = newSticker;
|
||||||
|
|
||||||
|
setStickersMonth(newStickersMonth);
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: Add stickers validation function here.
|
// TODO: Add stickers validation function here.
|
||||||
|
|
||||||
const stickersContextValues = {
|
const stickersContextValues = {
|
||||||
stickersMonth
|
stickersMonth,
|
||||||
|
addEditSticker
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user