Use a single mutable location, event, slot, etc, for each unique resource that keeps track of the local editable client copy and the server copy of the data contained in it. This makes it much simpler to update these data structures as I can take advantage of the v-model bindings in Vue.js and work with the system instead of against it.
38 lines
1 KiB
TypeScript
38 lines
1 KiB
TypeScript
import { Info } from "~/shared/utils/luxon";
|
|
import { ClientEntity } from "~/utils/client-entity";
|
|
|
|
const typeMap: Record<string, EntityClass<ClientEntity>> = {
|
|
"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 })
|
|
},
|
|
);
|
|
});
|