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

89 lines
2.8 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', []);
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', []);
2017-01-10 16:58:51 +02:00
const collapsedServices = (state) => get(state, 'services.ui.collapsed', []);
2017-01-09 21:08:47 +02:00
const instances = (state) => get(state, 'instances.data', []);
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(
orgs,
2017-01-03 00:32:29 +02:00
(orgs) => find(orgs, ['id', orgId])
);
const serviceById = (serviceId) => createSelector(
[services],
(services) => find(services, ['id', serviceId])
);
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(
2017-01-10 16:58:51 +02:00
[services, projectById(projectId), collapsedServices],
(services, project, collapsed) =>
2017-01-03 00:32:29 +02:00
services.filter((s) => s.project === project.uuid)
.map((service) => ({
...service,
services: services.filter((s) => s.parent === service.uuid)
}))
.filter((s) => !s.parent)
2017-01-10 16:58:51 +02:00
.map((service) => ({
...service,
collapsed: collapsed.indexOf(service.uuid) >= 0,
services: service.services.map((service) => ({
...service,
collapsed: collapsed.indexOf(service.uuid) >= 0
}))
}))
2017-01-03 00:32:29 +02:00
);
2017-01-09 21:08:47 +02:00
const instancesByServiceId = (serviceId) => createSelector(
[instances, serviceById(serviceId)],
2017-01-10 16:58:51 +02:00
(instances, service) =>
instances.filter((i) => i.service === service.uuid)
2017-01-09 21:08:47 +02:00
);
module.exports = {
accountSelector: account,
accountUISelector: accountUi,
orgByIdSelector: orgById,
orgsSelector: orgs,
2017-01-03 00:32:29 +02:00
servicesSelector: services,
serviceByIdSelector: serviceById,
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,
2017-01-09 21:08:47 +02:00
servicesByProjectIdSelector: servicesByProjectId,
instancesByServiceIdSelector: instancesByServiceId
};