Added group sorting to GroupCollection

• Added:
- group sorting to GroupCollection
- constants for group sorting
- GroupListSortOptionsPopover
This commit is contained in:
mgabdev
2020-07-24 21:41:05 -05:00
parent e9fbccaa12
commit 86ac15ce90
7 changed files with 152 additions and 55 deletions

View File

@@ -6,6 +6,10 @@ import api, { getLinks } from '../api';
import { me } from '../initial_state';
import { importFetchedAccounts } from './importer';
import { fetchRelationships } from './accounts';
import {
GROUP_LIST_SORTING_TYPE_ALPHABETICAL,
GROUP_LIST_SORTING_TYPE_MOST_POPULAR,
} from '../constants'
export const GROUP_FETCH_REQUEST = 'GROUP_FETCH_REQUEST';
export const GROUP_FETCH_SUCCESS = 'GROUP_FETCH_SUCCESS';
@@ -578,4 +582,38 @@ export function updateRoleFail(groupId, id, error) {
id,
error,
};
};
};
export const sortGroups = (tab, sortType) => (dispatch, getState) => {
if (!me) return
const groupIdsByTab = getState().getIn(['group_lists', tab, 'items'], ImmutableList()).toJS()
const allGroups = getState().get('groups', ImmutableMap()).toJS()
let groupsByTab = []
for (const key in allGroups) {
const block = allGroups[key]
if (groupIdsByTab.indexOf(block.id > -1)) {
groupsByTab.push(block)
}
}
if (sortType === GROUP_LIST_SORTING_TYPE_ALPHABETICAL) {
groupsByTab.sort((a, b) => a.title.localeCompare(b.title))
} else if (sortType === GROUP_LIST_SORTING_TYPE_MOST_POPULAR) {
groupsByTab.sort((a, b) => (a.member_count < b.member_count) ? 1 : -1)
}
const sortedGroupsIdsByTab = groupsByTab.map((group) => group.id)
dispatch(groupsSort(tab, sortedGroupsIdsByTab))
};
export function groupsSort(tab, groupIds) {
return {
type: GROUP_SORT,
tab,
groupIds,
}
}