-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add sorting to my alerts #212
Changes from 11 commits
76b9db0
136c26b
30a3e00
38122f3
5da7ea5
3295a58
060ba3b
a8ceece
dddc2eb
670723e
b415fc0
bdd7770
6faee0e
3b2ab52
f26c455
fd6ed45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable jsx-a11y/anchor-is-valid */ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Tag, Table } from '@trussworks/react-uswds'; | ||
|
@@ -51,12 +52,16 @@ function renderReports(reports) { | |
</Tag> | ||
)); | ||
|
||
const idKey = `my_alerts_${id}`; | ||
const idLink = `/activity-reports/${id}`; | ||
const statusClassName = `smart-hub--table-tag-status smart-hub--status-${status}`; | ||
|
||
return ( | ||
<tr key={`my_alerts_${id}`}> | ||
<tr key={idKey}> | ||
<td> | ||
<Link | ||
to={`/activity-reports/${id}`} | ||
href={`/activity-reports/${id}`} | ||
to={idLink} | ||
href={idLink} | ||
> | ||
{displayId} | ||
</Link> | ||
|
@@ -79,7 +84,7 @@ function renderReports(reports) { | |
</td> | ||
<td> | ||
<Tag | ||
className={`smart-hub--table-tag-status smart-hub--status-${status}`} | ||
className={statusClassName} | ||
> | ||
{status === 'needs_action' ? 'Needs action' : status} | ||
</Tag> | ||
|
@@ -89,51 +94,121 @@ function renderReports(reports) { | |
}); | ||
} | ||
|
||
function MyAlerts({ reports, newBtn }) { | ||
export function renderTotal(offset, perPage, activePage, reportsCount) { | ||
const from = offset >= reportsCount ? 0 : offset + 1; | ||
const offsetTo = perPage * activePage; | ||
let to; | ||
if (offsetTo > reportsCount) { | ||
to = reportsCount; | ||
} else { | ||
to = offsetTo; | ||
} | ||
return `${from}-${to} of ${reportsCount}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's hide this until pagination is active. With 11 reports the table is displaying all 11 (good, since otherwise one would be inaccessible) but this says |
||
} | ||
|
||
function MyAlerts(props) { | ||
const { | ||
reports, | ||
newBtn, | ||
alertsSortConfig, | ||
alertsOffset, | ||
alertsPerPage, | ||
alertsActivePage, | ||
alertReportsCount, | ||
sortHandler, | ||
} = props; | ||
const getClassNamesFor = (name) => (alertsSortConfig.sortBy === name ? alertsSortConfig.direction : ''); | ||
|
||
const renderColumnHeader = (displayName, name) => { | ||
const sortClassName = getClassNamesFor(name); | ||
let fullAriaSort; | ||
switch (sortClassName) { | ||
case 'asc': | ||
fullAriaSort = 'ascending'; | ||
break; | ||
case 'desc': | ||
fullAriaSort = 'descending'; | ||
break; | ||
default: | ||
fullAriaSort = 'none'; | ||
break; | ||
} | ||
return ( | ||
<th scope="col" aria-sort={fullAriaSort}> | ||
<a | ||
role="button" | ||
tabIndex={0} | ||
onClick={() => { | ||
sortHandler(name); | ||
}} | ||
onKeyPress={() => sortHandler(name)} | ||
className={sortClassName} | ||
aria-label={`${displayName}. Activate to sort ${ | ||
sortClassName === 'asc' ? 'descending' : 'ascending' | ||
}`} | ||
> | ||
{displayName} | ||
</a> | ||
</th> | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
{ reports && reports.length === 0 && ( | ||
<Container className="landing" padding={0}> | ||
<div id="caughtUp"> | ||
<div><h2>You're all caught up!</h2></div> | ||
{ newBtn && <p id="beginNew">Would you like to begin a new activity report?</p> } | ||
{ newBtn && <NewReport /> } | ||
</div> | ||
</Container> | ||
) } | ||
{ reports && reports.length > 0 && ( | ||
<SimpleBar> | ||
<Container className="landing inline-size" padding={0}> | ||
<Table bordered={false}> | ||
<caption className="smart-hub--table-caption"> | ||
My activity report alerts | ||
</caption> | ||
<thead> | ||
<tr> | ||
<th scope="col">Report ID</th> | ||
<th | ||
scope="col" | ||
> | ||
Grantee | ||
</th> | ||
<th scope="col">Start date</th> | ||
<th | ||
scope="col" | ||
> | ||
Creator | ||
</th> | ||
<th scope="col">Collaborator(s)</th> | ||
<th | ||
scope="col" | ||
> | ||
Status | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody>{renderReports(reports)}</tbody> | ||
</Table> | ||
{reports && reports.length === 0 && ( | ||
<Container className="landing" padding={0}> | ||
<div id="caughtUp"> | ||
<div> | ||
<h2>You're all caught up!</h2> | ||
</div> | ||
{newBtn && ( | ||
<p id="beginNew"> | ||
Would you like to begin a new activity report? | ||
</p> | ||
)} | ||
{newBtn && <NewReport />} | ||
</div> | ||
</Container> | ||
</SimpleBar> | ||
)} | ||
{reports && reports.length > 0 && ( | ||
<SimpleBar> | ||
<Container className="landing inline-size" padding={0}> | ||
<span | ||
id="alertsTotalCount" | ||
aria-label={`Displaying rows ${renderTotal( | ||
alertsOffset, | ||
alertsPerPage, | ||
alertsActivePage, | ||
alertReportsCount, | ||
)}`} | ||
> | ||
{renderTotal( | ||
alertsOffset, | ||
alertsPerPage, | ||
alertsActivePage, | ||
alertReportsCount, | ||
)} | ||
</span> | ||
|
||
<Table bordered={false}> | ||
<caption className="smart-hub--table-caption"> | ||
My activity report alerts | ||
<p id="arTblDesc">with sorting</p> | ||
</caption> | ||
<thead> | ||
<tr> | ||
{renderColumnHeader('Report ID', 'regionId')} | ||
{renderColumnHeader('Grantee', 'activityRecipients')} | ||
{renderColumnHeader('Start date', 'startDate')} | ||
{renderColumnHeader('Creator', 'author')} | ||
{renderColumnHeader('Collaborator(s)', 'collaborators')} | ||
{renderColumnHeader('Status', 'status')} | ||
</tr> | ||
</thead> | ||
<tbody>{renderReports(reports)}</tbody> | ||
</Table> | ||
</Container> | ||
</SimpleBar> | ||
)} | ||
</> | ||
); | ||
|
@@ -142,10 +217,20 @@ function MyAlerts({ reports, newBtn }) { | |
MyAlerts.propTypes = { | ||
reports: PropTypes.arrayOf(PropTypes.object), | ||
newBtn: PropTypes.bool.isRequired, | ||
alertsSortConfig: PropTypes.shape({ sortBy: PropTypes.string, direction: PropTypes.string }), | ||
alertsOffset: PropTypes.number, | ||
alertsPerPage: PropTypes.number, | ||
alertsActivePage: PropTypes.number, | ||
alertReportsCount: PropTypes.number.isRequired, | ||
sortHandler: PropTypes.func.isRequired, | ||
}; | ||
|
||
MyAlerts.defaultProps = { | ||
reports: [], | ||
alertsSortConfig: { sortBy: 'startDate', direction: 'asc' }, | ||
alertsOffset: 0, | ||
alertsPerPage: 7, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this default to 10? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to 10, but right now since the pagination is not working it will be a placeholder only until we find a way to integrate the pagination with sorting (HHS#369) |
||
alertsActivePage: 1, | ||
}; | ||
|
||
export default MyAlerts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are both
to
andhref
required?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed href.