File tree Expand file tree Collapse file tree 2 files changed +53
-11
lines changed
Expand file tree Collapse file tree 2 files changed +53
-11
lines changed Original file line number Diff line number Diff line change @@ -5,33 +5,75 @@ class PostDetail extends Component {
55 constructor ( props ) {
66 super ( props )
77 this . state = {
8- slug : null
8+ slug : null ,
9+ post : null ,
10+ doneLoading : false ,
911 }
1012 }
13+
14+ loadPost ( slug ) {
15+ const endpoint = `/api/posts/${ slug } /`
16+ let thisComp = this
17+ let lookupOptions = {
18+ method : "GET" ,
19+ headers : {
20+ 'Content-Type' : 'application/json'
21+ }
22+ }
23+
24+ fetch ( endpoint , lookupOptions )
25+ . then ( function ( response ) {
26+ if ( response . status == 404 ) {
27+ console . log ( 'Page not found' )
28+ }
29+ return response . json ( )
30+ } ) . then ( function ( responseData ) {
31+ if ( responseData . detail ) {
32+ thisComp . setState ( {
33+ doneLoading : true ,
34+ post : null
35+ } )
36+ } else {
37+ thisComp . setState ( {
38+ doneLoading : true ,
39+ post : responseData
40+ } )
41+ }
42+ } ) . catch ( function ( error ) {
43+ console . log ( "error" , error )
44+ } )
45+ }
1146 componentDidMount ( ) {
1247 this . setState ( {
13- slug : null
48+ slug : null ,
49+ post : null
1450 } )
1551 if ( this . props . match ) {
1652 const { slug} = this . props . match . params
1753 this . setState ( {
18- slug : slug
54+ slug : slug ,
55+ doneLoading : false
1956 } )
57+ this . loadPost ( slug )
2058 }
2159 }
2260 render ( ) {
23- const { slug} = this . state
61+ const { doneLoading} = this . state
62+ const { post} = this . state
2463 return (
25- < p > { ( slug !== null ) ? < div >
26-
27- { slug }
64+ < p > { ( doneLoading === true ) ? < div >
65+ { post === null ? "Not Found" :
66+ < div >
67+ < h1 > { post . title } </ h1 >
68+ { post . slug }
2869
2970 < p className = 'lead' > < Link maintainScrollPosition = { false } to = { {
3071 pathname : `/posts` ,
3172 state : { fromDashboard : false }
3273 } } > Posts</ Link > </ p >
33-
34- </ div > : "Not found" } </ p >
74+ </ div >
75+ }
76+ </ div > : "Loading..." } </ p >
3577 )
3678 }
3779}
You can’t perform that action at this time.
0 commit comments