Skip to content

Commit

Permalink
fix(Recommendations list): map sorting parameters from column names i…
Browse files Browse the repository at this point in the history
…n the recs list table (#359)
  • Loading branch information
gkarat authored Jul 15, 2022
1 parent b4388bf commit 9899306
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/AppConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,17 @@ export const CLUSTERS_LIST_COLUMNS = [
];
export const RECS_LIST_COLUMNS_KEYS = [
'', // reserved for expand button
'description',
'publish_date',
'tags',
'name',
'modified',
'category',
'total_risk',
'impacted_clusters_count',
'clusters',
];
export const RECS_LIST_NAME_CELL = 1;
export const RECS_LIST_MODIFIED_CELL = 2;
export const RECS_LIST_CATEGORY_CELL = 3;
export const RECS_LIST_TOTAL_RISK_CELL = 4;
export const RECS_LIST_CLUSTERS_CELL = 5;
export const AFFECTED_CLUSTERS_NAME_CELL = 1;
export const AFFECTED_CLUSTERS_VERSION_CELL = 2;
export const AFFECTED_CLUSTERS_LAST_SEEN_CELL = 3;
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RecsListTable/RecsListTable.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ describe('successful non-empty recommendations list table', () => {
'Name',
'description',
DEFAULT_DISPLAYED_SIZE,
category
label
);
});
});
Expand Down
40 changes: 33 additions & 7 deletions src/Components/RecsListTable/RecsListTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ import { addNotification } from '@redhat-cloud-services/frontend-components-noti

import {
FILTER_CATEGORIES,
RECS_LIST_CATEGORY_CELL,
RECS_LIST_CLUSTERS_CELL,
RECS_LIST_COLUMNS,
RECS_LIST_COLUMNS_KEYS,
RECS_LIST_MODIFIED_CELL,
RECS_LIST_NAME_CELL,
RECS_LIST_TOTAL_RISK_CELL,
TOTAL_RISK_LABEL_LOWER,
} from '../../AppConstants';
import messages from '../../Messages';
Expand Down Expand Up @@ -247,15 +252,36 @@ const RecsListTable = ({ query }) => {
*/
const buildDisplayedRows = (rows, index, direction) => {
const sortingRows = [...rows].sort((firstItem, secondItem) => {
let fst = firstItem[0].rule,
snd = secondItem[0].rule;
const d = direction === SortByDirection.asc ? 1 : -1;
const fst = firstItem[0].rule[RECS_LIST_COLUMNS_KEYS[index]];
const snd = secondItem[0].rule[RECS_LIST_COLUMNS_KEYS[index]];
if (index === 3) {
return (
d * extractCategories(fst)[0].localeCompare(extractCategories(snd)[0])
);
switch (index) {
case RECS_LIST_NAME_CELL:
fst = fst.description;
snd = snd.description;
return fst.localeCompare(snd) ? fst.localeCompare(snd) * d : 0;
case RECS_LIST_MODIFIED_CELL:
fst = new Date(fst.publish_date || 0);
snd = new Date(snd.publish_date || 0);
return fst > snd ? d : snd > fst ? -d : 0;
case RECS_LIST_CATEGORY_CELL:
return (
d *
extractCategories(fst.tags)[0].localeCompare(
extractCategories(snd.tags)[0]
)
);
case RECS_LIST_TOTAL_RISK_CELL:
fst = fst.total_risk;
snd = snd.total_risk;
return fst > snd ? d : snd > fst ? -d : 0;
case RECS_LIST_CLUSTERS_CELL:
fst = fst.impacted_clusters_count;
snd = snd.impacted_clusters_count;
return fst > snd ? d : snd > fst ? -d : 0;
default:
console.error('Incorrect sorting parameters received');
}
return fst > snd ? d : snd > fst ? -d : 0;
});
return sortingRows
.slice(
Expand Down

0 comments on commit 9899306

Please sign in to comment.