Files
HKSingleParty/03_source/frontend/src/actions/calendar.ts
louiscklaw bebf9834e6 update,
2025-06-15 04:09:29 +08:00

132 lines
2.9 KiB
TypeScript

import { useMemo } from 'react';
import axios, { endpoints, fetcher } from 'src/lib/axios';
import type { ICalendarEvent } from 'src/types/calendar';
import type { SWRConfiguration } from 'swr';
import useSWR, { mutate } from 'swr';
// ----------------------------------------------------------------------
const enableServer = false;
const CALENDAR_ENDPOINT = endpoints.calendar;
const swrOptions: SWRConfiguration = {
revalidateIfStale: enableServer,
revalidateOnFocus: enableServer,
revalidateOnReconnect: enableServer,
};
// ----------------------------------------------------------------------
type EventsData = {
events: ICalendarEvent[];
};
export function useGetEvents() {
const { data, isLoading, error, isValidating } = useSWR<EventsData>(
CALENDAR_ENDPOINT,
fetcher,
swrOptions
);
const memoizedValue = useMemo(() => {
const events = data?.events.map((event) => ({ ...event, textColor: event.color }));
return {
events: events || [],
eventsLoading: isLoading,
eventsError: error,
eventsValidating: isValidating,
eventsEmpty: !isLoading && !isValidating && !data?.events.length,
};
}, [data?.events, error, isLoading, isValidating]);
return memoizedValue;
}
// ----------------------------------------------------------------------
export async function createEvent(eventData: ICalendarEvent) {
/**
* Work on server
*/
if (enableServer) {
const data = { eventData };
await axios.post(CALENDAR_ENDPOINT, data);
}
/**
* Work in local
*/
mutate(
CALENDAR_ENDPOINT,
(currentData) => {
const currentEvents: ICalendarEvent[] = currentData?.events;
const events = [...currentEvents, eventData];
return { ...currentData, events };
},
false
);
}
// ----------------------------------------------------------------------
export async function updateEvent(eventData: Partial<ICalendarEvent>) {
/**
* Work on server
*/
if (enableServer) {
const data = { eventData };
await axios.put(CALENDAR_ENDPOINT, data);
}
/**
* Work in local
*/
mutate(
CALENDAR_ENDPOINT,
(currentData) => {
const currentEvents: ICalendarEvent[] = currentData?.events;
const events = currentEvents.map((event) =>
event.id === eventData.id ? { ...event, ...eventData } : event
);
return { ...currentData, events };
},
false
);
}
// ----------------------------------------------------------------------
export async function deleteEvent(eventId: string) {
/**
* Work on server
*/
if (enableServer) {
const data = { eventId };
await axios.patch(CALENDAR_ENDPOINT, data);
}
/**
* Work in local
*/
mutate(
CALENDAR_ENDPOINT,
(currentData) => {
const currentEvents: ICalendarEvent[] = currentData?.events;
const events = currentEvents.filter((event) => event.id !== eventId);
return { ...currentData, events };
},
false
);
}