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-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
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2017-04-26 00:18:40 -07:00
|
|
|
const urnsToNodeUrn = (state, { data: nodes = [] }) => {
|
2017-04-25 21:12:19 -07:00
|
|
|
const { query: { urn }, nodesByUrn } = state;
|
|
|
|
return Object.assign({}, nodesByUrn, {
|
|
|
|
[urn]: 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
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} entityName
|
|
|
|
*/
|
|
|
|
const createPageMapping = entityName => (state, payload = {}) => {
|
|
|
|
return entitiesToPage(entityName)(state, payload);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes the response from the list api request and invokes a function to
|
|
|
|
* map a urn to child urns or nodes
|
|
|
|
* @param {Object} state
|
2017-04-26 00:18:40 -07:00
|
|
|
* @param {Object} payload the response from the list endpoint/api
|
2017-04-25 21:12:19 -07:00
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
const receiveNodes = (state, payload = {}) => urnsToNodeUrn(state, payload);
|
|
|
|
|
|
|
|
export { receiveNodes, initializeState, receiveEntities, createUrnMapping, createPageMapping };
|