Skip to content

Commit 7ea2707

Browse files
committed
Updated readme with live preview
1 parent 1d0a1e3 commit 7ea2707

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,12 @@ export default ParagraphElementComponent
170170
Now we need the master component which will be responsible for rendering the layout (sections/row/columns):
171171

172172
```tsx
173-
import React, { FC } from 'react'
173+
import React, { FC, useEffect } from 'react'
174174
import { useQuery } from '@apollo/client'
175175

176176
import { graphql } from '@/graphql'
177177
import CompositionNodeComponent from './CompositionNodeComponent'
178+
import { onContentSaved } from "@/helpers/onContentSaved";
178179

179180
export const VisualBuilder = graphql(/* GraphQL */ `
180181
query VisualBuilder($key: String, $version: String) {
@@ -227,35 +228,46 @@ const VisualBuilderComponent: FC<VisualBuilderProps> = ({ key, version }) => {
227228
variables.key = key;
228229
}
229230

230-
const { data } = useQuery(VisualBuilder, { variables: variables })
231+
const { data, refetch } = useQuery(VisualBuilder, { variables: variables });
232+
233+
useEffect(() => {
234+
onContentSaved(_ => {
235+
refetch();
236+
})
237+
}, []);
238+
239+
const experiences = data?._Experience?.items;
240+
if (!experiences) {
241+
return null;
242+
}
243+
244+
const experience: any = experiences[experiences.length - 1];
245+
246+
if (!experience) {
247+
return null;
248+
}
231249

232250
return (
233251
<div className="relative w-full flex-1 vb:outline">
234-
{data?._Experience?.items?.map((experience: any) => (
235-
<div className="relative w-full flex-1 vb:outline">
236-
{experience?.composition?.grids?.map((grid: any) =>
237-
<div className="relative w-full flex flex-col flex-nowrap justify-start vb:grid"
238-
data-epi-block-id={grid?.key}>
239-
{grid.rows?.map((row: any) =>
240-
<div
241-
className="flex-1 flex flex-row flex-nowrap justify-start vb:row">
242-
{row.columns?.map((column: any) => (
243-
<div
244-
className="flex-1 flex flex-col flex-nowrap justify-start vb:col">
245-
{column.elements?.map((element: any) =>
246-
<div
247-
data-epi-block-id={element?.key}>
248-
<CompositionNodeComponent
249-
compositionElementNode={element}/>
250-
</div>
251-
)}
252-
</div>
253-
))}
254-
</div>)}
255-
</div>
256-
)}
257-
</div>
258-
))}
252+
<div className="relative w-full flex-1 vb:outline">
253+
{experience?.composition?.grids?.map((grid: any) =>
254+
<div key={grid.key} className="relative w-full flex flex-col flex-nowrap justify-start vb:grid"
255+
data-epi-block-id={grid.key}>
256+
{grid.rows?.map((row: any) =>
257+
<div key={row.key} className="flex-1 flex flex-row flex-nowrap justify-start vb:row">
258+
{row.columns?.map((column: any) => (
259+
<div className="flex-1 flex flex-col flex-nowrap justify-start vb:col" key={column.key}>
260+
{column.elements?.map((element: any) =>
261+
<div data-epi-block-id={element?.key} key={element?.key}>
262+
<CompositionNodeComponent compositionElementNode={element}/>
263+
</div>
264+
)}
265+
</div>
266+
))}
267+
</div>)}
268+
</div>
269+
)}
270+
</div>
259271
</div>
260272
)
261273
}
@@ -304,3 +316,18 @@ export default CompositionElementNodeComponent
304316

305317
As you can see based on `element.__typename` we can use different components - in our
306318
example we will use `ParagraphElementComponent`.
319+
320+
### Subscribing to content changes
321+
322+
You need to subscribe to a special event in order to know once content has been updated.
323+
324+
In this repo the subscription is already done in [onContentSaved.ts](src%2Fhelpers%2FonContentSaved.ts)
325+
326+
```ts
327+
epi.subscribe("contentSaved", function (message: ContentSavedEventArgs) {
328+
// your code here
329+
});
330+
```
331+
332+
More details here:
333+
https://docs.developers.optimizely.com/content-management-system/v1.0.0-CMS-SaaS/docs/enable-live-preview#refresh-the-applications-view-when-content-has-changed

0 commit comments

Comments
 (0)