Stickers now updating in local state, the context state is not.
This commit is contained in:
@@ -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"
|
||||||
|
>
|
||||||
|
<DemoStickers stickerVal={stickerState} />
|
||||||
</Box>
|
</Box>
|
||||||
) : (
|
|
||||||
<Box fontSize="1.5rem">
|
|
||||||
<span aria-label="spacer"> </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"
|
||||||
|
>
|
||||||
|
<DemoStickers stickerVal={stickerState} />
|
||||||
</Box>
|
</Box>
|
||||||
) : (
|
|
||||||
<Box fontSize="1.5rem">
|
|
||||||
<span aria-label="spacer"> </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>
|
||||||
|
|||||||
@@ -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}`
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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"> </span>;
|
||||||
|
}
|
||||||
interface StickerToEmoji {
|
interface StickerToEmoji {
|
||||||
[key: string]: JSX.Element;
|
[key: string]: JSX.Element;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
15
types/CalenderContext.d.ts
vendored
15
types/CalenderContext.d.ts
vendored
@@ -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 {
|
||||||
|
|||||||
2
types/StickerContext.d.ts
vendored
2
types/StickerContext.d.ts
vendored
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user