import { Button, Modal, ModalOverlay, ModalContent, ModalHeader, ModalBody, ModalFooter, Heading, HStack, Text, VStack, SimpleGrid, Box } from "@chakra-ui/react"; import React, { useState, useContext, useRef } from "react"; import { format, isSameDay } from "date-fns"; import { Icon } from "@iconify/react"; import { StickersContext } from "../../../contexts/StickerContext"; import StickerSelector from "./StickerSelector"; import DemoStickers from "../stickers/DemoStickers"; interface AddStickerProps { isOpen: boolean; updateIsOpen: React.Dispatch>; date: Date; updateSticker: React.Dispatch>; currSticker: StickerVal; step: number; updateStep: React.Dispatch>; selectedSticker: StickerVal; updateSelectedSticker: React.Dispatch>; currDate: Date; } /** * Handles adding and modifying the stickers for the given month. * @param {boolean} isOpen Tells the component when the modal should be open. * @param {React.Dispatch>} updateIsOpen Used to close the modal. * @param {date} date The date for which the sticker will be added or modified. * @param {React.Dispatch>} updateSticker The react state function to update the sticker. * @param {StickerVal} currSticker The current sticker for the date. * @param {number} step A numerical variable that represents the page the modal should be at. * @param {React.Dispatch>} updateStep Used to navigate the pages of the modal by updating the step the modal is on. * @param {React.Dispatch>} updateSticker The react state function to update the selected sticker that will be added or updated. */ const AddUpdateSticker = ({ isOpen, updateIsOpen, date, updateSticker, currSticker, step, updateStep, selectedSticker, updateSelectedSticker, currDate }: AddStickerProps): JSX.Element => { // TODO: Import the stickers array from the calender context. const { addEditSticker } = useContext(StickersContext); // ! Update these states to say "add" and "edit" for easier reading. const [modalVariant] = useState<"currDate" | "notCurrDate">( isSameDay(date, currDate) ? "currDate" : "notCurrDate" ); const handleClose = () => { updateIsOpen(false); }; // TODO: Validate that the provided sticker is not the current sticker. Throw an error if the same sticker is attempted. const handleSubmit = (sticker) => { const newSticker: Sticker = addEditSticker(date, sticker); updateSticker(newSticker.sticker); handleClose(); }; // The first sticker to have focus when the modal opens. const initialRef = useRef(); // * Double check that the submit button is disabled if the selected sticker is the same as the current sticker. const variants = { currDate: [ { header: `Which sticker did you earn for ${format(date, "LLL d, y")}?`, body: ( {"Select a sticker"} ), footer: ( ) } ], notCurrDate: [ { header: `Which sticker did you want to update for ${format( date, "LLL d, y" )}?`, body: ( {"Current Sticker"} {"Select your new sticker"} ), footer: ( ) }, { header: `Are you sure you want to change the sticker for ${format( date, "M/d/y" )}?`, body: ( {"Previous Sticker"} {"New Sticker"} ), footer: ( ) } ] }; return ( handleClose()} motionPreset="slideInBottom" scrollBehavior="inside" size={modalVariant === "currDate" ? "xl" : "2xl"} > {modalVariant && variants[modalVariant][step].header} {modalVariant && variants[modalVariant][step].body} {modalVariant && variants[modalVariant][step].footer} ); }; export default AddUpdateSticker;