import { CalendarApi, DateSelectArg } from "@fullcalendar/core"; import { Box, Button, Card, CardActions, CardContent, CardHeader, FormControl, InputLabel, MenuItem, Popover, Select, SelectChangeEvent, TextField, Typography, } from "@mui/material"; import React, { useEffect, useState } from "react"; import { useAppDispatch, useAppSelector } from "../../app/hooks"; import AttendeeSelector from "../../components/Attendees/AttendeeSearch"; import { TIMEZONES } from "../../utils/timezone-data"; import { putEventAsync } from "../Calendars/CalendarSlice"; import { Calendars } from "../Calendars/CalendarTypes"; import { userAttendee } from "../User/userDataTypes"; import { CalendarEvent, RepetitionObject } from "./EventsTypes"; import { createSelector } from "@reduxjs/toolkit"; import RepeatEvent from "../../components/Event/EventRepeat"; function EventPopover({ anchorEl, open, onClose, selectedRange, setSelectedRange, calendarRef, }: { anchorEl: HTMLElement | null; open: boolean; onClose: (event: {}, reason: "backdropClick" | "escapeKeyDown") => void; selectedRange: DateSelectArg | null; setSelectedRange: Function; calendarRef: React.RefObject; }) { const dispatch = useAppDispatch(); const organizer = useAppSelector((state) => state.user.organiserData); const userId = useAppSelector((state) => state.user.userData.openpaasId); const selectPersonnalCalendars = createSelector( (state) => state.calendars, (calendars) => Object.keys(calendars.list) .map((id) => { if (id.split("/")[0] === userId) { return calendars.list[id]; } return {} as Calendars; }) .filter((calendar) => calendar.id) ); const userPersonnalCalendars: Calendars[] = useAppSelector( selectPersonnalCalendars ); const timezones = TIMEZONES.aliases; const [showMore, setShowMore] = useState(false); 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 [allday, setAllDay] = useState(false); const [repetition, setRepetition] = useState( {} as RepetitionObject ); const [attendees, setAttendees] = useState([]); const [alarm, setAlarm] = useState(""); const [eventClass, setEventClass] = useState("PUBLIC"); const [busy, setBusy] = useState(""); 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 () => { const newEventUID = crypto.randomUUID(); const newEvent: CalendarEvent = { calId: userPersonnalCalendars[calendarid].id, title, URL: `/calendars/${userPersonnalCalendars[calendarid].id}/${newEventUID}.ics`, start: new Date(start), allday, uid: newEventUID, description, location, class: eventClass, repetition, 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, alarm: { trigger: alarm, action: "EMAIL" }, }; if (end) { newEvent.end = new Date(end); } if (attendees.length > 0) { newEvent.attendee = newEvent.attendee.concat(attendees); } dispatch( putEventAsync({ cal: userPersonnalCalendars[calendarid], newEvent, }) ); onClose({}, "backdropClick"); // Reset setTitle(""); setDescription(""); setLocation(""); setCalendarid(0); }; return ( setTitle(e.target.value)} size="small" margin="dense" /> Calendar { const newStart = e.target.value; setStart(newStart); const newRange = { ...selectedRange, start: new Date(newStart), startStr: newStart, allDay: allday, }; setSelectedRange(newRange); calendarRef.current?.select(newRange); }} size="small" margin="dense" InputLabelProps={{ shrink: true }} /> { const newEnd = e.target.value; setEnd(newEnd); const newRange = { ...selectedRange, end: new Date(newEnd), endStr: newEnd, allDay: allday, }; setSelectedRange(newRange); calendarRef.current?.select(newRange); }} 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" /> {/* Extended options */} {showMore && ( <> Alarm Visibility )} ); } export default EventPopover; export 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())}`; }