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 all 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.

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
47 changes: 23 additions & 24 deletions static/src/js/components/DraftList/DraftList.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from 'react'
import React, { useState } from 'react'
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 All @@ -26,10 +25,17 @@ const DraftList = ({ draftType }) => {
const { providerId } = user
const { draftType: paramDraftType } = useParams()

const { drafts, error, loading } = useDraftsQuery({ draftType })
const [offset, setOffset] = useState(0)
const limit = 20

const { drafts, error, loading } = useDraftsQuery({
draftType,
offset,
limit
})
const { count, items = [] } = drafts

const noDraftsError = `No ${draftType} drafts exist for the provider ${providerId}`
const noDataError = `No ${draftType} drafts exist for the provider ${providerId}`

const [downloadDraft] = useLazyQuery(DOWNLOAD_DRAFT, {
onCompleted: (getDraftData) => {
Expand All @@ -56,10 +62,11 @@ const DraftList = ({ draftType }) => {
// Building a Table using Data in items
const data = (items.map((item) => {
const {
conceptId, revisionDate, previewMetadata: {
name, longName, shortName, title
}
conceptId, revisionDate, ummMetadata
} = item
const {
ShortName, EntryTitle, Name, LongName
} = ummMetadata || {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These should be separate lines. Not sure why eslint didnt complain here 🤔


const draftLink = `/drafts/${`${paramDraftType}`}/${conceptId}`

Expand All @@ -72,14 +79,14 @@ const DraftList = ({ draftType }) => {
value:
(
<Link to={draftLink}>
{name || shortName || '<Blank Name>'}
{Name || ShortName || '<Blank Name>'}
</Link>
)
},
{
value:
(
longName || title || '<Untitled Record>'
LongName || EntryTitle || '<Untitled Record>'
)
},
{
Expand Down Expand Up @@ -140,33 +147,25 @@ 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}
error={error}
noDataError={noDataError}
count={count}
setOffset={setOffset}
limit={limit}
offset={offset}
/>
</>
)
Expand Down
13 changes: 9 additions & 4 deletions static/src/js/components/DraftList/__tests__/DraftList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,19 @@ describe('DraftList', () => {
{
conceptId: 'TD1200000092-MMT_2',
revisionDate: '2023-12-08T17:56:09.385Z',
ummMetadata: {
Name: 'Tool TD1200000092 short name',
LongName: 'Tool TD1200000092 long name'
},
previewMetadata: {
name: 'Tool TD1200000092 short name',
longName: 'Tool TD1200000092 long name',
__typename: 'Tool'
},
__typename: 'Draft'
},
{
conceptId: 'TD1200000093-MMT_2',
revisionDate: '2023-11-08T17:56:09.385Z',
ummMetadata: {},
previewMetadata: {
__typename: 'Tool'
},
Expand All @@ -115,9 +118,11 @@ describe('DraftList', () => {
{
conceptId: 'TD1200000094-MMT_2',
revisionDate: '2023-10-08T17:56:09.385Z',
ummMetadata: {
Name: 'Tool TD1200000094 short name',
LongName: 'Tool TD1200000094 long name'
},
previewMetadata: {
name: 'Tool TD1200000094 short name',
longName: 'Tool TD1200000094 long name',
__typename: 'Tool'
},
__typename: 'Draft'
Expand Down
143 changes: 143 additions & 0 deletions static/src/js/components/Pagination/Pagination.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import Pagination from 'react-bootstrap/Pagination'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'

/**
* Table
* @typedef {Object} PaginationComponent
* @property {Number} limit A number that is set in parent element of table
* @property {Number} count A number that indicates how many results are in the total query
* @property {function} setOffset A function that resets the offset of results to come back
*/

const PaginationComponent = ({
limit,
count,
setOffset
}) => {
mandyparson marked this conversation as resolved.
Show resolved Hide resolved
const [pageNum, setPageNum] = useState(1)

const lastPageNum = parseInt(Math.ceil(count / limit), 10)

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

const handleItemClick = (currentPage) => {
setPageNum(currentPage)
setOffset((currentPage - 1) * limit)
}

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}
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}
style={defaultPaginationStyles}
>
{lastPageNum}
</Pagination.Item>
)
}

return returnItems
}

const handlePageChange = (direction) => {
const newCurrentPage = pageNum + direction

setPageNum(newCurrentPage)
setOffset((newCurrentPage - 1) * limit)
}

return (
<Row>
<Col xs="auto">
<div className="mx-auto">
<Pagination>
<Pagination.Prev
disabled={pageNum === 1}
onClick={() => handlePageChange(-1)}
/>
{generatePaginationItems()}
<Pagination.Next
onClick={() => handlePageChange(1)}
disabled={pageNum >= lastPageNum}
/>
</Pagination>
</div>
</Col>
</Row>
)
}

PaginationComponent.defaultProps = {
count: null
}

PaginationComponent.propTypes = {
setOffset: PropTypes.func.isRequired,
limit: PropTypes.number.isRequired,
count: PropTypes.number
}

export default PaginationComponent
61 changes: 61 additions & 0 deletions static/src/js/components/Pagination/__tests__/Pagination.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react'
import {
render,
screen,
fireEvent
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import PaginationComponent from '../Pagination'

const setOffset = jest.fn()

const setup = () => {
const props = {
limit: 2,
count: 14,
setOffset
}
render(
<PaginationComponent {...props} />
)

return {
props,
user: userEvent.setup()
}
}

describe('Pagination', () => {
describe('when pagination component is passed count < limit', () => {
test('renders pagination bar', () => {
setup()

expect(screen.queryAllByRole('button')).toHaveLength(3)

// Check individual buttons work
fireEvent.click(screen.getByRole('button', { name: '2' }))

// Check the next button works
fireEvent.click(screen.getByRole('button', { name: 'Next' }))

// // Click on Previous Page
fireEvent.click(screen.getByRole('button', { name: 'Previous' }))

// // Check pages[0] always stays at 1 and two ellipsis render
fireEvent.click(screen.getByRole('button', { name: 'Next' }))
fireEvent.click(screen.getByRole('button', { name: '4' }))
expect(screen.getByRole('button', { name: '1' }))
expect(screen.queryAllByText('More')).toHaveLength(2)

// Make sure onclick for pages[0] function above works
fireEvent.click(screen.getByRole('button', { name: '1' }))

// Can click on last page
fireEvent.click(screen.getByRole('button', { name: '7' }))
fireEvent.click(screen.getByRole('button', { name: 'Previous' }))
fireEvent.click(screen.getByRole('button', { name: 'Previous' }))
expect(screen.queryAllByText('More')).toHaveLength(1)
})
})
})
Loading
Loading