Refactor imports (#470)

This commit is contained in:
Camille Moussu
2026-01-20 17:33:30 +01:00
committed by GitHub
parent aeb8670895
commit 23d50f250d
149 changed files with 1439 additions and 1374 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import { api } from "../../utils/apiUtils";
import { api } from "@/utils/apiUtils";
export async function getCalendars(
userId: string,
+22 -488
View File
@@ -1,40 +1,27 @@
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Calendar } from "./CalendarTypes";
import { extractEventBaseUuid } from "@/utils/extractEventBaseUuid";
import { browserDefaultTimeZone } from "@/utils/timezone";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { CalendarEvent } from "../Events/EventsTypes";
import { Calendar } from "./CalendarTypes";
import {
addSharedCalendar,
getCalendar,
getCalendars,
postCalendar,
proppatchCalendar,
removeCalendar,
updateAclCalendar,
} from "./CalendarApi";
import { getUserDetails } from "../User/userAPI";
import { parseCalendarEvent } from "../Events/eventUtils";
import {
deleteEvent,
deleteEventInstance,
getEvent,
importEventFromFile,
moveEvent,
putEvent,
putEventWithOverrides,
updateSeries,
} from "../Events/EventApi";
import {
computeWeekRange,
formatDateToYYYYMMDDTHHMMSS,
} from "../../utils/dateUtils";
import { User } from "../../components/Attendees/PeopleSearch";
import { getCalendarVisibility } from "../../components/Calendar/utils/calendarUtils";
import { importFile } from "../../utils/apiUtils";
import { formatReduxError } from "../../utils/errorUtils";
import { browserDefaultTimeZone } from "../../utils/timezone";
import { getCalendarDetailAsync } from "./services/getCalendarDetailAsync";
import { refreshCalendarWithSyncToken } from "./services/refreshCalendar";
import { getCalendarsListAsync } from "./services/getCalendarsListAsync";
import { extractEventBaseUuid } from "../../utils/extractEventBaseUuid";
addSharedCalendarAsync,
createCalendarAsync,
deleteEventAsync,
deleteEventInstanceAsync,
getCalendarDetailAsync,
getCalendarsListAsync,
getEventAsync,
getTempCalendarsListAsync,
importEventFromFileAsync,
moveEventAsync,
patchACLCalendarAsync,
patchCalendarAsync,
putEventAsync,
refreshCalendarWithSyncToken,
removeCalendarAsync,
updateEventInstanceAsync,
updateSeriesAsync,
} from "./services";
// Define error type for rejected actions
export interface RejectedError {
@@ -42,459 +29,6 @@ export interface RejectedError {
status?: number;
}
export const getTempCalendarsListAsync = createAsyncThunk<
Record<string, Calendar>,
User,
{ rejectValue: RejectedError }
>("calendars/getTempCalendars", async (tempUser, { rejectWithValue }) => {
try {
const importedCalendars: Record<string, Calendar> = {};
const calendars = (await getCalendars(
tempUser.openpaasId ?? "",
"sharedPublic=true&"
)) as Record<string, any>;
const rawCalendars = calendars._embedded?.["dav:calendar"];
if (!rawCalendars || rawCalendars.length === 0) {
const userName = tempUser.displayName || tempUser.email || "User";
// Format: TRANSLATION:key|param1=value1
const encodedName = encodeURIComponent(userName);
throw new Error(
`TRANSLATION:calendar.userDoesNotHavePublicCalendars|name=${encodedName}`
);
}
for (const cal of rawCalendars) {
const name = cal["dav:name"];
const description = cal["caldav:description"];
const delegated = cal["calendarserver:delegatedsource"] ? true : false;
const source = cal["calendarserver:source"]
? cal["calendarserver:source"]._links.self.href
: cal._links.self.href;
const link = cal._links.self.href;
const id = source.replace("/calendars/", "").replace(".json", "");
const visibility = getCalendarVisibility(cal["acl"]);
const ownerData: any = await getUserDetails(id.split("/")[0]);
importedCalendars[id] = {
id,
name,
link,
owner: `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${ownerData.lastname}`,
ownerEmails: ownerData.emails,
description,
delegated,
color: {
light: tempUser.color?.light ?? "#a8a8a8ff",
dark: tempUser.color?.dark ?? "#a8a8a8ff",
},
visibility,
events: {},
};
}
return importedCalendars;
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
export const putEventAsync = createAsyncThunk<
{ calId: string; events: CalendarEvent[]; calType?: "temp" },
{ cal: Calendar; newEvent: CalendarEvent; calType?: "temp" },
{ rejectValue: RejectedError }
>(
"calendars/putEvent",
async ({ cal, newEvent, calType }, { rejectWithValue }) => {
try {
await putEvent(
newEvent,
cal.ownerEmails ? cal.ownerEmails[0] : undefined
);
const eventDate = new Date(newEvent.start);
const { start: weekStart, end: weekEnd } = computeWeekRange(eventDate);
const calEvents = (await getCalendar(cal.id, {
start: formatDateToYYYYMMDDTHHMMSS(weekStart),
end: formatDateToYYYYMMDDTHHMMSS(weekEnd),
})) as Record<string, any>;
const events: CalendarEvent[] = calEvents._embedded["dav:item"].flatMap(
(eventdata: any) => {
const vevents = eventdata.data[2] as any[][];
const eventURL = eventdata._links.self.href;
const valarm = eventdata.data[2][0][2][0];
return vevents.map((vevent: any[]) => {
return parseCalendarEvent(
vevent[1],
cal.color ?? {},
cal.id,
eventURL,
valarm
);
});
}
);
return {
calId: cal.id,
events,
calType,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const getEventAsync = createAsyncThunk<
{ calId: string; event: CalendarEvent },
CalendarEvent,
{ rejectValue: RejectedError }
>("calendars/getEvent", async (event, { rejectWithValue }) => {
try {
const response: CalendarEvent = await getEvent(event);
return {
calId: event.calId,
event: response,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
export const patchCalendarAsync = createAsyncThunk<
{
calId: string;
calLink: string;
patch: { name: string; desc: string; color: Record<string, string> };
},
{
calId: string;
calLink: string;
patch: { name: string; desc: string; color: Record<string, string> };
},
{ rejectValue: RejectedError }
>(
"calendars/patchCalendar",
async ({ calId, calLink, patch }, { rejectWithValue }) => {
try {
await proppatchCalendar(calLink, patch);
return {
calId,
calLink,
patch,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const removeCalendarAsync = createAsyncThunk<
{
calId: string;
},
{
calId: string;
calLink: string;
},
{ rejectValue: RejectedError }
>(
"calendars/removeCalendar",
async ({ calId, calLink }, { rejectWithValue }) => {
try {
await removeCalendar(calLink);
return {
calId,
calLink,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const moveEventAsync = createAsyncThunk<
{ calId: string; events: CalendarEvent[] },
{ cal: Calendar; newEvent: CalendarEvent; newURL: string },
{ rejectValue: RejectedError }
>(
"calendars/moveEvent",
async ({ cal, newEvent, newURL }, { rejectWithValue }) => {
try {
await moveEvent(newEvent, newURL);
const eventDate = new Date(newEvent.start);
const { start: weekStart, end: weekEnd } = computeWeekRange(eventDate);
const calEvents = (await getCalendar(cal.id, {
start: formatDateToYYYYMMDDTHHMMSS(weekStart),
end: formatDateToYYYYMMDDTHHMMSS(weekEnd),
})) as Record<string, any>;
const events: CalendarEvent[] = calEvents._embedded["dav:item"].flatMap(
(eventdata: any) => {
const vevents = eventdata.data[2] as any[][];
const eventURL = eventdata._links.self.href;
return vevents.map((vevent: any[]) => {
return parseCalendarEvent(
vevent[1],
cal.color ?? {},
cal.id,
eventURL
);
});
}
);
return {
calId: cal.id,
events,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const patchACLCalendarAsync = createAsyncThunk<
{
calId: string;
calLink: string;
request: string;
},
{
calId: string;
calLink: string;
request: string;
},
{ rejectValue: RejectedError }
>(
"calendars/requestACLCalendar",
async ({ calId, calLink, request }, { rejectWithValue }) => {
try {
const response = await updateAclCalendar(calLink, request);
return {
calId,
calLink,
request,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const deleteEventAsync = createAsyncThunk<
{ calId: string; eventId: string },
{ calId: string; eventId: string; eventURL: string },
{ rejectValue: RejectedError }
>(
"calendars/delEvent",
async ({ calId, eventId, eventURL }, { rejectWithValue }) => {
try {
await deleteEvent(eventURL);
return { calId, eventId };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const deleteEventInstanceAsync = createAsyncThunk<
{ calId: string; eventId: string },
{ cal: Calendar; event: CalendarEvent },
{ rejectValue: RejectedError }
>("calendars/delEventInstance", async ({ cal, event }, { rejectWithValue }) => {
try {
await deleteEventInstance(event, cal.ownerEmails?.[0]);
return { calId: cal.id, eventId: event.uid };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
export const updateEventInstanceAsync = createAsyncThunk<
{ calId: string; event: CalendarEvent },
{ cal: Calendar; event: CalendarEvent },
{ rejectValue: RejectedError }
>(
"calendars/updateEventInstance",
async ({ cal, event }, { rejectWithValue }) => {
try {
await putEventWithOverrides(event, cal.ownerEmails?.[0]);
return { calId: cal.id, event };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const updateSeriesAsync = createAsyncThunk<
void,
{ cal: Calendar; event: CalendarEvent; removeOverrides?: boolean },
{ rejectValue: RejectedError }
>(
"calendars/updateSeries",
async ({ cal, event, removeOverrides = true }, { rejectWithValue }) => {
try {
await updateSeries(event, cal.ownerEmails?.[0], removeOverrides);
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const createCalendarAsync = createAsyncThunk<
{
userId: string;
calId: string;
color: Record<string, string>;
name: string;
desc: string;
owner: string;
ownerEmails: string[];
},
{
userId: string;
calId: string;
color: Record<string, string>;
name: string;
desc: string;
},
{ rejectValue: RejectedError }
>(
"calendars/createCalendar",
async ({ userId, calId, color, name, desc }, { rejectWithValue }) => {
try {
await postCalendar(userId, calId, color, name, desc);
const ownerData: any = await getUserDetails(userId.split("/")[0]);
return {
userId,
calId,
color,
name,
desc,
owner: `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${
ownerData.lastname
}`,
ownerEmails: ownerData.emails,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const addSharedCalendarAsync = createAsyncThunk<
{
calId: string;
color: Record<string, string>;
link: string;
name: string;
desc: string;
owner: string;
ownerEmails: string[];
},
{ userId: string; calId: string; cal: Record<string, any> },
{ rejectValue: RejectedError }
>(
"calendars/addSharedCalendar",
async ({ userId, calId, cal }, { rejectWithValue }) => {
try {
await addSharedCalendar(userId, calId, cal);
const ownerData: any = await getUserDetails(
cal.cal._links.self.href
.replace("/calendars/", "")
.replace(".json", "")
.split("/")[0]
);
return {
calId: cal.cal._links.self.href
.replace("/calendars/", "")
.replace(".json", ""),
color: cal.color,
link: `/calendars/${userId}/${calId}.json`,
desc: cal.cal["caldav:description"],
name:
ownerData.id !== userId && cal.cal["dav:name"] === "#default"
? `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${
ownerData.lastname
}` + "'s calendar"
: cal.cal["dav:name"],
owner: `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${
ownerData.lastname
}`,
ownerEmails: ownerData.emails,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
export const importEventFromFileAsync = createAsyncThunk<
void,
{
calLink: string;
file: File;
},
{ rejectValue: RejectedError }
>("calendars/importEvent", async ({ calLink, file }, { rejectWithValue }) => {
try {
const id = ((await importFile(file)) as Record<string, string>)._id;
const response = await importEventFromFile(id, calLink);
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
const CalendarSlice = createSlice({
name: "calendars",
initialState: {
@@ -1,4 +1,4 @@
import { api } from "../../../utils/apiUtils";
import { api } from "@/utils/apiUtils";
import { Calendar } from "../CalendarTypes";
import { DavSyncResponse } from "./types";
@@ -0,0 +1,56 @@
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { getUserDetails } from "@/features/User/userAPI";
import { addSharedCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
export const addSharedCalendarAsync = createAsyncThunk<
{
calId: string;
color: Record<string, string>;
link: string;
name: string;
desc: string;
owner: string;
ownerEmails: string[];
},
{ userId: string; calId: string; cal: Record<string, any> },
{ rejectValue: RejectedError }
>(
"calendars/addSharedCalendar",
async ({ userId, calId, cal }, { rejectWithValue }) => {
try {
await addSharedCalendar(userId, calId, cal);
const ownerData: any = await getUserDetails(
cal.cal._links.self.href
.replace("/calendars/", "")
.replace(".json", "")
.split("/")[0]
);
return {
calId: cal.cal._links.self.href
.replace("/calendars/", "")
.replace(".json", ""),
color: cal.color,
link: `/calendars/${userId}/${calId}.json`,
desc: cal.cal["caldav:description"],
name:
ownerData.id !== userId && cal.cal["dav:name"] === "#default"
? `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${
ownerData.lastname
}` + "'s calendar"
: cal.cal["dav:name"],
owner: `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${
ownerData.lastname
}`,
ownerEmails: ownerData.emails,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,50 @@
import { getUserDetails } from "@/features/User/userAPI";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { postCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
export const createCalendarAsync = createAsyncThunk<
{
userId: string;
calId: string;
color: Record<string, string>;
name: string;
desc: string;
owner: string;
ownerEmails: string[];
},
{
userId: string;
calId: string;
color: Record<string, string>;
name: string;
desc: string;
},
{ rejectValue: RejectedError }
>(
"calendars/createCalendar",
async ({ userId, calId, color, name, desc }, { rejectWithValue }) => {
try {
await postCalendar(userId, calId, color, name, desc);
const ownerData: any = await getUserDetails(userId.split("/")[0]);
return {
userId,
calId,
color,
name,
desc,
owner: [ownerData.firstname, ownerData.lastname]
.filter(Boolean)
.join(" "),
ownerEmails: ownerData.emails ?? [],
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,23 @@
import { deleteEvent } from "@/features/Events/EventApi";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
export const deleteEventAsync = createAsyncThunk<
{ calId: string; eventId: string },
{ calId: string; eventId: string; eventURL: string },
{ rejectValue: RejectedError }
>(
"calendars/delEvent",
async ({ calId, eventId, eventURL }, { rejectWithValue }) => {
try {
await deleteEvent(eventURL);
return { calId, eventId };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,22 @@
import { deleteEventInstance } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const deleteEventInstanceAsync = createAsyncThunk<
{ calId: string; eventId: string },
{ cal: Calendar; event: CalendarEvent },
{ rejectValue: RejectedError }
>("calendars/delEventInstance", async ({ cal, event }, { rejectWithValue }) => {
try {
await deleteEventInstance(event, cal.ownerEmails?.[0]);
return { calId: cal.id, eventId: event.uid };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
@@ -1,6 +1,6 @@
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { formatReduxError } from "../../../utils/errorUtils";
import { CalendarEvent } from "../../Events/EventsTypes";
import { getCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
import { extractCalendarEvents } from "../utils/extractCalendarEvents";
@@ -1,6 +1,6 @@
import { getOpenPaasUser, getUserDetails } from "@/features/User/userAPI";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { formatReduxError } from "../../../utils/errorUtils";
import { getOpenPaasUser, getUserDetails } from "../../User/userAPI";
import { getCalendars } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
@@ -0,0 +1,24 @@
import { getEvent } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
export const getEventAsync = createAsyncThunk<
{ calId: string; event: CalendarEvent },
CalendarEvent,
{ rejectValue: RejectedError }
>("calendars/getEvent", async (event, { rejectWithValue }) => {
try {
const response: CalendarEvent = await getEvent(event);
return {
calId: event.calId,
event: response,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
@@ -0,0 +1,70 @@
import { User } from "@/components/Attendees/PeopleSearch";
import { getCalendarVisibility } from "@/components/Calendar/utils/calendarUtils";
import { getUserDetails } from "@/features/User/userAPI";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { getCalendars } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const getTempCalendarsListAsync = createAsyncThunk<
Record<string, Calendar>,
User,
{ rejectValue: RejectedError }
>("calendars/getTempCalendars", async (tempUser, { rejectWithValue }) => {
try {
const importedCalendars: Record<string, Calendar> = {};
const calendars = (await getCalendars(
tempUser.openpaasId ?? "",
"sharedPublic=true&"
)) as Record<string, any>;
const rawCalendars = calendars._embedded?.["dav:calendar"];
if (!rawCalendars || rawCalendars.length === 0) {
const userName = tempUser.displayName || tempUser.email || "User";
// Format: TRANSLATION:key|param1=value1
const encodedName = encodeURIComponent(userName);
throw new Error(
`TRANSLATION:calendar.userDoesNotHavePublicCalendars|name=${encodedName}`
);
}
for (const cal of rawCalendars) {
const name = cal["dav:name"];
const description = cal["caldav:description"];
const delegated = cal["calendarserver:delegatedsource"] ? true : false;
const source = cal["calendarserver:source"]
? cal["calendarserver:source"]._links.self.href
: cal._links.self.href;
const link = cal._links.self.href;
const id = source.replace("/calendars/", "").replace(".json", "");
const visibility = getCalendarVisibility(cal["acl"]);
const ownerData: any = await getUserDetails(id.split("/")[0]);
importedCalendars[id] = {
id,
name,
link,
owner: `${ownerData.firstname ? `${ownerData.firstname} ` : ""}${ownerData.lastname}`,
ownerEmails: ownerData.emails,
description,
delegated,
color: {
light: tempUser.color?.light ?? "#a8a8a8ff",
dark: tempUser.color?.dark ?? "#a8a8a8ff",
},
visibility,
events: {},
};
}
return importedCalendars;
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
@@ -0,0 +1,31 @@
import { importEventFromFile } from "@/features/Events/EventApi";
import { importFile } from "@/utils/apiUtils";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
export const importEventFromFileAsync = createAsyncThunk<
void,
{
calLink: string;
file: File;
},
{ rejectValue: RejectedError }
>("calendars/importEvent", async ({ calLink, file }, { rejectWithValue }) => {
try {
const response = await importFile(file);
const id = response?._id;
if (!id) {
return rejectWithValue({
message: "Failed to upload file: missing file ID",
status: undefined,
});
}
await importEventFromFile(id, calLink);
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
});
+17
View File
@@ -0,0 +1,17 @@
export { addSharedCalendarAsync } from "./addSharedCalendarAsync";
export { createCalendarAsync } from "./createCalendarAsync";
export { deleteEventAsync } from "./deleteEventAsync";
export { deleteEventInstanceAsync } from "./deleteEventInstanceAsync";
export { getCalendarDetailAsync } from "./getCalendarDetailAsync";
export { getCalendarsListAsync } from "./getCalendarsListAsync";
export { getEventAsync } from "./getEventAsync";
export { getTempCalendarsListAsync } from "./getTempCalendarsListAsync";
export { importEventFromFileAsync } from "./importEventFromFileAsync";
export { moveEventAsync } from "./moveEventAsync";
export { patchACLCalendarAsync } from "./patchACLCalendarAsync";
export { patchCalendarAsync } from "./patchCalendarAsync";
export { putEventAsync } from "./putEventAsync";
export { refreshCalendarWithSyncToken } from "./refreshCalendar";
export { removeCalendarAsync } from "./removeCalendarAsync";
export { updateEventInstanceAsync } from "./updateEventInstanceAsync";
export { updateSeriesAsync } from "./updateSeriesAsync";
@@ -0,0 +1,57 @@
import { moveEvent } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { parseCalendarEvent } from "@/features/Events/eventUtils";
import {
computeWeekRange,
formatDateToYYYYMMDDTHHMMSS,
} from "@/utils/dateUtils";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { getCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const moveEventAsync = createAsyncThunk<
{ calId: string; events: CalendarEvent[] },
{ cal: Calendar; newEvent: CalendarEvent; newURL: string },
{ rejectValue: RejectedError }
>(
"calendars/moveEvent",
async ({ cal, newEvent, newURL }, { rejectWithValue }) => {
try {
await moveEvent(newEvent, newURL);
const eventDate = new Date(newEvent.start);
const { start: weekStart, end: weekEnd } = computeWeekRange(eventDate);
const calEvents = (await getCalendar(cal.id, {
start: formatDateToYYYYMMDDTHHMMSS(weekStart),
end: formatDateToYYYYMMDDTHHMMSS(weekEnd),
})) as Record<string, any>;
const events: CalendarEvent[] = calEvents._embedded["dav:item"].flatMap(
(eventdata: any) => {
const vevents = eventdata.data[2] as any[][];
const eventURL = eventdata._links.self.href;
return vevents.map((vevent: any[]) => {
return parseCalendarEvent(
vevent[1],
cal.color ?? {},
cal.id,
eventURL
);
});
}
);
return {
calId: cal.id,
events,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,35 @@
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { updateAclCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
export const patchACLCalendarAsync = createAsyncThunk<
{
calId: string;
calLink: string;
request: string;
},
{
calId: string;
calLink: string;
request: string;
},
{ rejectValue: RejectedError }
>(
"calendars/requestACLCalendar",
async ({ calId, calLink, request }, { rejectWithValue }) => {
try {
await updateAclCalendar(calLink, request);
return {
calId,
calLink,
request,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,35 @@
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { proppatchCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
export const patchCalendarAsync = createAsyncThunk<
{
calId: string;
calLink: string;
patch: { name: string; desc: string; color: Record<string, string> };
},
{
calId: string;
calLink: string;
patch: { name: string; desc: string; color: Record<string, string> };
},
{ rejectValue: RejectedError }
>(
"calendars/patchCalendar",
async ({ calId, calLink, patch }, { rejectWithValue }) => {
try {
await proppatchCalendar(calLink, patch);
return {
calId,
calLink,
patch,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,63 @@
import { putEvent } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { parseCalendarEvent } from "@/features/Events/eventUtils";
import {
computeWeekRange,
formatDateToYYYYMMDDTHHMMSS,
} from "@/utils/dateUtils";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { getCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const putEventAsync = createAsyncThunk<
{ calId: string; events: CalendarEvent[]; calType?: "temp" },
{ cal: Calendar; newEvent: CalendarEvent; calType?: "temp" },
{ rejectValue: RejectedError }
>(
"calendars/putEvent",
async ({ cal, newEvent, calType }, { rejectWithValue }) => {
try {
await putEvent(
newEvent,
cal.ownerEmails ? cal.ownerEmails[0] : undefined
);
const eventDate = new Date(newEvent.start);
const { start: weekStart, end: weekEnd } = computeWeekRange(eventDate);
const calEvents = (await getCalendar(cal.id, {
start: formatDateToYYYYMMDDTHHMMSS(weekStart),
end: formatDateToYYYYMMDDTHHMMSS(weekEnd),
})) as Record<string, any>;
const events: CalendarEvent[] = calEvents._embedded["dav:item"].flatMap(
(eventdata: any) => {
const vevents = eventdata.data[2] as any[][];
const eventURL = eventdata._links.self.href;
const valarm = eventdata.data[2][0][2][0];
return vevents.map((vevent: any[]) => {
return parseCalendarEvent(
vevent[1],
cal.color ?? {},
cal.id,
eventURL,
valarm
);
});
}
);
return {
calId: cal.id,
events,
calType,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -1,7 +1,7 @@
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import pMap from "p-map";
import { formatReduxError } from "../../../utils/errorUtils";
import { CalendarEvent } from "../../Events/EventsTypes";
import { fetchSyncTokenChanges } from "../api/fetchSyncTokenChanges";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
@@ -0,0 +1,30 @@
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { removeCalendar } from "../CalendarApi";
import { RejectedError } from "../CalendarSlice";
export const removeCalendarAsync = createAsyncThunk<
{
calId: string;
},
{
calId: string;
calLink: string;
},
{ rejectValue: RejectedError }
>(
"calendars/removeCalendar",
async ({ calId, calLink }, { rejectWithValue }) => {
try {
await removeCalendar(calLink);
return {
calId,
};
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,25 @@
import { putEventWithOverrides } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const updateEventInstanceAsync = createAsyncThunk<
{ calId: string; event: CalendarEvent },
{ cal: Calendar; event: CalendarEvent },
{ rejectValue: RejectedError }
>(
"calendars/updateEventInstance",
async ({ cal, event }, { rejectWithValue }) => {
try {
await putEventWithOverrides(event, cal.ownerEmails?.[0]);
return { calId: cal.id, event };
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -0,0 +1,24 @@
import { updateSeries } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatReduxError } from "@/utils/errorUtils";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RejectedError } from "../CalendarSlice";
import { Calendar } from "../CalendarTypes";
export const updateSeriesAsync = createAsyncThunk<
void,
{ cal: Calendar; event: CalendarEvent; removeOverrides?: boolean },
{ rejectValue: RejectedError }
>(
"calendars/updateSeries",
async ({ cal, event, removeOverrides = true }, { rejectWithValue }) => {
try {
await updateSeries(event, cal.ownerEmails?.[0] ?? "", removeOverrides);
} catch (err: any) {
return rejectWithValue({
message: formatReduxError(err),
status: err.response?.status,
});
}
}
);
@@ -1,6 +1,6 @@
import { formatDateToYYYYMMDDTHHMMSS } from "../../../utils/dateUtils";
import { reportEvent } from "../../Events/EventApi";
import { CalendarEvent } from "../../Events/EventsTypes";
import { reportEvent } from "@/features/Events/EventApi";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { formatDateToYYYYMMDDTHHMMSS } from "@/utils/dateUtils";
import { Calendar } from "../CalendarTypes";
import { extractCalendarEvents } from "./extractCalendarEvents";
@@ -1,6 +1,6 @@
import { defaultColors } from "../../../components/Calendar/utils/calendarColorsUtils";
import { CalendarEvent } from "../../Events/EventsTypes";
import { parseCalendarEvent } from "../../Events/eventUtils";
import { defaultColors } from "@/components/Calendar/utils/calendarColorsUtils";
import { CalendarEvent } from "@/features/Events/EventsTypes";
import { parseCalendarEvent } from "@/features/Events/eventUtils";
import { CalDavItem } from "../api/types";
export function extractCalendarEvents(
@@ -1,4 +1,4 @@
import { getCalendarVisibility } from "../../../components/Calendar/utils/calendarUtils";
import { getCalendarVisibility } from "@/components/Calendar/utils/calendarUtils";
export function normalizeCalendar(rawCalendar: Record<string, any>) {
const description = rawCalendar["caldav:description"];