Skip to content

Commit

Permalink
WIP: Add sorting options [gh-1138]
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbiang888 committed Dec 29, 2020
1 parent f9f09d1 commit 61aa5a7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/controller/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion api/models/entity/Contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}}
Expand Down
12 changes: 11 additions & 1 deletion app/src/state/ducks/contributions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand Down

0 comments on commit 61aa5a7

Please sign in to comment.