2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-06-24 15:19:11 +02:00
|
|
|
import type { ApiEntity } from "~/shared/types/api";
|
2025-06-23 00:03:37 +02:00
|
|
|
import { Info } from "~/shared/utils/luxon";
|
2025-06-23 22:46:39 +02:00
|
|
|
import { ClientEntity } from "~/utils/client-entity";
|
2025-06-23 18:17:23 +02:00
|
|
|
|
2025-06-24 15:19:11 +02:00
|
|
|
const typeMap: Record<string, EntityClass<ApiEntity, ClientEntity<ApiEntity>>> = {
|
2025-06-23 18:17:23 +02:00
|
|
|
"user": ClientUser,
|
2025-06-23 22:46:39 +02:00
|
|
|
"schedule-location": ClientScheduleLocation,
|
|
|
|
"schedule-event": ClientScheduleEvent,
|
|
|
|
"schedule-role": ClientScheduleRole,
|
|
|
|
"schedule-shift": ClientScheduleShift,
|
2025-06-23 18:17:23 +02:00
|
|
|
};
|
|
|
|
const classMap = new Map(Object.entries(typeMap).map(([k, v]) => [v, k]));
|
2025-06-23 00:03:37 +02:00
|
|
|
|
|
|
|
export default definePayloadPlugin(() => {
|
|
|
|
definePayloadReducer(
|
|
|
|
"ClientMap",
|
|
|
|
data => {
|
|
|
|
if (!(data instanceof ClientMap)) {
|
|
|
|
return;
|
|
|
|
}
|
2025-06-23 18:17:23 +02:00
|
|
|
const type = classMap.get(data.EntityClass)!;
|
2025-06-23 00:03:37 +02:00
|
|
|
const accountStore = useAccountStore();
|
|
|
|
return {
|
2025-06-23 18:17:23 +02:00
|
|
|
type,
|
2025-06-23 00:03:37 +02:00
|
|
|
timezone: accountStore.activeTimezone,
|
|
|
|
locale: accountStore.activeLocale,
|
|
|
|
api: data.toApi(false),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
definePayloadReviver(
|
|
|
|
"ClientMap",
|
2025-06-23 18:17:23 +02:00
|
|
|
({ type, timezone, locale, api }) => {
|
|
|
|
const EntityClass = typeMap[type];
|
2025-06-23 00:03:37 +02:00
|
|
|
const zone = Info.normalizeZone(timezone);
|
2025-06-23 18:17:23 +02:00
|
|
|
return ClientMap.fromApi(EntityClass, api, { zone, locale })
|
2025-06-23 00:03:37 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|