Skip to content

Commit ca7b2b8

Browse files
Live-preview implementation using SDK
1 parent b2ba7e9 commit ca7b2b8

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

package-lock.json

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"lint": "next lint"
99
},
1010
"dependencies": {
11+
"@contentstack/live-preview-utils": "^1.0.1",
1112
"contentstack": "^3.15.0",
1213
"dotenv": "^10.0.0",
1314
"next": "12.0.7",

pages/api/utils.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Contentstack from 'contentstack'
2+
import ContentstackLivePreview from "@contentstack/live-preview-utils";
3+
4+
const Stack = Contentstack.Stack({
5+
api_key: process.env.api_key,
6+
delivery_token: process.env.delivery_token,
7+
environment: process.env.environment,
8+
live_preview: {
9+
management_token: process.env.management_token,
10+
enable: true,
11+
host: 'api.contentstack.io'
12+
}
13+
})
14+
15+
16+
Stack.setHost("api.contentstack.io")
17+
ContentstackLivePreview.init({enable: true, stackSdk: Stack, ssr:false});
18+
19+
export const onEntryChange = ContentstackLivePreview.onEntryChange;

pages/index.js

+13-39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Contentstack from "contentstack"
22
import { useEffect, useState } from "react";
3+
import { onEntryChange } from "./api/utils";
34

4-
let apiUrl = `https://api.contentstack.io/v3/content_types/ashwini_live_preview/entries/blt8a69169a533e195a?`
5+
6+
// let apiUrl = `https://api.contentstack.io/v3/content_types/ashwini_live_preview/entries/blt8a69169a533e195a?`
57
const Stack = Contentstack.Stack(
68
{
79
api_key: process.env.api_key,
@@ -10,6 +12,7 @@ const Stack = Contentstack.Stack(
1012
}
1113
);
1214

15+
1316
function fetchData (contentType, entryId){
1417
try{
1518
if(entryId){
@@ -37,49 +40,20 @@ function fetchData (contentType, entryId){
3740

3841
export default function Home(props) {
3942
console.log("PROPS", props)
40-
const [entry, setEntry] = useState(props['single_line'])
41-
// const {title, single_line, multi_line, paragraph_1, paragraph_2, paragraph_3, file} = entry
43+
const [entry, setEntry] = useState(props)
44+
45+
const updatedData = async() => {
46+
let updatedEntry = await fetchData("ashwini_live_preview", "blt8a69169a533e195a")
47+
setEntry(updatedEntry)
48+
}
4249

4350
useEffect(() => {
44-
//initializing live preview when the page renders for the first time.
45-
window.parent.postMessage(
46-
{
47-
from: "live-preview",
48-
type: "init",
49-
data: {
50-
config: {
51-
shouldReload: false,
52-
href: window.location.href,
53-
},
54-
},
55-
},
56-
"*"
57-
);
58-
//listening to post messages for the hash value
59-
window.addEventListener("message", async (e) => {
60-
console.log("Event:",e)
61-
const {data, from, type} = e.data
62-
//check for change in data
63-
if(data?.hasOwnProperty('hash') && from === "live-preview" && type === "client-data-send"){
64-
//making an api call with the hash generated and content type uid
65-
let res = await fetch(`${apiUrl}live_preview=${data['hash']}&content_type_uid=${data['content_type_uid']}`, {
66-
headers: {
67-
api_key: process.env.api_key,
68-
access_token: process.env.delivery_token
69-
}
70-
})
71-
let entryData = await res.json()
72-
console.log("Fetching changed data", entryData)
73-
//changing the states based on the data changed for that particular hash and rendering the same
74-
if(entryData){
75-
setEntry(entryData.entry)
76-
}
77-
}
78-
});
51+
onEntryChange(updatedData)
7952
}, [])
53+
console.log("updated entry", entry)
8054
return (
8155
<div>
82-
<h1>{entry}</h1>
56+
<h1>{"HELLO WORLD"}</h1>
8357
</div>
8458
)
8559
}

0 commit comments

Comments
 (0)