owltide/plugins/payload-client-map.ts
Hornwitser e52972853d License under AGPL version 3 or later
I firmly believe in free software.

The application I'm making here have capabilities that I've not seen in
any system.  It presents itself as an opportunity to collaborate on a
tool that serves the people rather than corporations.  Whose incentives
are to help people rather, not make the most money.  And whose terms
ensure that these freedoms and incentives cannot be taken back or
subverted.

I license this software under the AGPL.
2025-06-30 18:58:24 +02:00

43 lines
1.2 KiB
TypeScript

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