insomnia/app/ui/redux/selectors.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-11-25 23:09:17 +00:00
import {createSelector} from 'reselect';
import * as models from '../../models/index';
2016-11-26 08:29:16 +00:00
2016-11-25 23:09:17 +00:00
// ~~~~~~~~~ //
// Selectors //
// ~~~~~~~~~ //
export const selectEntitiesLists = createSelector(
state => state.entities,
entities => {
const entitiesLists = {};
for (const k of Object.keys(entities)) {
const entityMap = entities[k];
entitiesLists[k] = Object.keys(entityMap).map(id => entityMap[id]);
}
return entitiesLists;
}
);
export const selectActiveWorkspace = createSelector(
state => selectEntitiesLists(state).workspaces,
state => state.entities,
state => state.global.activeWorkspaceId,
(workspaces, entities, activeWorkspaceId) => {
return entities.workspaces[activeWorkspaceId] || workspaces[0];
}
);
export const selectRequestsAndRequestGroups = createSelector(
selectEntitiesLists,
entities => [...entities.requests, ...entities.requestGroups]
);
export const selectSidebarChildren = createSelector(
state => state.requestGroupMeta.collapsed,
selectRequestsAndRequestGroups,
selectActiveWorkspace,
(collapsed, docs, activeWorkspace) => {
function next (parentId) {
const children = docs
.filter(e => e.parentId === parentId)
.sort((a, b) => {
// Always sort folders above
if (a.type === models.requestGroup.type && b.type !== models.requestGroup.type) {
return -1;
}
if (a.metaSortKey === b.metaSortKey) {
return a._id > b._id ? -1 : 1;
} else {
return a.metaSortKey < b.metaSortKey ? -1 : 1;
}
});
if (children.length > 0) {
return children.map(c => ({
doc: c,
children: next(c._id,),
collapsed: !!collapsed[c._id],
}));
} else {
return children;
}
}
return next(activeWorkspace._id, false);
}
);
2016-11-26 07:33:55 +00:00
export const selectWorkspaceRequestsAndRequestGroups = createSelector(
selectActiveWorkspace,
selectEntitiesLists,
(activeWorkspace, entities) => {
function getChildren (doc) {
const requests = entities.requests.filter(r => r.parentId === doc._id);
const requestGroups = entities.requestGroups.filter(rg => rg.parentId === doc._id);
const requestGroupChildren = [];
for (const requestGroup of requestGroups) {
for (const requestGroupChild of getChildren(requestGroup)) {
requestGroupChildren.push(requestGroupChild);
}
}
return [...requests, ...requestGroups, ...requestGroupChildren];
}
return getChildren(activeWorkspace);
}
);