(null);
-
return (
<>
@@ -39,39 +43,35 @@ export default function CalendarSelection({
- {personnalCalendars.map((id) => {
- return (
-
-
-
- );
- })}
+ {personnalCalendars.map((id) =>
+ CalendarSelector(
+ calendars,
+ id,
+ selectedCalendars,
+ handleCalendarToggle,
+ () => {
+ setAnchorElCal(document.body);
+ setSelectedCalId(id);
+ }
+ )
+ )}
{delegatedCalendars.length > 0 && (
<>
Delegated Calendars
- {delegatedCalendars.map((id) => (
-
-
-
- ))}
+ {delegatedCalendars.map((id) =>
+ CalendarSelector(
+ calendars,
+ id,
+ selectedCalendars,
+ handleCalendarToggle,
+ () => {
+ setAnchorElCal(document.body);
+ setSelectedCalId(id);
+ }
+ )
+ )}
>
)}
{sharedCalendars.length > 0 && (
@@ -79,27 +79,57 @@ export default function CalendarSelection({
Shared Calendars
- {sharedCalendars.map((id) => (
-
-
-
- ))}
+ {sharedCalendars.map((id) =>
+ CalendarSelector(
+ calendars,
+ id,
+ selectedCalendars,
+ handleCalendarToggle,
+ () => {
+ setAnchorElCal(document.body);
+ setSelectedCalId(id);
+ }
+ )
+ )}
>
)}
setAnchorElCal(null)}
+ onClose={() => {
+ setAnchorElCal(null);
+ }}
+ calendar={calendars[selectedCalId] ?? undefined}
/>
>
);
}
+
+function CalendarSelector(
+ calendars: Record,
+ id: string,
+ selectedCalendars: string[],
+ handleCalendarToggle: (name: string) => void,
+ setOpen: Function
+) {
+ return (
+
+
+ setOpen()}>
+
+
+
+ );
+}
diff --git a/src/features/Calendars/CalendarApi.ts b/src/features/Calendars/CalendarApi.ts
index 9cf8035..5ae9f59 100644
--- a/src/features/Calendars/CalendarApi.ts
+++ b/src/features/Calendars/CalendarApi.ts
@@ -51,3 +51,27 @@ export async function postCalendar(
});
return response;
}
+
+export async function proppatchCalendar(
+ calLink: string,
+ patch: { name: string; desc: string; color: string }
+) {
+ const body: Record = {};
+ if (patch.name) {
+ body["dav:name"] = patch.name;
+ }
+ if (patch.desc) {
+ body["caldav:description"] = patch.desc;
+ }
+ if (patch.color) {
+ body["apple:color"] = patch.color;
+ }
+ const response = await api(`dav${calLink}`, {
+ method: "PROPPATCH",
+ headers: {
+ Accept: "application/json, text/plain, */*",
+ },
+ body: JSON.stringify(body),
+ });
+ return response;
+}
diff --git a/src/features/Calendars/CalendarModal.tsx b/src/features/Calendars/CalendarModal.tsx
index 05c2307..623716b 100644
--- a/src/features/Calendars/CalendarModal.tsx
+++ b/src/features/Calendars/CalendarModal.tsx
@@ -1,5 +1,8 @@
-import { useState } from "react";
-import { createCalendar, createCalendarAsync } from "./CalendarSlice";
+import { useEffect, useState } from "react";
+import {
+ createCalendarAsync /*, updateCalendarAsync */,
+ patchCalendarAsync,
+} from "./CalendarSlice";
import { useAppDispatch, useAppSelector } from "../../app/hooks";
import {
Popover,
@@ -7,38 +10,76 @@ import {
Button,
Box,
Typography,
- Select,
ButtonGroup,
} from "@mui/material";
+import { Calendars } from "./CalendarTypes";
function CalendarPopover({
anchorEl,
open,
onClose,
+ calendar,
}: {
anchorEl: HTMLElement | null;
open: boolean;
- onClose: (Calendar: {}, reason: "backdropClick" | "escapeKeyDown") => void;
+ 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("");
- const [description, setDescription] = useState("");
- const [color, setColor] = useState("");
- const handleSave = () => {
- const calId = crypto.randomUUID();
- if (name) {
- dispatch(
- createCalendarAsync({ name, desc: description, color, userId, calId })
- );
- onClose({}, "backdropClick");
+ const [name, setName] = useState(calendar?.name ?? "");
+ const [description, setDescription] = useState(calendar?.description ?? "");
+ const [color, setColor] = useState(calendar?.color ?? "");
- // Reset
- setName("");
- setDescription("");
+ 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",
@@ -52,19 +93,14 @@ function CalendarPopover({
"#8E24AA",
"#616161",
];
+
return (
onClose(e, reason)}
+ anchorOrigin={{ vertical: "center", horizontal: "center" }}
+ transformOrigin={{ vertical: "center", horizontal: "center" }}
>
- {palette.map((color) => (
+ {palette.map((c) => (