Skip to content

Commit 204bd05

Browse files
committed
Sorting tables remember the selected sorting column in the local storage.
1 parent 5280cc2 commit 204bd05

File tree

9 files changed

+50
-14
lines changed

9 files changed

+50
-14
lines changed

src/components/Groups/ResultsTable/ResultsTable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class ResultsTable extends Component {
423423
total: userStats && userStats.points,
424424
passed: userStats,
425425
buttons: renderActions && hasPermissions(group, 'update') ? renderActions(user.id) : '',
426-
// actually 'update' is not the right premission, but its sufficiently close :)
426+
// actually 'update' is not the right permission, but its sufficiently close :)
427427
};
428428

429429
assignments.forEach(assignment => {
@@ -470,6 +470,7 @@ class ResultsTable extends Component {
470470
return (
471471
<>
472472
<SortableTable
473+
id="ResultsTable"
473474
hover
474475
columns={this.prepareColumnDescriptors(
475476
assignments,

src/components/SystemMessages/MessagesList/MessagesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class MessagesList extends Component {
105105
/>
106106
<hr className="m-0" />
107107
<SortableTable
108+
id="MessageList"
108109
hover
109110
columns={this.prepareColumnDescriptors(locale)}
110111
defaultOrder="visibleTo"

src/components/widgets/SortableTable/SortableTable.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,37 @@ import { lruMemoize } from 'reselect';
77
import { UserUIDataContext } from '../../../helpers/contexts.js';
88
import { CloseIcon, SortedIcon } from '../../icons';
99
import withRouter, { withRouterProps } from '../../../helpers/withRouter.js';
10+
import { storageGetItem, storageSetItem } from '../../../helpers/localStorage.js';
11+
12+
const localStorageKeyPrefix = 'SortableTable';
1013

1114
class SortableTable extends Component {
1215
constructor(props) {
1316
super(props);
1417
this.state = { sortColumn: props.defaultOrder || null, ascendant: true };
1518
}
1619

17-
// Default row rendering fucntion (if the user does not provide custom function)
20+
setSortColumnFromLocalStorage = () => {
21+
const key = `${localStorageKeyPrefix}[${this.props.id}].order`;
22+
const order = storageGetItem(key, null);
23+
if (order) {
24+
const ascendant = order[0] === '+';
25+
const sortColumn = order.substring(1);
26+
this.setState({ sortColumn, ascendant });
27+
}
28+
};
29+
30+
componentDidMount() {
31+
this.setSortColumnFromLocalStorage();
32+
}
33+
34+
componentDidUpdate(prevProps) {
35+
if (prevProps.id !== this.props.id) {
36+
this.setSortColumnFromLocalStorage();
37+
}
38+
}
39+
40+
// Default row rendering function (if the user does not provide custom function)
1841
defaultRowRenderer = (row, idx, columns, openLinkGenerator = null) => {
1942
const { navigate } = this.props;
2043
const doubleClickLink = openLinkGenerator && openLinkGenerator(row, idx);
@@ -44,17 +67,24 @@ class SortableTable extends Component {
4467

4568
// Change internal state that holds sorting parameters.
4669
orderBy = colId => {
47-
const { columns } = this.props;
48-
const { sortColumn, ascendant } = this.state;
70+
const { id, columns } = this.props;
71+
let { sortColumn, ascendant } = this.state;
4972
const column = columns && columns.find(({ id }) => id === colId);
73+
const key = `${localStorageKeyPrefix}[${id}].order`;
5074

5175
if (!colId || !column) {
52-
this.setState({ sortColumn: null, ascendant: true });
76+
sortColumn = null;
77+
ascendant = true;
5378
} else if (colId === sortColumn) {
54-
this.setState({ ascendant: !ascendant });
79+
ascendant = !ascendant;
5580
} else {
56-
this.setState({ sortColumn: colId, ascendant: true });
81+
sortColumn = colId;
82+
ascendant = true;
5783
}
84+
85+
const order = sortColumn ? `${ascendant ? '+' : '-'}${sortColumn}` : null;
86+
storageSetItem(key, order);
87+
this.setState({ sortColumn, ascendant });
5888
};
5989

6090
// Helper function that actually sorts the data according to internal state
@@ -158,6 +188,7 @@ class SortableTable extends Component {
158188
}
159189

160190
SortableTable.propTypes = {
191+
id: PropTypes.string.isRequired,
161192
columns: PropTypes.array.isRequired,
162193
defaultOrder: PropTypes.string,
163194
data: PropTypes.array,

src/locales/cs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@
811811
"app.exercise.runtimes": "Běhová prostředí",
812812
"app.exercise.solutionFilesLimitExplanation": "Maximální počet odevzdaných souborů v řešení. Uživatelům není dovoleno odevzdávat řešení, která překračují tento limit. Limit se neaplikuje pokud není vyplněn.",
813813
"app.exercise.solutionSizeLimitExplanation": "Maximální celková velikost všech souborů v řešení. Uživatelům není dovoleno odevzdávat řešení, která překračují tento limit. Limit se neaplikuje pokud není vyplněn.",
814-
"app.exercise.submitReferenceSoution": "Nové referenční řešení",
814+
"app.exercise.submitReferenceSolution": "Nové referenční řešení",
815815
"app.exercise.title": "Podrobnosti úlohy",
816816
"app.exercise.validationErrors.config": "Konfigurace testů není kompletní, což se typicky stane, pokud přidáte test nebo smažete soubor",
817817
"app.exercise.validationErrors.limits": "Paměťové nebo časové limity nejsou správně nastaveny",

src/locales/en.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@
811811
"app.exercise.runtimes": "Runtime environments",
812812
"app.exercise.solutionFilesLimitExplanation": "Maximal number of files submitted in a solution. The users are not allowed to submit solutions that exceed this limit. If empty, no limit is applied.",
813813
"app.exercise.solutionSizeLimitExplanation": "Maximal total size of all files submitted in a solution. The users are not allowed to submit solutions that exceed this limit. If empty, no limit is applied.",
814-
"app.exercise.submitReferenceSoution": "New Reference Solution",
814+
"app.exercise.submitReferenceSolution": "New Reference Solution",
815815
"app.exercise.title": "Exercise Detail",
816816
"app.exercise.validationErrors.config": "Configuration of some tests is incomplete; this typically happens when you add a test or delete a file",
817817
"app.exercise.validationErrors.limits": "Memory or time limits are not properly set",
@@ -1554,7 +1554,7 @@
15541554
"app.referenceSolution.exerciseNoLongerHasEnvironment": "The exercise no longer supports the environment for which this solution was evaluated. Resubmission is not possible.",
15551555
"app.referenceSolution.title": "Reference Solution Detail",
15561556
"app.referenceSolution.visibility.private": "The solution is visible only to you.",
1557-
"app.referenceSolution.visibility.promoted": "The solution is promoted by the auhor of the exercise (recommended for reading to other supervisors)",
1557+
"app.referenceSolution.visibility.promoted": "The solution is promoted by the author of the exercise (recommended for reading to other supervisors)",
15581558
"app.referenceSolution.visibility.public": "The solution is visible to all supervisors who can access this exercise.",
15591559
"app.referenceSolutionDetail.comments.additionalSwitchNote": "(teachers who can see this reference solution)",
15601560
"app.referenceSolutionDetail.title.details": "Reference Solution Detail",
@@ -2200,4 +2200,4 @@
22002200
"recodex-judge-shuffle-all": "Unordered-tokens-and-rows judge",
22012201
"recodex-judge-shuffle-newline": "Unordered-tokens judge (ignoring ends of lines)",
22022202
"recodex-judge-shuffle-rows": "Unordered-rows judge"
2203-
}
2203+
}

src/pages/AssignmentSolutions/AssignmentSolutions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ class AssignmentSolutions extends Component {
668668
unlimitedHeight
669669
noPadding>
670670
<SortableTable
671+
id={`AssignmentSolutions.${this.state.viewMode}`}
671672
hover
672673
columns={prepareTableColumnDescriptors(
673674
loggedUserId,

src/pages/Exercise/Exercise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Exercise extends Component {
192192
/>
193193
) : (
194194
<FormattedMessage
195-
id="app.exercise.submitReferenceSoution"
195+
id="app.exercise.submitReferenceSolution"
196196
defaultMessage="New Reference Solution"
197197
/>
198198
)}

src/pages/ExerciseReferenceSolutions/ExerciseReferenceSolutions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const prepareTableColumnDescriptors = lruMemoize((loggedUserId, locale, links, d
7171
tooltip={
7272
<FormattedMessage
7373
id="app.referenceSolution.visibility.promoted"
74-
defaultMessage="The solution is promoted by the auhor of the exercise (recommended for reading to other supervisors)"
74+
defaultMessage="The solution is promoted by the author of the exercise (recommended for reading to other supervisors)"
7575
/>
7676
}
7777
/>
@@ -389,7 +389,7 @@ class ExerciseReferenceSolutions extends Component {
389389
/>
390390
) : (
391391
<FormattedMessage
392-
id="app.exercise.submitReferenceSoution"
392+
id="app.exercise.submitReferenceSolution"
393393
defaultMessage="New Reference Solution"
394394
/>
395395
)}
@@ -446,6 +446,7 @@ class ExerciseReferenceSolutions extends Component {
446446
unlimitedHeight>
447447
<>
448448
<SortableTable
449+
id="ReferenceSolutions"
449450
hover
450451
columns={prepareTableColumnDescriptors(userId, locale, links, deleteReferenceSolution)}
451452
defaultOrder="createdAt"

src/pages/GroupUserSolutions/GroupUserSolutions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ class GroupUserSolutions extends Component {
541541
unlimitedHeight
542542
noPadding>
543543
<SortableTable
544+
id="GroupUserSolutions"
544545
hover
545546
columns={prepareTableColumnDescriptors(assignments, groupId, locale, links)}
546547
defaultOrder="date"

0 commit comments

Comments
 (0)