import React, { useState, useRef } from "react"; import { useAppDispatch } from "../../../redux/hooks"; import { addEditSticker } from "../../../features/calender/stickers"; import { Button, Modal, ModalOverlay, ModalContent, ModalHeader, ModalBody, ModalFooter, Heading, HStack, Text, VStack, SimpleGrid, Box } from "@chakra-ui/react"; import { format, isSameDay } from "date-fns"; import { Icon } from "@iconify/react"; import StickerSelector from "./StickerSelector"; import DemoStickers from "../stickers/DemoStickers"; interface AddStickerProps { isOpen: boolean; updateIsOpen: React.Dispatch>; stickerDate: string; 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} stickerDate The date for which the sticker will be added or modified. * @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 {StickerVal} selectedSticker the value of the selected sticker. * @param {React.Dispatch>} updateSelectedSticker The react state function to update the selected sticker that will be added or updated. * @param {Date} currDate the current date. */ const AddUpdateSticker = ({ isOpen, updateIsOpen, stickerDate, currSticker, step, updateStep, selectedSticker, updateSelectedSticker, currDate }: AddStickerProps): JSX.Element => { const dispatch = useAppDispatch(); const stickerDateObj = new Date(stickerDate); const [modalVariant] = useState<"add" | "edit">( isSameDay(stickerDateObj, currDate) ? "add" : "edit" ); 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: StickerVal) => { dispatch(addEditSticker({ stickerDate, 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 = { add: [ { header: `Which sticker did you earn for ${format( stickerDateObj, "LLL d, y" )}?`, body: ( {"Select a sticker"} ), footer: ( ) } ], edit: [ { header: `Which sticker did you want to update for ${format( stickerDateObj, "LLL d, y" )}?`, body: ( {"Current Sticker"} {"Select your new sticker"} ), footer: ( ) }, { header: `Are you sure you want to change the sticker for ${format( stickerDateObj, "M/d/y" )}?`, body: ( {"Previous Sticker"} {"New Sticker"} ), footer: ( ) } ] }; return ( handleClose()} motionPreset="slideInBottom" scrollBehavior="inside" size={modalVariant === "add" ? "xl" : "2xl"} > {modalVariant && variants[modalVariant][step].header} {modalVariant && variants[modalVariant][step].body} {modalVariant && variants[modalVariant][step].footer} ); }; export default AddUpdateSticker;