From 61aa5a755f7baa64bfff1b3b106ef547c50add0b Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 29 Dec 2020 13:03:27 -0800 Subject: [PATCH] WIP: Add sorting options [gh-1138] --- api/controller/contributions.ts | 2 ++ api/models/entity/Contribution.ts | 3 ++- .../ContributionsTable/ContributionsTable.js | 5 ++++- app/src/state/ducks/contributions.js | 12 +++++++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/api/controller/contributions.ts b/api/controller/contributions.ts index 1d4aeb75f..a61da0fc8 100644 --- a/api/controller/contributions.ts +++ b/api/controller/contributions.ts @@ -232,6 +232,7 @@ export class GetContributionsDto implements IGetContributionAttrs { export async function getContributions(request: IRequest, response: Response, next: Function) { try { + // TODO: update for additional fields checkCurrentUser(request); const getContributionsDto = Object.assign(new GetContributionsDto(), { ...request.body, @@ -249,6 +250,7 @@ export async function getContributions(request: IRequest, response: Response, ne } return response.status(200).send(JSON.stringify(contributions)); } catch (err) { + console.log(request.body, err); if (process.env.NODE_ENV === 'production' && err.message !== 'No token set') { bugsnagClient.notify(err); } diff --git a/api/models/entity/Contribution.ts b/api/models/entity/Contribution.ts index 63ee5a6c9..508883cdc 100644 --- a/api/models/entity/Contribution.ts +++ b/api/models/entity/Contribution.ts @@ -661,7 +661,8 @@ export async function getContributionsByGovernmentIdAsync( } }; if (sort) { - if (!['date', 'status', 'campaignId', 'matchAmount', 'amount'].includes(sort.field)) { + // TODO: update here + if (!['date', 'status', 'campaignId', 'matchAmount', 'amount', 'oaeType'].includes(sort.field)) { throw new Error('Sort.field must be one of date, status, matchAmount, amount or campaignid'); } diff --git a/app/src/Pages/Portal/Contributions/ContributionsTable/ContributionsTable.js b/app/src/Pages/Portal/Contributions/ContributionsTable/ContributionsTable.js index 7de7912da..494a344e3 100644 --- a/app/src/Pages/Portal/Contributions/ContributionsTable/ContributionsTable.js +++ b/app/src/Pages/Portal/Contributions/ContributionsTable/ContributionsTable.js @@ -259,15 +259,18 @@ class ContributionsTable extends React.Component { data={contributionList} onOrderChange={(item, direction) => { const column = columns(isGovAdmin)[item]; + console.log(item, direction, column); let sortOptions = {}; if (column) { + const sortDirection = direction.toUpperCase(); sortOptions = { sort: { field: column.field, - direction: direction.toUpperCase(), + direction: sortDirection, // TODO: is this actually toggling? }, }; } + // TODO: sort here: updateFilter(sortOptions); fetchList(); }} diff --git a/app/src/state/ducks/contributions.js b/app/src/state/ducks/contributions.js index 872bed9ba..5a61726fb 100644 --- a/app/src/state/ducks/contributions.js +++ b/app/src/state/ducks/contributions.js @@ -196,9 +196,19 @@ export function updateFilter(newFilterOptions) { (filterOptions.page * filterOptions.perPage) / newFilterOptions.perPage ); } + const existingSortField = filterOptions.sort; + const newSortField = newFilterOptions.sort; + if ( + existingSortField.field === newSortField.field && + existingSortField.direction === newSortField.direction + ) { + const isAsc = existingSortField.direction === 'ASC'; + newFilterOptions.sort.direction = isAsc ? 'DESC' : 'ASC'; + } Object.entries(filterOptions).forEach(([key, value]) => { - if (Object.prototype.hasOwnProperty.call(newFilterOptions, key)) + if (Object.prototype.hasOwnProperty.call(newFilterOptions, key)) { filterOptions[key] = newFilterOptions[key]; + } }); dispatch(actionCreators.filter.update(filterOptions)); };