1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-29 14:40:04 +02:00
copilot/frontend/src/state/selectors.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

const find = require('lodash.find');
const forceArray = require('force-array');
2016-12-20 13:29:54 +02:00
const get = require('lodash.get');
const reselect = require('reselect');
const {
createSelector
} = reselect;
2016-12-20 13:29:54 +02:00
const account = (state) => get(state, 'account.data', {});
const accountUi = (state) => get(state, 'account.ui', {});
2016-12-20 13:29:54 +02:00
const orgUiSections = (state) => get(state, 'orgs.ui.sections', []);
2016-12-20 21:06:02 +02:00
const projectUiSections = (state) => get(state, 'projects.ui.sections', []);
2016-12-20 13:29:54 +02:00
const orgs = (state) => get(state, 'orgs.data', []);
const projects = (state) => get(state, 'projects.data', []);
2016-12-20 21:06:02 +02:00
const projectById= (id) => createSelector(
projects,
(projects) => find(projects, ['id', id])
);
const orgById = (id) => createSelector(
orgs,
(orgs) => find(orgs, ['id', id])
);
const projectsByOrgId = (orgId) => createSelector(
[projects, orgById(orgId)],
(projects, org) => projects.filter((p) => p.org === org.uuid)
);
const orgSections = (orgId) => createSelector(
[orgById(orgId), orgUiSections],
(org, sections) => sections.filter(
(section) => forceArray(org.hide).indexOf(section) < 0
)
);
module.exports = {
accountSelector: account,
accountUISelector: accountUi,
orgByIdSelector: orgById,
orgsSelector: orgs,
orgSectionsSelector: orgSections,
2016-12-20 21:06:02 +02:00
projectSectionsSelector: projectUiSections,
projectsByOrgIdSelector: projectsByOrgId,
projectByIdSelector: projectById
};