@@ -170,11 +170,12 @@ export default ParagraphElementComponent
170
170
Now we need the master component which will be responsible for rendering the layout (sections/row/columns):
171
171
172
172
``` tsx
173
- import React , { FC } from ' react'
173
+ import React , { FC , useEffect } from ' react'
174
174
import { useQuery } from ' @apollo/client'
175
175
176
176
import { graphql } from ' @/graphql'
177
177
import CompositionNodeComponent from ' ./CompositionNodeComponent'
178
+ import { onContentSaved } from " @/helpers/onContentSaved" ;
178
179
179
180
export const VisualBuilder = graphql (/* GraphQL */ `
180
181
query VisualBuilder($key: String, $version: String) {
@@ -227,35 +228,46 @@ const VisualBuilderComponent: FC<VisualBuilderProps> = ({ key, version }) => {
227
228
variables .key = key ;
228
229
}
229
230
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
+ }
231
249
232
250
return (
233
251
<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 >
259
271
</div >
260
272
)
261
273
}
@@ -304,3 +316,18 @@ export default CompositionElementNodeComponent
304
316
305
317
As you can see based on ` element.__typename ` we can use different components - in our
306
318
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