Skip to content

Commit

Permalink
fix: Variant sync
Browse files Browse the repository at this point in the history
  • Loading branch information
areknawo committed Feb 6, 2024
1 parent ef0c785 commit 7422000
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions apps/web/src/context/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const ContentDataProvider: ParentComponent = (props) => {
setContentLevels
});
const contentGroupsSubscription = client.contentGroups.changes.subscribe(undefined, {
onData({ action, data, userId }) {
onData({ action, data }) {
if (action === "move") {
contentActions.moveContentGroup(data);
} else if (action === "create") {
Expand All @@ -156,6 +156,22 @@ const ContentDataProvider: ParentComponent = (props) => {
}
}
});
const variantsSubscription = client.variants.changes.subscribe(undefined, {
onData({ action, data }) {
if (action === "create") {
setVariants(data.id, data);
} else if (action === "update") {
if (variants[data.id]) {
setVariants(data.id, (variant) => ({
...variant,
...data
}));
}
} else if (action === "delete") {
setVariants(data.id, undefined);
}
}
});
const load = (): void => {
setContentLevels(reconcile({}));
setContentGroups(reconcile({}));
Expand All @@ -167,39 +183,13 @@ const ContentDataProvider: ParentComponent = (props) => {
collapseContentLevel(id);
}
});
client.variants.list.query().then((variants) => {
variants.forEach((variant) => {
setVariants(variant.id, variant);
});
});
};

createEffect(
on(activeVariantId, (newVariantId, previousVariantId) => {
if (newVariantId === previousVariantId) return;

load();

const variantsSubscription = client.variants.changes.subscribe(undefined, {
onData({ action, data }) {
if (action === "create") {
setVariants(data.id, data);
} else if (action === "update") {
if (variants[data.id]) {
setVariants(data.id, (variant) => ({
...variant,
...data
}));
}
} else if (action === "delete") {
setVariants(data.id, undefined);
}
}
});

onCleanup(() => {
variantsSubscription.unsubscribe();
});
})
);
createEffect(() => {
Expand Down Expand Up @@ -253,6 +243,12 @@ const ContentDataProvider: ParentComponent = (props) => {
});
onCleanup(() => {
contentGroupsSubscription.unsubscribe();
variantsSubscription.unsubscribe();
});
client.variants.list.query().then((variants) => {
variants.forEach((variant) => {
setVariants(variant.id, variant);
});
});

return (
Expand Down

0 comments on commit 7422000

Please sign in to comment.