[#1] getting the calendars from the api

This commit is contained in:
Camille Moussu
2025-07-03 14:21:04 +02:00
parent c11c65c697
commit 65621924a2
10 changed files with 199 additions and 12 deletions
+55 -2
View File
@@ -1,6 +1,46 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Calendars } from "./CalendarTypes";
import { CalendarEvent } from "../Events/EventsTypes";
import { getCalendar, getCalendars } from "./CalendarApi";
import getOpenPaasUserId from "../User/userAPI";
import { parseCalendarEvent } from "../Events/eventUtils";
export const getCalendarsAsync = createAsyncThunk<
Record<string, Calendars>, // Return type
string // Arg type (access_token)
>("calendars/getCalendars", async (access_token: string) => {
const importedCalendars: Record<string, Calendars> = {};
const user = await getOpenPaasUserId(access_token);
const calendars = await getCalendars(user.id, access_token);
const rawCalendars = calendars._embedded["dav:calendar"];
for (const cal of rawCalendars) {
const name = cal["dav:name"];
const description = cal["dav:description"];
const id = cal["calendarserver:source"]
? cal["calendarserver:source"]._links.self.href
.replace("/calendars/", "")
.replace(".json", "")
.split("/")[0]
: cal._links.self.href
.replace("/calendars/", "")
.replace(".json", "")
.split("/")[0];
const color = cal["apple:color"];
const calendarDetails = await getCalendar(id, access_token);
const events = calendarDetails._embedded["dav:item"].map(
(eventdata: any) => {
const datas = eventdata.data[2][0][1];
return parseCalendarEvent(datas, color);
}
);
importedCalendars[id] = { id, name, description, color, events };
}
return importedCalendars;
});
const CalendarSlice = createSlice({
name: "calendars",
@@ -18,7 +58,7 @@ const CalendarSlice = createSlice({
state,
action: PayloadAction<{ calendarUid: string; event: CalendarEvent }>
) => {
console.log(action.payload)
console.log(action.payload);
if (!state[action.payload.calendarUid].events) {
state[action.payload.calendarUid].events = [];
}
@@ -37,6 +77,19 @@ const CalendarSlice = createSlice({
});
},
},
extraReducers: (builder) => {
builder.addCase(
getCalendarsAsync.fulfilled,
(state, action: PayloadAction<Record<string, Calendars>>) => {
console.log("ony est!!!!: ", action.payload);
console.log(Object.keys(action.payload));
Object.keys(action.payload).forEach((id) => {
console.log(id);
state[id] = action.payload[id];
});
}
);
},
});
export const { addEvent, removeEvent, createCalendar } = CalendarSlice.actions;