2017-04-20 09:27:09 -07:00
|
|
|
import _ from 'lodash';
|
2017-04-26 17:05:28 -07:00
|
|
|
import { mapEntitiesToIds } from 'wherehows-web/reducers/utils';
|
2017-04-20 09:27:09 -07:00
|
|
|
|
|
|
|
const { merge, union } = _;
|
|
|
|
|
2017-04-25 21:12:19 -07:00
|
|
|
// Initial state for entities (metrics, flows, datasets)
|
|
|
|
const _initialState = {
|
2017-04-20 09:27:09 -07:00
|
|
|
count: null,
|
|
|
|
page: null,
|
|
|
|
itemsPerPage: null,
|
|
|
|
totalPages: null,
|
2017-04-25 21:12:19 -07:00
|
|
|
query: {
|
|
|
|
urn: '',
|
|
|
|
page: ''
|
|
|
|
},
|
2017-04-20 09:27:09 -07:00
|
|
|
byId: {},
|
2017-04-25 21:12:19 -07:00
|
|
|
byPage: {},
|
|
|
|
byUrn: {}
|
2017-04-20 09:27:09 -07:00
|
|
|
};
|
|
|
|
|
2017-04-25 21:12:19 -07:00
|
|
|
/**
|
|
|
|
* Ensure we deep clone since this shape is shared amongst entities
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
const initializeState = () => JSON.parse(JSON.stringify(_initialState));
|
|
|
|
|
2017-04-20 09:27:09 -07:00
|
|
|
/**
|
|
|
|
* Merges previous entities and a new map of ids to entities into a new map
|
2017-04-25 21:12:19 -07:00
|
|
|
* @param {String} entityName
|
2017-04-20 09:27:09 -07:00
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
const appendEntityIdMap = (
|
|
|
|
entityName /**
|
2017-04-20 09:27:09 -07:00
|
|
|
*
|
|
|
|
* @param {Object} prevEntities current list of ids mapped to entities
|
|
|
|
* @param {Object} props
|
|
|
|
* @props {Array} props[entityName] list of received entities
|
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
) => (prevEntities, props) => merge({}, prevEntities, mapEntitiesToIds(props[entityName]));
|
2017-04-25 21:12:19 -07:00
|
|
|
|
|
|
|
/**
|
2017-04-26 17:05:28 -07:00
|
|
|
* Appends a list of child entity ids for a given urn. urn is null for top level entities
|
2017-04-25 21:12:19 -07:00
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
|
|
|
const appendUrnIdMap = (
|
|
|
|
entityName
|
|
|
|
/**
|
2017-04-26 17:05:28 -07:00
|
|
|
* @param {Object} urnEntities current mapping of Urns to ids
|
|
|
|
* @param {Object} props payload with new objects containing id, and urn prop
|
2017-04-25 21:12:19 -07:00
|
|
|
*/
|
2017-04-26 17:05:28 -07:00
|
|
|
) => (urnEntities, { parentUrn = null, [entityName]: entities = [] }) =>
|
|
|
|
Object.assign({}, urnEntities, {
|
|
|
|
[parentUrn]: union(urnEntities[parentUrn], entities.mapBy('id'))
|
|
|
|
});
|
2017-04-25 21:12:19 -07:00
|
|
|
|
2017-05-15 12:45:28 -07:00
|
|
|
/**
|
|
|
|
* Appends a list of child entities ids for a given name. name is null for top level entities
|
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
|
|
|
const appendNamedIdMap = entityName => (nameEntities, { parentName = null, [entityName]: entities = [] }) =>
|
|
|
|
Object.assign({}, nameEntities, {
|
|
|
|
[parentName]: union(nameEntities[parentName], entities.mapBy('id'))
|
|
|
|
});
|
|
|
|
|
2017-04-20 09:27:09 -07:00
|
|
|
/**
|
|
|
|
* Returns a curried function that receives entityName to lookup on the props object
|
|
|
|
* @param {String} entityName
|
|
|
|
* @return {Function}
|
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
const entitiesToPage = (
|
2017-04-25 21:12:19 -07:00
|
|
|
entityName
|
|
|
|
/**
|
|
|
|
* a new map of page numbers to datasetIds
|
2017-04-20 09:27:09 -07:00
|
|
|
* @param {Object} pagedEntities
|
|
|
|
* @param {Object} props
|
|
|
|
* @props {Array} props[entityName] list of received entities
|
|
|
|
* @return {Object} props.page page the page that contains the returned list of entities
|
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
) => (pagedEntities = {}, props) => {
|
|
|
|
const entities = props[entityName];
|
|
|
|
const { page } = props;
|
2017-04-25 21:12:19 -07:00
|
|
|
|
2017-04-21 15:43:48 -07:00
|
|
|
return Object.assign({}, pagedEntities, {
|
2017-04-25 21:12:19 -07:00
|
|
|
[page]: union(pagedEntities[page], entities.mapBy('id'))
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps a urn to a list of child nodes from the list api
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Array} nodes
|
2017-05-15 12:45:28 -07:00
|
|
|
* @param {String} parentUrn
|
|
|
|
* @return {*}
|
2017-04-25 21:12:19 -07:00
|
|
|
*/
|
2017-05-15 12:45:28 -07:00
|
|
|
const urnsToNodeUrn = (state, { nodes = [], parentUrn = null } = {}) => {
|
|
|
|
const { nodesByUrn } = state;
|
2017-04-25 21:12:19 -07:00
|
|
|
return Object.assign({}, nodesByUrn, {
|
2017-05-15 12:45:28 -07:00
|
|
|
[parentUrn]: union(nodes)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps a name to a list of child nodes from the list api
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Array} nodes
|
|
|
|
* @param {String} parentName
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
const namesToNodeName = (state, { nodes = [], parentName = null } = {}) => {
|
|
|
|
const { nodesByName } = state;
|
|
|
|
return Object.assign({}, nodesByName, {
|
|
|
|
[parentName]: union(nodes)
|
2017-04-21 15:43:48 -07:00
|
|
|
});
|
|
|
|
};
|
2017-04-20 09:27:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an identifier for an entity and returns a reducing function with the entityName as context
|
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
const receiveEntities = (
|
|
|
|
entityName /**
|
2017-04-25 21:12:19 -07:00
|
|
|
* entities (flows|metrics|datasets) for the ActionTypes.RECEIVE_PAGED_[ENTITY_NAME] action
|
2017-04-20 09:27:09 -07:00
|
|
|
* @param {Object} state previous state for datasets
|
|
|
|
* @param {Object} payload data received through ActionTypes.RECEIVE_PAGED_[ENTITY_NAME]
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2017-04-21 15:43:48 -07:00
|
|
|
) => (state, payload = {}) => {
|
2017-04-25 21:12:19 -07:00
|
|
|
return appendEntityIdMap(entityName)(state, payload);
|
|
|
|
};
|
2017-04-21 15:43:48 -07:00
|
|
|
|
2017-04-25 21:12:19 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
|
|
|
const createUrnMapping = entityName => (state, payload = {}) => {
|
|
|
|
return appendUrnIdMap(entityName)(state, payload);
|
2017-04-21 15:43:48 -07:00
|
|
|
};
|
2017-04-20 09:27:09 -07:00
|
|
|
|
2017-04-25 21:12:19 -07:00
|
|
|
/**
|
2017-05-15 12:45:28 -07:00
|
|
|
* Curries an entityName into a function to append named entity entities
|
2017-04-25 21:12:19 -07:00
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
2017-05-15 12:45:28 -07:00
|
|
|
const createNameMapping = entityName => (state, payload = {}) => {
|
|
|
|
return appendNamedIdMap(entityName)(state, payload);
|
2017-04-25 21:12:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-05-15 12:45:28 -07:00
|
|
|
* Appends a list of entities to a page
|
|
|
|
* @param {String} entityName
|
2017-04-25 21:12:19 -07:00
|
|
|
*/
|
2017-05-15 12:45:28 -07:00
|
|
|
const createPageMapping = entityName => (state, payload = {}) => {
|
|
|
|
return entitiesToPage(entityName)(state, payload);
|
|
|
|
};
|
2017-04-25 21:12:19 -07:00
|
|
|
|
2017-05-15 12:45:28 -07:00
|
|
|
export {
|
|
|
|
initializeState,
|
|
|
|
namesToNodeName,
|
|
|
|
urnsToNodeUrn,
|
|
|
|
receiveEntities,
|
|
|
|
createUrnMapping,
|
|
|
|
createPageMapping,
|
|
|
|
createNameMapping
|
|
|
|
};
|