2016-12-16 14:28:27 +02:00
|
|
|
const find = require('lodash.find');
|
|
|
|
const forceArray = require('force-array');
|
2016-12-20 13:29:54 +02:00
|
|
|
const get = require('lodash.get');
|
2016-12-16 14:28:27 +02:00
|
|
|
const reselect = require('reselect');
|
|
|
|
|
|
|
|
const {
|
|
|
|
createSelector
|
|
|
|
} = reselect;
|
|
|
|
|
2016-12-20 13:29:54 +02:00
|
|
|
const account = (state) => get(state, 'account.data', {});
|
2016-12-21 16:05:16 +02:00
|
|
|
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', []);
|
2017-01-03 00:32:29 +02:00
|
|
|
const serviceUiSections = (state) => get(state, 'services.ui.sections', []);
|
2016-12-20 13:29:54 +02:00
|
|
|
const orgs = (state) => get(state, 'orgs.data', []);
|
|
|
|
const projects = (state) => get(state, 'projects.data', []);
|
2017-01-03 00:32:29 +02:00
|
|
|
const services = (state) => get(state, 'services.data', []);
|
2016-12-16 14:28:27 +02:00
|
|
|
|
2017-01-03 00:32:29 +02:00
|
|
|
const projectById = (projectId) => createSelector(
|
2016-12-20 21:06:02 +02:00
|
|
|
projects,
|
2017-01-03 00:32:29 +02:00
|
|
|
(projects) => find(projects, ['id', projectId])
|
2016-12-20 21:06:02 +02:00
|
|
|
);
|
|
|
|
|
2017-01-03 00:32:29 +02:00
|
|
|
const orgById = (orgId) => createSelector(
|
2016-12-16 14:28:27 +02:00
|
|
|
orgs,
|
2017-01-03 00:32:29 +02:00
|
|
|
(orgs) => find(orgs, ['id', orgId])
|
|
|
|
);
|
|
|
|
|
|
|
|
const serviceById = (serviceId) => createSelector(
|
|
|
|
[services],
|
|
|
|
(services) => find(services, ['id', serviceId])
|
2016-12-16 14:28:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-01-03 00:32:29 +02:00
|
|
|
const servicesByProjectId = (projectId) => createSelector(
|
|
|
|
[services, projectById(projectId)],
|
|
|
|
(services, project) =>
|
|
|
|
services.filter((s) => s.project === project.uuid)
|
|
|
|
.map((service) => ({
|
|
|
|
...service,
|
|
|
|
services: services.filter((s) => s.parent === service.uuid)
|
|
|
|
}))
|
|
|
|
.filter((s) => !s.parent)
|
|
|
|
);
|
|
|
|
|
2016-12-16 14:28:27 +02:00
|
|
|
module.exports = {
|
|
|
|
accountSelector: account,
|
2016-12-21 16:05:16 +02:00
|
|
|
accountUISelector: accountUi,
|
2016-12-16 14:28:27 +02:00
|
|
|
orgByIdSelector: orgById,
|
|
|
|
orgsSelector: orgs,
|
2017-01-03 00:32:29 +02:00
|
|
|
servicesSelector: services,
|
|
|
|
serviceByIdSelector: serviceById,
|
2016-12-16 14:28:27 +02:00
|
|
|
orgSectionsSelector: orgSections,
|
2016-12-20 21:06:02 +02:00
|
|
|
projectSectionsSelector: projectUiSections,
|
2017-01-03 00:32:29 +02:00
|
|
|
serviceSectionsSelector: serviceUiSections,
|
2016-12-20 21:06:02 +02:00
|
|
|
projectsByOrgIdSelector: projectsByOrgId,
|
2017-01-03 00:32:29 +02:00
|
|
|
projectByIdSelector: projectById,
|
|
|
|
servicesByProjectIdSelector: servicesByProjectId
|
2016-12-16 14:28:27 +02:00
|
|
|
};
|