feat: add query parameters to Users view

This commit is contained in:
woothu 2020-06-10 10:27:40 +02:00
parent 8e4fbc2aa8
commit 98f8b677ed

View File

@ -1,5 +1,5 @@
import React from 'react'; import React, { useState, useEffect } from 'react'
import { useHistory } from "react-router-dom"; import { useHistory, useLocation } from 'react-router-dom'
import { import {
CBadge, CBadge,
CCard, CCard,
@ -7,12 +7,13 @@ import {
CCardHeader, CCardHeader,
CCol, CCol,
CDataTable, CDataTable,
CRow CRow,
} from '@coreui/react'; CPagination
} from '@coreui/react'
import usersData from './UsersData' import usersData from './UsersData'
const getBadge = (status) => { const getBadge = status => {
switch (status) { switch (status) {
case 'Active': return 'success' case 'Active': return 'success'
case 'Inactive': return 'secondary' case 'Inactive': return 'secondary'
@ -24,6 +25,18 @@ const getBadge = (status) => {
const Users = () => { const Users = () => {
const history = useHistory() const history = useHistory()
const queryPage = useLocation().search.match(/page=([0-9]+)/, '')
const currentPage = Number(queryPage && queryPage[1] ? queryPage[1] : 1)
const [page, setPage] = useState(currentPage)
const pageChange = newPage => {
currentPage !== newPage && history.push(`/users?page=${newPage}`)
}
useEffect(() => {
currentPage !== page && setPage(currentPage)
}, [currentPage, page])
return ( return (
<CRow> <CRow>
<CCol xl={6}> <CCol xl={6}>
@ -37,13 +50,14 @@ const Users = () => {
items={usersData} items={usersData}
fields={[ fields={[
{ key: 'name', _classes: 'font-weight-bold' }, { key: 'name', _classes: 'font-weight-bold' },
'registered', 'role', 'status']} 'registered', 'role', 'status'
]}
hover hover
striped striped
pagination={{ doubleArrows: false, align: 'center' }}
itemsPerPage={5} itemsPerPage={5}
activePage={page}
clickableRows clickableRows
onRowClick={(item, index) => history.push(`/users/${item.id}`)} onRowClick={(item) => history.push(`/users/${item.id}`)}
scopedSlots = {{ scopedSlots = {{
'status': 'status':
(item)=>( (item)=>(
@ -55,6 +69,13 @@ const Users = () => {
) )
}} }}
/> />
<CPagination
activePage={page}
onActivePageChange={pageChange}
pages={5}
doubleArrows={false}
align="center"
/>
</CCardBody> </CCardBody>
</CCard> </CCard>
</CCol> </CCol>