1
- import Contentstack from "contentstack"
2
- import { useEffect , useState } from "react" ;
3
- import { onEntryChange } from "./api/utils" ;
4
1
import { Stack } from './api/utils'
5
2
6
-
7
- // let apiUrl = `https://api.contentstack.io/v3/content_types/ashwini_live_preview/entries/blt8a69169a533e195a?`
8
- // const Stack = Contentstack.Stack(
9
- // {
10
- // api_key: process.env.api_key,
11
- // delivery_token: process.env.delivery_token,
12
- // environment: process.env.environment
13
- // }
14
- // );
15
-
16
-
17
3
function fetchData ( contentType , entryId ) {
18
4
try {
19
5
if ( entryId ) {
20
6
let Query = Stack . ContentType ( contentType ) . Entry ( entryId ) ;
21
- let entry = Query . fetch ( )
7
+ return Query . fetch ( )
22
8
. then ( function success ( entry ) {
23
9
return entry . toJSON ( ) ;
24
10
} , function error ( err ) {
25
11
console . log ( "Error" , err ) ;
26
12
} ) ;
27
- return entry ;
28
13
}
29
14
else {
30
15
let Query = Stack . ContentType ( contentType ) . Query ( ) ;
@@ -40,29 +25,24 @@ function fetchData (contentType, entryId){
40
25
}
41
26
42
27
export default function Home ( props ) {
43
- console . log ( "PROPS" , props )
44
- const [ entry , setEntry ] = useState ( props )
45
-
46
- const updatedData = async ( ) => {
47
- let updatedEntry = await fetchData ( "ashwini_live_preview" , "blt8a69169a533e195a" )
48
- setEntry ( updatedEntry )
49
- }
50
-
51
- useEffect ( ( ) => {
52
- onEntryChange ( updatedData )
53
- } , [ ] )
54
- console . log ( "updated entry" , entry )
28
+ const { entry} = props
55
29
return (
56
30
< div >
57
- < h1 > { "HELLO WORLD" } </ h1 >
31
+ < h1 > { entry ?. title } </ h1 >
32
+ < p > { entry ?. single_line } </ p >
58
33
</ div >
59
34
)
60
35
}
61
36
62
- export const getStaticProps = async ( context ) => {
63
- let entry = await fetchData ( "ashwini_live_preview" , "blt8a69169a533e195a" ) ;
37
+ export const getServerSideProps = async ( context ) => {
38
+ if ( context . query ) {
39
+ Stack . livePreviewQuery ( context . query )
40
+ }
41
+ let entry = await fetchData ( process . env . content_type_uid , process . env . entry_uid ) ;
64
42
return { props :
65
- { ...entry }
43
+ {
44
+ entry
45
+ }
66
46
}
67
47
68
48
}
0 commit comments