Skip to content

Commit 909e007

Browse files
23 - Post Detial View
1 parent de0f671 commit 909e007

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/reactify-ui/src/posts/PostDetail.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff 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
}

src/staticfiles/js/reactify-django.ui.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)