File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,19 @@ const BlogList = ({ isAdmin }) => {
11
11
const [ posts , setPosts ] = useState ( [ ] ) ;
12
12
const [ loading , setLoading ] = useState ( true ) ;
13
13
const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
14
+ const [ numberOfPosts , setNumberOfPosts ] = useState ( 0 ) ;
15
+ const [ numberOfPages , setNumberOfPages ] = useState ( 0 ) ;
16
+ const limit = 5 ;
17
+
18
+ useEffect ( ( ) => {
19
+ setNumberOfPages ( Math . ceil ( numberOfPosts / limit ) ) ;
20
+ } , [ numberOfPosts ] ) ;
14
21
15
22
const getPosts = ( page = 1 ) => {
16
23
setCurrentPage ( page ) ;
17
24
let params = {
18
25
_page : page ,
19
- _limit : 5 ,
26
+ _limit : limit ,
20
27
_sort : 'id' ,
21
28
_order : 'desc' ,
22
29
}
@@ -28,6 +35,7 @@ const BlogList = ({ isAdmin }) => {
28
35
axios . get ( `http://localhost:3001/posts` , {
29
36
params
30
37
} ) . then ( ( res ) => {
38
+ setNumberOfPosts ( res . headers [ 'x-total-count' ] ) ;
31
39
setPosts ( res . data ) ;
32
40
setLoading ( false ) ;
33
41
} )
@@ -78,11 +86,11 @@ const BlogList = ({ isAdmin }) => {
78
86
return (
79
87
< div >
80
88
{ renderBlogList ( ) }
81
- < Pagination
89
+ { numberOfPages > 1 && < Pagination
82
90
currentPage = { currentPage }
83
- numberOfPages = { 3 }
91
+ numberOfPages = { numberOfPages }
84
92
onClick = { getPosts }
85
- />
93
+ /> }
86
94
</ div >
87
95
)
88
96
} ;
You can’t perform that action at this time.
0 commit comments