Skip to content

Commit

Permalink
fix(code): ADDON-47626 Fixed the sorting issue for custom mapping (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbalar-splunk authored Oct 14, 2022
1 parent 8a8e802 commit 97b5cb2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@
"engines": {
"node": ">=6"
}
}
}
3 changes: 2 additions & 1 deletion ui/src/main/webapp/components/table/CustomTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function CustomTable({
column.push({
...item,
sortKey: item.field || null,
isCustomMapping: item.mapping ? true : false
});
});
}
Expand All @@ -203,7 +204,7 @@ function CustomTable({
columns.map((headData) => (
<Table.HeadCell
key={headData.field}
onSort={headData.sortKey ? handleSort : null}
onSort={(e) => headData.sortKey ? handleSort(e, headData) : null}
sortKey={headData.sortKey ? headData.sortKey : null}
sortDir={
headData.sortKey && headData.sortKey === sortKey ? sortDir : 'none'
Expand Down
13 changes: 9 additions & 4 deletions ui/src/main/webapp/components/table/TableWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PAGE_INPUT } from '../../constants/pages';
function TableWrapper({ page, serviceName, handleRequestModalOpen, handleOpenPageStyleDialog }) {
const [sortKey, setSortKey] = useState('name');
const [sortDir, setSortDir] = useState('asc');
const [isCustomMapping, setCustomMappingStatus] = useState(false);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

Expand Down Expand Up @@ -45,6 +46,7 @@ function TableWrapper({ page, serviceName, handleRequestModalOpen, handleOpenPag
id: val.id,
name: val.name,
serviceName: service.name,
serviceTitle: service.title || ''
};
});
obj[service.name] = tmpObj;
Expand Down Expand Up @@ -154,6 +156,7 @@ function TableWrapper({ page, serviceName, handleRequestModalOpen, handleOpenPag
const nextSortDir = prevSortDir === 'asc' ? 'desc' : 'asc';
setSortDir(nextSortDir);
setSortKey(val.sortKey);
setCustomMappingStatus(val.isCustomMapping);
};

/**
Expand Down Expand Up @@ -206,16 +209,18 @@ function TableWrapper({ page, serviceName, handleRequestModalOpen, handleOpenPag
arr = findByMatchingValue(rowData[searchType]);
}

const _sortKey = isCustomMapping ? 'serviceTitle' : sortKey;

// Sort the array based on the sort value
const sortedArr = arr.sort((rowA, rowB) => {
if (sortDir === 'asc') {
const rowAValue = rowA[sortKey] === undefined ? '' : rowA[sortKey];
const rowBValue = rowB[sortKey] === undefined ? '' : rowB[sortKey];
const rowAValue = rowA[_sortKey] === undefined ? '' : rowA[_sortKey];
const rowBValue = rowB[_sortKey] === undefined ? '' : rowB[_sortKey];
return rowAValue > rowBValue ? 1 : -1;
}
if (sortDir === 'desc') {
const rowAValue = rowA[sortKey] === undefined ? '' : rowA[sortKey];
const rowBValue = rowB[sortKey] === undefined ? '' : rowB[sortKey];
const rowAValue = rowA[_sortKey] === undefined ? '' : rowA[_sortKey];
const rowBValue = rowB[_sortKey] === undefined ? '' : rowB[_sortKey];
return rowBValue > rowAValue ? 1 : -1;
}
return 0;
Expand Down

0 comments on commit 97b5cb2

Please sign in to comment.