import React, { useEffect, useState } from "react"; import { addEvent, putEventAsync } from "../Calendars/CalendarSlice"; import { CalendarEvent } from "./EventsTypes"; import { DateSelectArg } from "@fullcalendar/core"; import { useAppDispatch, useAppSelector } from "../../app/hooks"; import { Popover, TextField, Button, Box, Typography, Select, MenuItem, SelectChangeEvent, FormControl, InputLabel, } from "@mui/material"; import { Calendars } from "../Calendars/CalendarTypes"; import { putEvent } from "./EventApi"; import { TIMEZONES } from "../../utils/timezone-data"; function EventPopover({ anchorEl, open, onClose, selectedRange, }: { anchorEl: HTMLElement | null; open: boolean; onClose: (event: {}, reason: "backdropClick" | "escapeKeyDown") => void; selectedRange: DateSelectArg | null; }) { const dispatch = useAppDispatch(); const organizer = useAppSelector((state) => state.user.organiserData); const userId = useAppSelector((state) => state.user.userData.openpaasId); const userPersonnalCalendars: Calendars[] = useAppSelector((state) => Object.keys(state.calendars.list).map((id) => { if (id.split("/")[0] === userId) { return state.calendars.list[id]; } return {} as Calendars; }) ).filter((calendar) => calendar.id); const timezones = TIMEZONES.aliases; const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [location, setLocation] = useState(""); const [start, setStart] = useState(""); const [end, setEnd] = useState(""); const [calendarid, setCalendarid] = useState(0); const [timezone, setTimezone] = useState( Intl.DateTimeFormat().resolvedOptions().timeZone ); useEffect(() => { if (selectedRange) { setStart(selectedRange ? formatLocalDateTime(selectedRange.start) : ""); setEnd(selectedRange ? formatLocalDateTime(selectedRange.end) : ""); } }, [selectedRange]); const handleSave = async () => { if (!calendarid) return; // Prevent crash if no calendar is selected const newEvent: CalendarEvent = { title, start: new Date(start), end: new Date(end), uid: crypto.randomUUID(), description, location, organizer, timezone, attendee: [ { cn: organizer.cn, cal_address: organizer.cal_address, partstat: "ACCEPTED", rsvp: "FALSE", role: "CHAIR", cutype: "INDIVIDUAL", }, ], transp: "OPAQUE", color: userPersonnalCalendars[calendarid]?.color, }; // dispatch( // addEvent({ // calendarUid: userPersonnalCalendars[calendarid]?.id, // event: newEvent, // }) // ); dispatch( putEventAsync({ cal: userPersonnalCalendars[calendarid], newEvent, }) ); onClose({}, "backdropClick"); // Reset setTitle(""); setDescription(""); setLocation(""); setCalendarid(0); }; return ( Create Event Calendar setTitle(e.target.value)} size="small" margin="dense" /> setStart(e.target.value)} size="small" margin="dense" InputLabelProps={{ shrink: true }} /> setEnd(e.target.value)} size="small" margin="dense" InputLabelProps={{ shrink: true }} /> setDescription(e.target.value)} size="small" margin="dense" multiline rows={2} /> setLocation(e.target.value)} size="small" margin="dense" /> Time Zone ); } export default EventPopover; function formatLocalDateTime(date: Date): string { const pad = (n: number) => n.toString().padStart(2, "0"); return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad( date.getDate() )}T${pad(date.getHours())}:${pad(date.getMinutes())}`; }