Skip to content

Commit 7c748b7

Browse files
authored
Merge pull request #3 from episerver/user/base/fix-preview-update
2 parents bf32c8d + 90611b3 commit 7c748b7

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

src/components/base/VisualBuilderComponent.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React, { FC } from 'react'
1+
import React, { FC, useEffect } from 'react'
22
import { useQuery } from '@apollo/client'
33

44
import { graphql } from '@/graphql'
55
import CompositionNodeComponent from './CompositionNodeComponent'
6+
import { onContentSaved } from "@/helpers/onContentSaved";
67

78
export const VisualBuilder = graphql(/* GraphQL */ `
89
query VisualBuilder($key: String, $version: String) {
@@ -55,8 +56,14 @@ const VisualBuilderComponent: FC<VisualBuilderProps> = ({ key, version }) => {
5556
variables.key = key;
5657
}
5758

58-
const { data } = useQuery(VisualBuilder, { variables: variables })
59+
const { data, refetch } = useQuery(VisualBuilder, { variables: variables });
5960

61+
useEffect(() => {
62+
onContentSaved(_ => {
63+
refetch();
64+
})
65+
}, []);
66+
6067
const experiences = data?._Experience?.items;
6168
if (!experiences) {
6269
return null;

src/helpers/onContentSaved.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
interface PropertySaved {
2+
name: string;
3+
successful: boolean;
4+
}
5+
6+
interface ContentSavedEventArgs {
7+
contentLink: string;
8+
previewUrl: string;
9+
isIndexed: boolean;
10+
properties: PropertySaved[];
11+
parentId?: string;
12+
sectionId?: string;
13+
}
14+
15+
function ensureEpiLoaded() {
16+
if (typeof window !== "undefined") {
17+
const epi = (window as any).epi;
18+
if (typeof epi !== "undefined" && epi.isEditable && epi.ready) {
19+
return epi;
20+
}
21+
} else {
22+
return null;
23+
}
24+
}
25+
26+
export function onContentSaved(callback: ((message: ContentSavedEventArgs) => void )) {
27+
const epi = ensureEpiLoaded();
28+
if (epi) {
29+
console.info("successfully connected to CMS.");
30+
epi.subscribe("contentSaved", function (message: ContentSavedEventArgs) {
31+
callback(message);
32+
});
33+
} else {
34+
setTimeout(() => {
35+
console.info("connecting to CMS...");
36+
onContentSaved(callback);
37+
}, 100);
38+
}
39+
}

src/hooks/useSimpleReloader.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/pages/[[...url]]/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import VisualBuilderComponent from "@/components/base/VisualBuilderComponent";
22
import { Inter } from "next/font/google";
3-
import useSimpleReloader from "@/hooks/useSimpleReloader";
43

54
const inter = Inter({ subsets: ["latin"] });
65

76
export default function Home() {
8-
useSimpleReloader();
97
let version: string | undefined;
108
let key: string | undefined;
119

0 commit comments

Comments
 (0)