1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 16:36:47 +02:00

fix: update usage of sort map

This commit is contained in:
David Wheatley
2021-10-27 20:08:55 +02:00
parent 2c801711bb
commit 6a57183525

View File

@@ -217,34 +217,33 @@ export default class IndexPage extends Page {
*/ */
viewItems() { viewItems() {
const items = new ItemList(); const items = new ItemList();
const sortMap = app.discussions.sortMap();
const sortOptions = {}; const sortOptions = Object.values(app.discussions.sortMap().toObject());
for (const i in sortMap) {
sortOptions[i] = app.translator.trans('core.forum.index_sort.' + i + '_button'); // Chooses the sort option with highest priority for now
} const defaultSortMethod = sortOptions.reduce((acc, option) => (acc.priority > option.priority ? acc : option), { priority: -9e10 });
// Find the selected search method, otherwise heed the default
const activeSearchMethod = sortOptions.find((opt) => opt.itemName === app.search.params().sort) || defaultSortMethod;
items.add( items.add(
'sort', 'sort',
Dropdown.component( Dropdown.component(
{ {
buttonClassName: 'Button', buttonClassName: 'Button',
label: sortOptions[app.search.params().sort] || Object.keys(sortMap).map((key) => sortOptions[key])[0], label: app.translator.trans(`core.forum.index_sort.${activeSearchMethod.itemName}_button`),
accessibleToggleLabel: app.translator.trans('core.forum.index_sort.toggle_dropdown_accessible_label'), accessibleToggleLabel: app.translator.trans('core.forum.index_sort.toggle_dropdown_accessible_label'),
}, },
Object.keys(sortOptions).map((value) => { sortOptions.map(({ itemName: sortingId, content: sortType }) =>
const label = sortOptions[value]; Button.component(
const active = (app.search.params().sort || Object.keys(sortMap)[0]) === value;
return Button.component(
{ {
icon: active ? 'fas fa-check' : true, icon: active ? 'fas fa-check' : true,
onclick: app.search.changeSort.bind(app.search, value), onclick: app.search.changeSort.bind(app.search, sortType),
active: active, active: active,
}, },
label app.translator.trans(`core.forum.index_sort.${sortingId}_button`)
); )
}) )
) )
); );