Skip to content
Snippets Groups Projects
Commit e2a6260b authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

feat(sort-by-importance): add sorting by importance

parent 417c2ab4
No related branches found
No related tags found
1 merge request!13Sprint #14
......@@ -31,6 +31,7 @@ const DashboardFilters = ({
inline
onChange={changeFilterValue('order')}
options={getFilterOptions('order')}
value={getDefaultFilterValue('order')}
/>
</FilterGroup>
</FiltersContainer>
......
......@@ -13,7 +13,7 @@ import { Dashboard } from './'
import { getHandlingEditors } from '../../redux/editors'
import { getUserPermissions } from '../../../../component-faraday-selectors/src'
import { priorityFilter, withFiltersHOC } from '../Filters'
import { priorityFilter, importanceSort, withFiltersHOC } from '../Filters'
export default compose(
ConnectPage(() => [actions.getCollections(), actions.getUsers()]),
......@@ -64,13 +64,7 @@ export default compose(
withJournal,
withFiltersHOC({
priority: priorityFilter,
order: {
options: [
{ label: 'Newest first', value: 'newest' },
{ label: 'Oldest first', value: 'oldest' },
],
filterFn: () => () => true,
},
order: importanceSort,
}),
withContext(
{
......
import { get } from 'lodash'
import { utils } from './'
import cfg from '../../../../xpub-faraday/config/default'
const statuses = get(cfg, 'statuses')
export const SORT_VALUES = {
MORE_IMPORTANT: 'more_important',
LESS_IMPORTANT: 'less_important',
}
const options = [
{ label: 'Imporant first', value: SORT_VALUES.MORE_IMPORTANT },
{ label: 'Less important first', value: SORT_VALUES.LESS_IMPORTANT },
]
const sortFn = sortValue => (item1, item2) => {
const item1Importance = utils.getCollectionImportance(statuses, item1)
const item2Importance = utils.getCollectionImportance(statuses, item2)
if (sortValue === SORT_VALUES.MORE_IMPORTANT) {
return item2Importance - item1Importance
}
return item1Importance - item2Importance
}
export default {
sortFn,
options,
type: 'sort',
}
......@@ -3,3 +3,4 @@ import * as utils from './utils'
export { utils }
export { default as withFiltersHOC } from './withFilters'
export { default as priorityFilter } from './priorityFilter'
export { default as importanceSort } from './importanceSort'
......@@ -5,10 +5,16 @@ import cfg from '../../../../xpub-faraday/config/default'
const statuses = get(cfg, 'statuses')
export const FILTER_VALUES = {
ALL: 'all',
NEEDS_ATTENTION: 'needsAttention',
IN_PROGRESS: 'inProgress',
}
const options = [
{ label: 'All', value: 'all' },
{ label: 'Needs Attention', value: 'needsAttention' },
{ label: 'In Progress', value: 'inProgress' },
{ label: 'All', value: FILTER_VALUES.ALL },
{ label: 'Needs Attention', value: FILTER_VALUES.NEEDS_ATTENTION },
{ label: 'In Progress', value: FILTER_VALUES.IN_PROGRESS },
]
const filterFn = (filterValue, { currentUser, userPermissions = [] }) => ({
......@@ -21,9 +27,9 @@ const filterFn = (filterValue, { currentUser, userPermissions = [] }) => ({
)
const userRole = utils.getUserRole(currentUser, get(permission, 'role'))
switch (filterValue) {
case 'needsAttention':
case FILTER_VALUES.NEEDS_ATTENTION:
return get(statuses, `${status}.${userRole}.needsAttention`)
case 'inProgress':
case FILTER_VALUES.IN_PROGRESS:
return !get(statuses, `${status}.${userRole}.needsAttention`)
default:
return true
......@@ -33,4 +39,5 @@ const filterFn = (filterValue, { currentUser, userPermissions = [] }) => ({
export default {
options,
filterFn,
type: 'filter',
}
import fixturesService from 'pubsweet-component-fixture-service'
import { FILTER_VALUES } from './priorityFilter'
import { priorityFilter, utils } from './'
const {
......@@ -8,12 +9,6 @@ const {
const { filterFn } = priorityFilter
const FILTER_VALUES = {
ALL: 'all',
NEEDS_ATTENTION: 'needsAttention',
IN_PROGRESS: 'inProgress',
}
describe('Priority filter function for reviewersInvited status', () => {
describe('ALL', () => {
it('should return true if ALL is selected', () => {
......
import { get } from 'lodash'
export const hydrateFilters = defaultValues => {
const filterValues = localStorage.getItem('filterValues')
if (filterValues) return JSON.parse(filterValues)
......@@ -5,10 +7,22 @@ export const hydrateFilters = defaultValues => {
}
export const makeFilterFunctions = config =>
Object.entries(config).map(([filterKey, { filterFn }]) => ({
key: filterKey,
fn: filterFn,
}))
Object.entries(config)
.filter(([filterKey, { type }]) => type === 'filter')
.map(([filterKey, { filterFn }]) => ({
key: filterKey,
fn: filterFn,
}))
export const makeSortFunction = config => {
const [sortKey, { sortFn }] = Object.entries(config).find(
([filterKey, { type }]) => type !== 'filter',
)
return {
sortKey,
sortFn,
}
}
export const makeFilterValues = config =>
Object.keys(config).reduce((acc, el) => ({ ...acc, [el]: '' }), {})
......@@ -23,3 +37,6 @@ export const parsePermission = permission => ({
objectId: permission.object.id,
role: permission.group,
})
export const getCollectionImportance = (statuses, item) =>
get(statuses, `${get(item, 'status') || 'draft'}.importance`)
......@@ -6,6 +6,7 @@ import { utils } from './'
export default config => Component => {
const filterFns = utils.makeFilterFunctions(config)
const filterValues = utils.makeFilterValues(config)
const { sortKey, sortFn } = utils.makeSortFunction(config)
return compose(
withState(
......@@ -37,10 +38,12 @@ export default config => Component => {
)
},
filterItems: ({ filterValues, ...props }) => items =>
filterFns.reduce(
(acc, { key, fn }) => acc.filter(fn(filterValues[key], props)),
items,
),
filterFns
.reduce(
(acc, { key, fn }) => acc.filter(fn(filterValues[key], props)),
items,
)
.sort(sortFn(filterValues[sortKey], props)),
}),
)(Component)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment