1
- import React from 'react' ;
2
- import { useHistory } from " react-router-dom" ;
1
+ import React , { useState , useEffect } from 'react'
2
+ import { useHistory , useLocation } from ' react-router-dom'
3
3
import {
4
4
CBadge ,
5
5
CCard ,
6
6
CCardBody ,
7
7
CCardHeader ,
8
8
CCol ,
9
9
CDataTable ,
10
- CRow
11
- } from '@coreui/react' ;
10
+ CRow ,
11
+ CPagination
12
+ } from '@coreui/react'
12
13
13
14
import usersData from './UsersData'
14
15
15
- const getBadge = ( status ) => {
16
+ const getBadge = status => {
16
17
switch ( status ) {
17
18
case 'Active' : return 'success'
18
19
case 'Inactive' : return 'secondary'
@@ -24,6 +25,18 @@ const getBadge = (status) => {
24
25
25
26
const Users = ( ) => {
26
27
const history = useHistory ( )
28
+ const queryPage = useLocation ( ) . search . match ( / p a g e = ( [ 0 - 9 ] + ) / , '' )
29
+ const currentPage = Number ( queryPage && queryPage [ 1 ] ? queryPage [ 1 ] : 1 )
30
+ const [ page , setPage ] = useState ( currentPage )
31
+
32
+ const pageChange = newPage => {
33
+ currentPage !== newPage && history . push ( `/users?page=${ newPage } ` )
34
+ }
35
+
36
+ useEffect ( ( ) => {
37
+ currentPage !== page && setPage ( currentPage )
38
+ } , [ currentPage , page ] )
39
+
27
40
return (
28
41
< CRow >
29
42
< CCol xl = { 6 } >
@@ -37,13 +50,14 @@ const Users = () => {
37
50
items = { usersData }
38
51
fields = { [
39
52
{ key : 'name' , _classes : 'font-weight-bold' } ,
40
- 'registered' , 'role' , 'status' ] }
53
+ 'registered' , 'role' , 'status'
54
+ ] }
41
55
hover
42
56
striped
43
- pagination = { { doubleArrows : false , align : 'center' } }
44
57
itemsPerPage = { 5 }
58
+ activePage = { page }
45
59
clickableRows
46
- onRowClick = { ( item , index ) => history . push ( `/users/${ item . id } ` ) }
60
+ onRowClick = { ( item ) => history . push ( `/users/${ item . id } ` ) }
47
61
scopedSlots = { {
48
62
'status' :
49
63
( item ) => (
@@ -55,6 +69,13 @@ const Users = () => {
55
69
)
56
70
} }
57
71
/>
72
+ < CPagination
73
+ activePage = { page }
74
+ onActivePageChange = { pageChange }
75
+ pages = { 5 }
76
+ doubleArrows = { false }
77
+ align = "center"
78
+ />
58
79
</ CCardBody >
59
80
</ CCard >
60
81
</ CCol >
0 commit comments