Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMT-3561: Adding Pagination to Table Component #1110

Merged
merged 34 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b89cbb1
MMT-3492: Saving progress on table component
mandyparson Jan 18, 2024
10d76a5
MMT-3492: Adjusting RenderRows
mandyparson Jan 19, 2024
205f9cc
MMT-3492: Finish writing tests
mandyparson Jan 19, 2024
9f15d69
MMT-3492: Addressing PR Comments
mandyparson Jan 22, 2024
6a6405f
Merge branch 'MMT-3492' into MMT-3561
mandyparson Jan 22, 2024
4a3f8db
MMT-3492: Fixing 'data' structure
mandyparson Jan 23, 2024
3de9b6d
Merge branch 'MMT-3492' into MMT-3561
mandyparson Jan 23, 2024
4a6d7ac
MMT-3561: Adding Pagination to Table
mandyparson Jan 24, 2024
e003d4a
MMT-3492: eslint error fixes
mandyparson Jan 24, 2024
bd18c78
Merge branch 'MMT-3390' into MMT-3492
mandyparson Jan 24, 2024
0a2ba21
MMT-3492: reverting changes to ManagePage
mandyparson Jan 24, 2024
94fc6af
Merge branch 'MMT-3390' into MMT-3561
mandyparson Jan 24, 2024
3fa0aec
MMT-3561: Adding Pagination to Table
mandyparson Jan 25, 2024
3a1d179
MMT-3561: Adding tests
mandyparson Jan 26, 2024
abba026
Merge branch 'MMT-3390' into MMT-3561
mandyparson Jan 26, 2024
606cf0d
Merge branch 'MMT-3390' into MMT-3492
mandyparson Jan 26, 2024
d8de076
MMT-3492: Fixing shortName and title
mandyparson Jan 26, 2024
bcef541
Merge branch 'MMT-3492' into MMT-3561
mandyparson Jan 26, 2024
368a920
MMT-3561: Fixing conflict
mandyparson Jan 26, 2024
9825156
Merge branch 'MMT-3390' into MMT-3561
mandyparson Jan 26, 2024
7feb925
Merge branch 'MMT-3390' into MMT-3561
mandyparson Jan 26, 2024
c059d73
MMT-3561: Saving progress on limit and offset
mandyparson Jan 26, 2024
9fb74c9
MMT-3561: Adding offset and limit
mandyparson Jan 29, 2024
607d34b
MMT-3492: Editing table and ManagePage
mandyparson Jan 31, 2024
23b301b
Merge branch 'MMT-3390' into MMT-3492
mandyparson Jan 31, 2024
8cad2e6
MMT-3492: Trying to get tests to run on github
mandyparson Jan 31, 2024
66ce18e
Merge branch 'MMT-3492' into MMT-3561
mandyparson Jan 31, 2024
5190f2f
MMT-3492: Addressing comments
mandyparson Feb 1, 2024
17c78c5
MMT-3492: Changing all conceptTypes to ummMetadata
mandyparson Feb 1, 2024
a57b864
MMT-3492: lint
mandyparson Feb 1, 2024
e0b8e6e
Merge branch 'MMT-3492' into MMT-3561
mandyparson Feb 1, 2024
a6e54e2
MMT-3561: Writing tests for pagination
mandyparson Feb 1, 2024
868cbae
MMT-3561: Addressing Comments
mandyparson Feb 2, 2024
ac94889
Merge branch 'MMT-3390' into MMT-3561
mandyparson Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"bootstrap": "^5.3.2",
"classnames": "^2.3.2",
"commafy": "^0.0.6",
mandyparson marked this conversation as resolved.
Show resolved Hide resolved
"commafy-anything": "^3.0.4",
"compact-object-deep": "^1.0.0",
"esbuild": "^0.19.5",
"graphql": "^16.8.1",
Expand Down
3 changes: 2 additions & 1 deletion static.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"apiHost": "http://localhost:4001/dev",
"graphQlHost": "http://localhost:3013/dev/api",
"cmrHost": "http://localhost:4000",
"version": "development"
"version": "development",
"defaultPageSize": 20
},
"ummVersions": {
"ummC": "1.17.3",
Expand Down
2 changes: 1 addition & 1 deletion static/src/css/vendor/bootstrap/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
// @import "bootstrap/scss/card";
@import "bootstrap/scss/breadcrumb";
@import "bootstrap/scss/accordion";
// @import "bootstrap/scss/pagination";
@import "bootstrap/scss/pagination";
@import "bootstrap/scss/badge";
@import "bootstrap/scss/alert";
// @import "bootstrap/scss/progress";
Expand Down
17 changes: 2 additions & 15 deletions static/src/js/components/DraftList/DraftList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PropTypes from 'prop-types'
import { Link, useParams } from 'react-router-dom'
import { useLazyQuery } from '@apollo/client'
import { FaFileDownload } from 'react-icons/fa'
import pluralize from 'pluralize'
import Col from 'react-bootstrap/Col'
import Placeholder from 'react-bootstrap/Placeholder'
import Row from 'react-bootstrap/Row'
Expand Down Expand Up @@ -135,33 +134,21 @@ const DraftList = ({ draftType }) => {
<>
{
loading
? (
&& (
<span className="d-block mb-3">
<Placeholder as="span" animation="glow">
<Placeholder xs={2} />
</Placeholder>
</span>
)
: (
<span className="d-block mb-3">
Showing
{' '}
{count > 0 && 'all'}
{' '}
{count}
{' '}
{draftType}
{' '}
{pluralize('Draft', count)}
</span>
)
}
<Table
headers={['Short Name', 'Entry Title', 'Last Modified', 'Actions']}
classNames={['col-md-4', 'col-md-4', 'col-auto', 'col-auto']}
loading={loading}
data={data}
error={noDraftsError}
count={count}
/>
</>
)
Expand Down
198 changes: 182 additions & 16 deletions static/src/js/components/Table/Table.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import React from 'react'
import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import BootstrapTable from 'react-bootstrap/Table'
import Pagination from 'react-bootstrap/Pagination'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
import Container from 'react-bootstrap/Container'
import Placeholder from 'react-bootstrap/Placeholder'
import config from '../../../../../static.config.json'

import For from '../For/For'

const { application } = config
const { defaultPageSize } = application
dmistry1 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Table
* @typedef {Object} Table
Expand All @@ -15,12 +22,139 @@ import For from '../For/For'
*/

const Table = ({
headers, classNames, loading, data, error
headers, classNames, loading, data, error, count
}) => {
const [rowCount, setRowCount] = useState(0)
const [pageNum, setPageNum] = useState(1)
const [currentCount, setCurrentCount] = useState(defaultPageSize)

// Start and end index of the current page of rows
const startIndex = (pageNum - 1) * defaultPageSize
const endIndex = Math.min(startIndex + defaultPageSize, rowCount)

// Current page of rows
const pagedRows = data ? data.slice(startIndex, endIndex) : []

const lastPageNum = parseInt(Math.ceil(rowCount / defaultPageSize), 10)

// // Does this provider have enough Rows to page? We hide the pagination buttons if not
const hasPages = rowCount > defaultPageSize

const defaultPaginationStyles = {
minWidth: '2.5rem',
textAlign: 'center'
}

useEffect(() => {
if (!loading) {
setRowCount(data.length)
}
}, [loading, data])

const renderHeaders = headers.map((header) => (
<th key={header}>{header}</th>
))

const handleItemClick = (currentPage) => {
setPageNum(currentPage)
setCurrentCount(currentPage * defaultPageSize)
}

const generatePaginationItems = () => {
// Only show 3 pages, the current page and one before or after (within the valid range of pages)
const pages = [pageNum - 1, pageNum, pageNum + 1]
.filter((page) => page >= 1 && page <= lastPageNum)

const returnItems = []

// If the first page is not 1, add the pagination item for page 1
if (pages[0] !== 1) {
returnItems.push(
<Pagination.Item
key="page-1"
onClick={() => handleItemClick(1)}
active={pageNum === 1}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coverage is saying this line is uncovered in your tests

style={defaultPaginationStyles}
>
{1}
</Pagination.Item>
)

// And if the first page is not 2, add an ellipsis
if (pages[0] !== 2) {
returnItems.push(
<Pagination.Ellipsis
key="page-ellipsis-1"
disabled
/>
)
}
}

pages.forEach((page) => {
returnItems.push(
<Pagination.Item
key={`page-${page}`}
onClick={() => handleItemClick(page)}
active={page === pageNum}
style={defaultPaginationStyles}
>
{page}
</Pagination.Item>
)
})

// If the last page is not lastPageNum, add the pagination item for the lastPageNum
if (pages[pages.length - 1] !== lastPageNum) {
// And if the last page is not lastPageNum - 1, add an ellipsis
if (pages[pages.length - 1] !== lastPageNum - 1) {
returnItems.push(
<Pagination.Ellipsis
key="page-ellipsis-2"
disabled
/>
)
}

returnItems.push(
<Pagination.Item
key={`page-${lastPageNum}`}
onClick={() => handleItemClick(lastPageNum)}
active={pageNum === lastPageNum}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also this line.

style={defaultPaginationStyles}
>
{lastPageNum}
</Pagination.Item>
)
}

return returnItems
}

const handlePageChange = (direction) => {
const newPageNum = pageNum + direction
const newCurrentCount = currentCount + direction * defaultPageSize

setPageNum(newPageNum)
setCurrentCount(newCurrentCount)
}

const pagination = (
<Pagination>
Copy link
Collaborator

@cgokey cgokey Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this class is getting pretty long, wonder if it would be better to pull it out into a separate component? Just the pagination piece.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I had a comment about breaking it up.

<Pagination.Prev
disabled={pageNum === 1}
onClick={() => handlePageChange(-1)}
/>

{generatePaginationItems()}

<Pagination.Next
onClick={() => handlePageChange(1)}
disabled={pageNum >= lastPageNum}
/>
</Pagination>
)

const content = []

if (loading) {
Expand Down Expand Up @@ -51,8 +185,8 @@ const Table = ({
}
</For>
)
} else if (data.length > 0) {
const rowData = data.map((row) => {
} else if (rowCount > 0) {
const rowData = pagedRows.map((row) => {
const { cells, key } = row
const rowKey = key

Expand Down Expand Up @@ -80,23 +214,54 @@ const Table = ({
}

return (
<BootstrapTable striped>
<thead>
<tr>
{renderHeaders}
</tr>
</thead>
<tbody>
{content}
</tbody>
</BootstrapTable>
<Container>
<Row>
<Col>
<span className="d-block mb-3">
Showing
{' '}
mandyparson marked this conversation as resolved.
Show resolved Hide resolved
{count > 0 && (currentCount - defaultPageSize)}
-
{currentCount}
{' '}
of
{' '}
{count}
{' '}
Results
</span>
</Col>
</Row>
<Row>
<Col>
<BootstrapTable striped>
<thead>
<tr>
{renderHeaders}
</tr>
</thead>
<tbody>
{content}
</tbody>
</BootstrapTable>
</Col>
</Row>
<Row className="mt-2 justify-content-md-center">
<Col xs="auto">
<div className="mx-auto">
{hasPages && pagination}
</div>
</Col>
</Row>
</Container>
)
}

Table.defaultProps = {
classNames: [],
loading: null,
error: 'No Data to Display'
error: 'No Data to Display',
count: null
mandyparson marked this conversation as resolved.
Show resolved Hide resolved
}

Table.propTypes = {
Expand All @@ -107,7 +272,8 @@ Table.propTypes = {
key: PropTypes.string,
cells: PropTypes.arrayOf(PropTypes.shape({}))
})).isRequired,
error: PropTypes.string
error: PropTypes.string,
count: PropTypes.number
}

export default Table
Loading
Loading