Remove type from Api serialisation of ClientMap
Move the logic that converts the EntityClass of a map to a string and then back into the class to the payload plugin in order to avoid a circular dependency where the ClientMap needs to know the entity classes and the entity classes needs to know the ClientMap. The only place that doesn't know the type of the entities stored in the client map is the payload reviver, so it makes sense to keep this logic contained to the payload plugin.
This commit is contained in:
parent
930d93a95f
commit
d48fb035b4
4 changed files with 70 additions and 91 deletions
|
@ -1,4 +1,10 @@
|
|||
import { Info } from "~/shared/utils/luxon";
|
||||
import { ClientEntityNew } from "~/utils/client-user";
|
||||
|
||||
const typeMap: Record<string, EntityClass<ClientEntityNew>> = {
|
||||
"user": ClientUser,
|
||||
};
|
||||
const classMap = new Map(Object.entries(typeMap).map(([k, v]) => [v, k]));
|
||||
|
||||
export default definePayloadPlugin(() => {
|
||||
definePayloadReducer(
|
||||
|
@ -7,8 +13,10 @@ export default definePayloadPlugin(() => {
|
|||
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),
|
||||
|
@ -17,9 +25,10 @@ export default definePayloadPlugin(() => {
|
|||
);
|
||||
definePayloadReviver(
|
||||
"ClientMap",
|
||||
({ timezone, locale, api }) => {
|
||||
({ type, timezone, locale, api }) => {
|
||||
const EntityClass = typeMap[type];
|
||||
const zone = Info.normalizeZone(timezone);
|
||||
return ClientMap.fromApi(api, { zone, locale })
|
||||
return ClientMap.fromApi(EntityClass, api, { zone, locale })
|
||||
},
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue