Stickers now updating in local state, the context state is not.

This commit is contained in:
Lucid Kobold
2022-02-10 01:57:54 -06:00
parent af57b29e2d
commit 609882fd1e
7 changed files with 51 additions and 45 deletions

View File

@@ -61,6 +61,8 @@ const Day = ({
// This handles the modal for this date.
const [isOpen, setIsOpen] = useState<boolean>(false);
const [stickerState, setStickerState] = useState<StickerVal>(sticker);
/**
* TODO: Add logic to remove the onClick within overflow dates.
* Do not give dates for the next month an onClick.
@@ -96,15 +98,12 @@ const Day = ({
<Text w="auto" h="auto">
{`${getDate(date)}`}
</Text>
{sticker !== null ? (
<Box fontSize="1.5rem">
<DemoStickers stickerVal={sticker} />
</Box>
) : (
<Box fontSize="1.5rem">
<span aria-label="spacer">&nbsp;</span>
</Box>
)}
<Box
key={stickerState === null ? Math.random() : stickerState}
fontSize="1.5rem"
>
<DemoStickers stickerVal={stickerState} />
</Box>
</VStack>
)}
{!isOverflow && (
@@ -140,21 +139,19 @@ const Day = ({
>
{`${getDate(date)}`}
</Text>
{sticker !== null ? (
<Box fontSize="1.5rem">
<DemoStickers stickerVal={sticker} />
</Box>
) : (
<Box fontSize="1.5rem">
<span aria-label="spacer">&nbsp;</span>
</Box>
)}
<Box
key={stickerState === null ? Math.random() : stickerState}
fontSize="1.5rem"
>
<DemoStickers stickerVal={stickerState} />
</Box>
<StickersContextProvider>
{isBefore(date, endOfDay(new Date())) && (
<AddSticker
date={date}
isOpen={isOpen}
updateIsOpen={setIsOpen}
updateSticker={setStickerState}
/>
)}
</StickersContextProvider>

View File

@@ -1,9 +1,9 @@
import React, { useContext, useEffect } from "react";
import { Box, HStack, SimpleGrid, Text, VStack } from "@chakra-ui/react";
import CalenderNav from "./CalenderNav";
import { isSameDay, format } from "date-fns";
import { CalenderContext } from "../../contexts/CalenderContext";
import { StickersContext } from "../../contexts/StickerContext";
import { isSameDay } from "date-fns";
import CalenderNav from "./CalenderNav";
import Day from "./Day";
const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
@@ -28,15 +28,12 @@ const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
startOfWeek: "Sunday"
};
const currMonth = layout[`${userSettings.startOfWeek.toLowerCase()}`];
const currMonth: WeekLayout =
layout[`${userSettings.startOfWeek.toLowerCase()}`];
const { month, weekdays } = currMonth;
// TODO: Move the weekdays into it's own component for responsiveness.
useEffect(() => {
console.log("Stickers month updated");
}, [stickersMonth]);
return (
<VStack h="91vh" w="100%">
<CalenderNav />
@@ -94,7 +91,12 @@ const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
sticker={sticker}
date={date}
selectedDate={selectedDate}
key={id}
key={
id.length
? id
: format(date, "yyyyddLL") +
`/${sticker === null ? 0 : sticker}`
}
/>
);
});

View File

@@ -20,6 +20,7 @@ interface AddStickerProps {
isOpen: boolean;
updateIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
date: Date;
updateSticker: React.Dispatch<React.SetStateAction<StickerVal>>;
}
/**
@@ -28,11 +29,13 @@ interface AddStickerProps {
* @param {boolean} props.isOpen tells the component when the modal should be open.
* @param {React.Dispatch<React.SetStateAction<boolean>>} props.updateIsOpen used to close the modal.
* @param {date} props.date the date for which the sticker will be added or modified.
* @param {React.Dispatch<React.SetStateAction<StickerVal>>} updateSticker the react state function to update the sticker.
*/
const AddSticker = ({
isOpen,
updateIsOpen,
date
date,
updateSticker
}: AddStickerProps): JSX.Element => {
// TODO: Import the stickers array from the calender context.
@@ -51,13 +54,13 @@ const AddSticker = ({
const handleClose = () => {
setSelectedSticker(null);
updateIsOpen(false);
}
const handleSubmit = (sticker) => {
addEditSticker(date, sticker);
handleClose();
};
const handleSubmit = (sticker) => {
const newSticker: Sticker = addEditSticker(date, sticker);
updateSticker(newSticker.sticker);
handleClose();
};
// useEffect(() => {
// if (selectedSticker !== null) {

View File

@@ -9,6 +9,9 @@ interface DemoStickersProps {
const DemoStickers: FC<DemoStickersProps> = ({
stickerVal
}: DemoStickersProps) => {
if (stickerVal === null) {
return <span aria-label="spacer">&nbsp;</span>;
}
interface StickerToEmoji {
[key: string]: JSX.Element;
}