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

View File

@@ -1,9 +1,9 @@
import React, { useContext, useEffect } from "react"; import React, { useContext, useEffect } from "react";
import { Box, HStack, SimpleGrid, Text, VStack } from "@chakra-ui/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 { CalenderContext } from "../../contexts/CalenderContext";
import { StickersContext } from "../../contexts/StickerContext"; import { StickersContext } from "../../contexts/StickerContext";
import { isSameDay } from "date-fns"; import CalenderNav from "./CalenderNav";
import Day from "./Day"; import Day from "./Day";
const Calender = (newDate?: UpdateCalendarProps): JSX.Element => { const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
@@ -28,15 +28,12 @@ const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
startOfWeek: "Sunday" startOfWeek: "Sunday"
}; };
const currMonth = layout[`${userSettings.startOfWeek.toLowerCase()}`]; const currMonth: WeekLayout =
layout[`${userSettings.startOfWeek.toLowerCase()}`];
const { month, weekdays } = currMonth; const { month, weekdays } = currMonth;
// TODO: Move the weekdays into it's own component for responsiveness. // TODO: Move the weekdays into it's own component for responsiveness.
useEffect(() => {
console.log("Stickers month updated");
}, [stickersMonth]);
return ( return (
<VStack h="91vh" w="100%"> <VStack h="91vh" w="100%">
<CalenderNav /> <CalenderNav />
@@ -94,7 +91,12 @@ const Calender = (newDate?: UpdateCalendarProps): JSX.Element => {
sticker={sticker} sticker={sticker}
date={date} date={date}
selectedDate={selectedDate} 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; isOpen: boolean;
updateIsOpen: React.Dispatch<React.SetStateAction<boolean>>; updateIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
date: Date; 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 {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 {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 {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 = ({ const AddSticker = ({
isOpen, isOpen,
updateIsOpen, updateIsOpen,
date date,
updateSticker
}: AddStickerProps): JSX.Element => { }: AddStickerProps): JSX.Element => {
// TODO: Import the stickers array from the calender context. // TODO: Import the stickers array from the calender context.
@@ -51,13 +54,13 @@ const AddSticker = ({
const handleClose = () => { const handleClose = () => {
setSelectedSticker(null); setSelectedSticker(null);
updateIsOpen(false); updateIsOpen(false);
}
const handleSubmit = (sticker) => {
addEditSticker(date, sticker);
handleClose();
}; };
const handleSubmit = (sticker) => {
const newSticker: Sticker = addEditSticker(date, sticker);
updateSticker(newSticker.sticker);
handleClose();
};
// useEffect(() => { // useEffect(() => {
// if (selectedSticker !== null) { // if (selectedSticker !== null) {

View File

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

View File

@@ -14,8 +14,8 @@ 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 addEditSticker = (date: Date, sticker: ValidStickerVal): Sticker => {
const newStickersMonth = [...stickersMonth]; const newStickersMonth = stickersMonth.slice();
const index = getDate(date) - 1; const index = getDate(date) - 1;
const currDate = newStickersMonth[index]; const currDate = newStickersMonth[index];
@@ -35,11 +35,13 @@ const StickersContextProvider = ({
sticker: sticker, sticker: sticker,
edited: edited, edited: edited,
manual: false manual: false
} };
newStickersMonth[index] = newSticker; newStickersMonth[index] = newSticker;
setStickersMonth(newStickersMonth); setStickersMonth(newStickersMonth.slice());
return newSticker;
}; };
// TODO: Add stickers validation function here. // TODO: Add stickers validation function here.

View File

@@ -34,15 +34,14 @@ interface MonthInfo {
title: string; title: string;
} }
interface WeekLayout {
weekdays: DaysOfWeek;
month: Month;
}
interface MonthLayout { interface MonthLayout {
sunday: { sunday: WeekLayout;
weekdays: DaysOfWeek; monday: WeekLayout;
month: Month;
};
monday: {
weekdays: DaysOfWeek;
month: Month;
};
} }
interface MonthContext extends MonthInfo { interface MonthContext extends MonthInfo {

View File

@@ -1,6 +1,6 @@
interface StickersContextState { interface StickersContextState {
stickersMonth: StickerDays; stickersMonth: StickerDays;
addEditSticker: (date: Date, sticker: ValidStickerVal) => void; addEditSticker: (date: Date, sticker: ValidStickerVal) => Sticker;
} }
type StickerVal = -2 | -1 | 0 | 1 | 2 | null; type StickerVal = -2 | -1 | 0 | 1 | 2 | null;