From fc7e6dbd92490557e706091a8f83296411d22453 Mon Sep 17 00:00:00 2001 From: Tomas R Date: Thu, 24 Nov 2022 17:25:03 +0100 Subject: [PATCH] Fix sorting editable list by program code (#5582) --- CHANGES.rst | 1 + .../management/editable_type/EditableList.jsx | 29 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 0b4b8d644a0..abed6491fcc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,6 +20,7 @@ Bugfixes - Fix always-disabled submit button when submitting an agreement response on someone's behalf (:pr:`5574`) - Disallow nonsensical retention periods and visibility durations (:pr:`5576`) +- Fix sorting by program code in editable list (:pr:`5582`) Internal Changes ^^^^^^^^^^^^^^^^ diff --git a/indico/modules/events/editing/client/js/management/editable_type/EditableList.jsx b/indico/modules/events/editing/client/js/management/editable_type/EditableList.jsx index c4004ffbad0..5f95fa39422 100644 --- a/indico/modules/events/editing/client/js/management/editable_type/EditableList.jsx +++ b/indico/modules/events/editing/client/js/management/editable_type/EditableList.jsx @@ -33,6 +33,7 @@ import {useNumericParam} from 'indico/react/util/routing'; import {indicoAxios, handleAxiosError} from 'indico/utils/axios'; import {camelizeKeys} from 'indico/utils/case'; import Palette from 'indico/utils/palette'; +import {natSortCompare} from 'indico/utils/sort'; import StateIndicator from '../../editing/timeline/StateIndicator'; import {userPropTypes} from '../../editing/timeline/util'; @@ -201,25 +202,33 @@ function EditableListDisplay({ ['editor', Translate.string('Editor'), 400], ]; - const sortEditor = contribution => + const programCodeKey = contribution => contribution.code; + const titleKey = contribution => contribution.title.toLowerCase(); + const revisionKey = contribution => contribution.editable && contribution.editable.revisionCount; + const statusKey = contribution => + contribution.editable && contribution.editable.state.toLowerCase(); + const editorKey = contribution => contribution.editable && contribution.editable.editor && contribution.editable.editor.fullName.toLowerCase(); - const sortStatus = contribution => - contribution.editable && contribution.editable.state.toLowerCase(); - const sortTitle = contribution => contribution.title.toLowerCase(); + + const sortKeys = { + code: programCodeKey, + title: titleKey, + revision: revisionKey, + status: statusKey, + editor: editorKey, + }; const sortFuncs = { - title: sortTitle, - revision: contribution => contribution.editable && contribution.editable.revisionCount, - status: sortStatus, - editor: sortEditor, + code: (arr, key) => [...arr].sort((a, b) => natSortCompare(key(a), key(b))), }; // eslint-disable-next-line no-shadow const _sortList = (sortBy, sortDirection, filteredResults) => { - const fn = sortFuncs[sortBy] || (x => x); - const newList = _.sortBy(contribList, fn); + const sortKey = sortKeys[sortBy] || (x => x[sortBy]); + const sortFn = sortFuncs[sortBy] || ((arr, key) => _.sortBy(arr, key)); + const newList = sortFn(contribList, sortKey); if (sortDirection === SortDirection.DESC) { newList.reverse(); }