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'
33import {
44 CBadge ,
55 CCard ,
66 CCardBody ,
77 CCardHeader ,
88 CCol ,
99 CDataTable ,
10- CRow
11- } from '@coreui/react' ;
10+ CRow ,
11+ CPagination
12+ } from '@coreui/react'
1213
1314import usersData from './UsersData'
1415
15- const getBadge = ( status ) => {
16+ const getBadge = status => {
1617 switch ( status ) {
1718 case 'Active' : return 'success'
1819 case 'Inactive' : return 'secondary'
@@ -24,6 +25,18 @@ const getBadge = (status) => {
2425
2526const Users = ( ) => {
2627 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+
2740 return (
2841 < CRow >
2942 < CCol xl = { 6 } >
@@ -37,13 +50,14 @@ const Users = () => {
3750 items = { usersData }
3851 fields = { [
3952 { key : 'name' , _classes : 'font-weight-bold' } ,
40- 'registered' , 'role' , 'status' ] }
53+ 'registered' , 'role' , 'status'
54+ ] }
4155 hover
4256 striped
43- pagination = { { doubleArrows : false , align : 'center' } }
4457 itemsPerPage = { 5 }
58+ activePage = { page }
4559 clickableRows
46- onRowClick = { ( item , index ) => history . push ( `/users/${ item . id } ` ) }
60+ onRowClick = { ( item ) => history . push ( `/users/${ item . id } ` ) }
4761 scopedSlots = { {
4862 'status' :
4963 ( item ) => (
@@ -55,6 +69,13 @@ const Users = () => {
5569 )
5670 } }
5771 />
72+ < CPagination
73+ activePage = { page }
74+ onActivePageChange = { pageChange }
75+ pages = { 5 }
76+ doubleArrows = { false }
77+ align = "center"
78+ />
5879 </ CCardBody >
5980 </ CCard >
6081 </ CCol >
0 commit comments