@@ -10,28 +10,57 @@ class Posts extends Component {
10
10
super ( props )
11
11
this . togglePostListClass = this . togglePostListClass . bind ( this )
12
12
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
+ }
13
22
}
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
+
17
30
}
18
- loadPosts ( ) {
19
- const endpoint = '/api/posts/'
31
+
32
+ loadPosts ( nextEndpoint ) {
33
+ let endpoint = '/api/posts/'
34
+ if ( nextEndpoint !== undefined ) {
35
+ endpoint = nextEndpoint
36
+ }
20
37
let thisComp = this
21
38
let lookupOptions = {
22
39
method : "GET" ,
23
40
headers : {
24
41
'Content-Type' : 'application/json'
25
42
}
26
43
}
44
+ const csrfToken = cookie . load ( 'csrftoken' )
45
+ if ( csrfToken !== undefined ) {
46
+ lookupOptions [ 'credentials' ] = 'include'
47
+ lookupOptions [ 'headers' ] [ 'X-CSRFToken' ] = csrfToken
48
+ }
27
49
28
50
fetch ( endpoint , lookupOptions )
29
51
. then ( function ( response ) {
30
52
return response . json ( )
31
53
} ) . then ( function ( responseData ) {
32
54
console . log ( responseData )
55
+ // let currentPosts = thisComp.state.posts
56
+ // let newPosts = currentPosts.concat(responseData.results)
57
+ // console.log(currentPosts)
33
58
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
35
64
} )
36
65
} ) . catch ( function ( error ) {
37
66
console . log ( "error" , error )
@@ -68,27 +97,32 @@ class Posts extends Component {
68
97
this . setState ( {
69
98
posts : [ ] ,
70
99
postsListClass : "card" ,
100
+ next : null ,
101
+ previous : null ,
102
+ author : false ,
103
+ count : 0
71
104
} )
72
105
this . loadPosts ( )
73
106
}
74
107
render ( ) {
75
108
const { posts} = this . state
76
109
const { postsListClass} = this . state
77
- const csrfToken = cookie . load ( 'csrftoken' )
110
+ const { author} = this . state
111
+ const { next} = this . state
78
112
return (
79
113
< div >
80
- < h1 > Hello World</ h1 >
81
- < Link maintainScrollPosition = { false } to = { {
114
+ { author === true ? < Link className = 'mr-2' maintainScrollPosition = { false } to = { {
82
115
pathname : `/posts/create/` ,
83
116
state : { fromDashboard : false }
84
- } } > Create Post</ Link >
117
+ } } > Create Post</ Link > : "" }
85
118
86
119
< button onClick = { this . togglePostListClass } > Toggle Class</ button >
87
120
{ posts . length > 0 ? posts . map ( ( postItem , index ) => {
88
121
return (
89
122
< PostInline post = { postItem } elClass = { postsListClass } />
90
123
)
91
124
} ) : < p > No Posts Found</ p > }
125
+ { next !== null ? < button onClick = { this . loadMorePosts } > Load more</ button > : '' }
92
126
</ div >
93
127
) ;
94
128
}
0 commit comments