Skip to content

Commit 01ce4eb

Browse files
29 - Dynamically Load More Posts
1 parent 49fe7e3 commit 01ce4eb

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ class PostDetail extends Component {
8888
pathname: `/posts`,
8989
state: { fromDashboard: false }
9090
}}>Posts</Link>
91-
<Link maintainScrollPosition={false} to={{
91+
92+
{post.owner === true ? <Link maintainScrollPosition={false} to={{
9293
pathname: `/posts/create/`,
9394
state: { fromDashboard: false }
94-
}}>Create Post</Link>
95+
}}>Create Post</Link> : "" }
9596
</p>
9697

9798
{post.owner === true ? <PostForm post={post} postItemUpdated={this.handlePostItemUpdated} /> : ""}

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,57 @@ class Posts extends Component {
1010
super(props)
1111
this.togglePostListClass = this.togglePostListClass.bind(this)
1212
this.handleNewPost = this.handleNewPost.bind(this)
13+
this.loadMorePosts =this.loadMorePosts.bind(this)
14+
this.state = {
15+
posts: [],
16+
postsListClass: "card",
17+
next: null,
18+
previous: null,
19+
author: false,
20+
count: 0
21+
}
1322
}
14-
state = {
15-
posts: [],
16-
postsListClass: "card",
23+
24+
loadMorePosts(){
25+
const {next} = this.state
26+
if (next !== null || next !== undefined) {
27+
this.loadPosts(next)
28+
}
29+
1730
}
18-
loadPosts(){
19-
const endpoint = '/api/posts/'
31+
32+
loadPosts(nextEndpoint){
33+
let endpoint = '/api/posts/'
34+
if (nextEndpoint !== undefined) {
35+
endpoint = nextEndpoint
36+
}
2037
let thisComp = this
2138
let lookupOptions = {
2239
method: "GET",
2340
headers: {
2441
'Content-Type': 'application/json'
2542
}
2643
}
44+
const csrfToken = cookie.load('csrftoken')
45+
if (csrfToken !== undefined) {
46+
lookupOptions['credentials'] = 'include'
47+
lookupOptions['headers']['X-CSRFToken'] = csrfToken
48+
}
2749

2850
fetch(endpoint, lookupOptions)
2951
.then(function(response){
3052
return response.json()
3153
}).then(function(responseData){
3254
console.log(responseData)
55+
// let currentPosts = thisComp.state.posts
56+
// let newPosts = currentPosts.concat(responseData.results)
57+
// console.log(currentPosts)
3358
thisComp.setState({
34-
posts: responseData
59+
posts: thisComp.state.posts.concat(responseData.results),
60+
next: responseData.next,
61+
previous: responseData.previous,
62+
author: responseData.author,
63+
count: responseData.count
3564
})
3665
}).catch(function(error){
3766
console.log("error", error)
@@ -68,27 +97,32 @@ class Posts extends Component {
6897
this.setState({
6998
posts: [],
7099
postsListClass: "card",
100+
next: null,
101+
previous: null,
102+
author: false,
103+
count: 0
71104
})
72105
this.loadPosts()
73106
}
74107
render() {
75108
const {posts} = this.state
76109
const {postsListClass} = this.state
77-
const csrfToken = cookie.load('csrftoken')
110+
const {author} = this.state
111+
const {next} = this.state
78112
return (
79113
<div>
80-
<h1>Hello World</h1>
81-
<Link maintainScrollPosition={false} to={{
114+
{author === true ? <Link className='mr-2' maintainScrollPosition={false} to={{
82115
pathname: `/posts/create/`,
83116
state: { fromDashboard: false }
84-
}}>Create Post</Link>
117+
}}>Create Post</Link> : ""}
85118

86119
<button onClick={this.togglePostListClass}>Toggle Class</button>
87120
{posts.length > 0 ? posts.map((postItem, index)=>{
88121
return (
89122
<PostInline post={postItem} elClass={postsListClass} />
90123
)
91124
}) : <p>No Posts Found</p>}
125+
{next !== null ? <button onClick={this.loadMorePosts}>Load more</button> : ''}
92126
</div>
93127
);
94128
}

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)