import { useEffect, useState } from "react"; import { createCalendarAsync /*, updateCalendarAsync */, patchCalendarAsync, } from "./CalendarSlice"; import { useAppDispatch, useAppSelector } from "../../app/hooks"; import { Popover, TextField, Button, Box, Typography, ButtonGroup, } from "@mui/material"; import { Calendars } from "./CalendarTypes"; function CalendarPopover({ anchorEl, open, onClose, calendar, }: { anchorEl: HTMLElement | null; open: boolean; onClose: ( event: object | null, reason: "backdropClick" | "escapeKeyDown" ) => void; calendar?: Calendars; }) { const dispatch = useAppDispatch(); const userId = useAppSelector((state) => state.user.userData.openpaasId) ?? ""; const [name, setName] = useState(calendar?.name ?? ""); const [description, setDescription] = useState(calendar?.description ?? ""); const [color, setColor] = useState(calendar?.color ?? ""); useEffect(() => { if (open) { if (calendar) { setName(calendar.name); setDescription(calendar.description ?? ""); setColor(calendar.color ?? ""); } else { setName(""); setDescription(""); setColor(""); } } }, [calendar, open]); const handleSave = () => { const trimmedName = name.trim(); const trimmedDesc = description.trim(); if (trimmedName) { const calId = calendar ? calendar.id : crypto.randomUUID(); if (calendar?.id) { dispatch( patchCalendarAsync({ calId: calendar.id, calLink: calendar.link, patch: { name: trimmedName, desc: trimmedDesc, color }, }) ); } else { dispatch( createCalendarAsync({ name: trimmedName, desc: trimmedDesc, color, userId, calId, }) ); } onClose({}, "backdropClick"); } }; const palette = [ "#D50000", "#E67C73", "#F4511E", "#F6BF26", "#33B679", "#0B8043", "#039BE5", "#3F51B5", "#7986CB", "#8E24AA", "#616161", ]; return ( onClose(e, reason)} anchorOrigin={{ vertical: "center", horizontal: "center" }} transformOrigin={{ vertical: "center", horizontal: "center" }} > Calendar configuration setName(e.target.value)} size="small" margin="dense" /> setDescription(e.target.value)} size="small" margin="dense" multiline rows={2} /> {palette.map((c) => ( ); } export default CalendarPopover;