From 989930633a979d2cd7851d8a722f0db532df1feb Mon Sep 17 00:00:00 2001 From: Georgy Karataev Date: Fri, 15 Jul 2022 16:17:18 +0200 Subject: [PATCH] fix(Recommendations list): map sorting parameters from column names in the recs list table (#359) --- src/AppConstants.js | 13 ++++-- .../RecsListTable/RecsListTable.cy.js | 2 +- src/Components/RecsListTable/RecsListTable.js | 40 +++++++++++++++---- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/AppConstants.js b/src/AppConstants.js index e400f35d..00fee69f 100644 --- a/src/AppConstants.js +++ b/src/AppConstants.js @@ -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; diff --git a/src/Components/RecsListTable/RecsListTable.cy.js b/src/Components/RecsListTable/RecsListTable.cy.js index 74942066..526c97b5 100644 --- a/src/Components/RecsListTable/RecsListTable.cy.js +++ b/src/Components/RecsListTable/RecsListTable.cy.js @@ -480,7 +480,7 @@ describe('successful non-empty recommendations list table', () => { 'Name', 'description', DEFAULT_DISPLAYED_SIZE, - category + label ); }); }); diff --git a/src/Components/RecsListTable/RecsListTable.js b/src/Components/RecsListTable/RecsListTable.js index bb0ffa2b..2f8103b4 100644 --- a/src/Components/RecsListTable/RecsListTable.js +++ b/src/Components/RecsListTable/RecsListTable.js @@ -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'; @@ -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(