diff --git a/src/actions/index.js b/src/actions/index.js index ce01025..ce86367 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,15 +1,15 @@ // TODO: relegate these URLs entirely to environment variables -const EVENT_DATA_URL = `${process.env.SERVER_ROOT}${process.env.EVENT_EXT}`; -const CATEGORY_URL = `${process.env.SERVER_ROOT}${process.env.CATEGORY_EXT}`; -const TAG_TREE_URL = `${process.env.SERVER_ROOT}${process.env.TAG_TREE_EXT}`; -const SITES_URL = `${process.env.SERVER_ROOT}${process.env.SITES_EXT}`; +const EVENT_DATA_URL = `${process.env.SERVER_ROOT}${process.env.EVENT_EXT}` +const CATEGORY_URL = `${process.env.SERVER_ROOT}${process.env.CATEGORY_EXT}` +const TAG_TREE_URL = `${process.env.SERVER_ROOT}${process.env.TAG_TREE_EXT}` +const SITES_URL = `${process.env.SERVER_ROOT}${process.env.SITES_EXT}` const eventUrlMap = (event) => `${process.env.SERVER_ROOT}${process.env.EVENT_DESC_ROOT}/${(event.id) ? event.id : event}` /* * Create an error notification object * Types: ['error', 'warning', 'good', 'neural'] */ -function makeError(type, id, message) { +function makeError (type, id, message) { return { type: 'error', id, @@ -17,62 +17,62 @@ function makeError(type, id, message) { } } -export function fetchDomain() { - let events = []; - let categories = []; - let sites = []; - let notifications = []; - let tags = {}; +export function fetchDomain () { + let notifications = [] - function makeError(domainType) { + function handleError (domainType) { + return () => { notifications.push({ message: `Something went wrong fetching ${domainType}. Check the URL or try disabling them in the config file.`, type: 'error' - }); + }) + return [] + } + } + + return dispatch => { + dispatch(toggleFetchingDomain()) + const promises = [] + + const eventPromise = fetch(EVENT_DATA_URL) + .then(response => response.json()) + .catch(handleError('events')) + + const catPromise = fetch(CATEGORY_URL) + .then(response => response.json()) + .catch(handleError('categories')) + + let sitesPromise = Promise.resolve([]) + if (process.env.features.USE_SITES) { + sitesPromise = fetch(SITES_URL) + .then(response => response.json()) + .catch(handleError('sites')) } - return dispatch => { + let tagsPromise + if (process.env.features.USE_TAGS) { + tagsPromise = fetch(TAG_TREE_URL) + .then(response => response.json()) + .catch(handleError('tags')) + } + + return Promise.all([ eventPromise, catPromise, sitesPromise, tagsPromise]) + .then(response => { dispatch(toggleFetchingDomain()); - const promises = []; - - const eventPromise = fetch(EVENT_DATA_URL) - .then(response => response.json()) - .then(jsonEv => { events = jsonEv; }) - .catch(err => { makeError('events')}); - promises.push(eventPromise); - - const catPromise = fetch(CATEGORY_URL) - .then(response => response.json()) - .then(jsonCat => { categories = jsonCat; }) - .catch(err => { makeError('categories')}); - promises.push(catPromise); - - if (process.env.features.USE_SITES) { - const sitesPromise = fetch(SITES_URL) - .then(response => response.json()) - .then(jsonSites => { sites = jsonSites; }) - .catch(err => { makeError('sites')}); - promises.push(sitesPromise); + const result = { + events: response[0], + categories: response[1], + sites: response[2], + tags: response[3], + notifications } - - if (process.env.features.USE_TAGS) { - const tagTreePromise = fetch(TAG_TREE_URL) - .then(response => response.json()) - .then(jsonTagTree => { tags = jsonTagTree; }) - .catch(err => { makeError('tags')}); - promises.push(tagTreePromise); - } - - return Promise.all(promises) - .then(reponse => { - dispatch(toggleFetchingDomain()); - return { events, categories, sites, tags, notifications }; - }) - .catch(err => { - dispatch(fetchError(err.message)) - dispatch(toggleFetchingDomain()); - }) - }; + return result + }) + .catch(err => { + dispatch(fetchError(err.message)) + dispatch(toggleFetchingDomain()); + }) + }; } export const FETCH_ERROR = 'FETCH_ERROR'; diff --git a/src/components/Dashboard.jsx b/src/components/Dashboard.jsx index 2a496a4..b764983 100644 --- a/src/components/Dashboard.jsx +++ b/src/components/Dashboard.jsx @@ -28,7 +28,7 @@ class Dashboard extends React.Component { componentDidMount() { if (!this.props.app.isMobile) { this.props.actions.fetchDomain() - .then((domain) => this.props.actions.updateDomain(domain)); + .then((domain) => this.props.actions.updateDomain(domain)) } } diff --git a/src/reducers/utils/validators.js b/src/reducers/utils/validators.js index 6355e1b..a995d66 100644 --- a/src/reducers/utils/validators.js +++ b/src/reducers/utils/validators.js @@ -26,23 +26,26 @@ const isDuplicate = (node, set) => { return (set.has(node.key)); }; /* * Traverse a tag tree and check its duplicates */ -function validateTree(node, parent, set, duplicates) { +function validateTree (node, parent, set, duplicates) { + if (!Array.isArray(node) || !node.length) { + return + } // If it's a leaf, check that it's not duplicate if (isLeaf(node)) { if (isDuplicate(node, set)) { duplicates.push({ id: node.key, error: makeError('Tags', node.key, 'tag was found more than once in hierarchy. Ignoring duplicate.') - }); - delete parent.children[node.key]; + }) + delete parent.children[node.key] } else { - set.add(node.key); + set.add(node.key) } } else { // If it's not a leaf, simply keep going Object.values(node.children).forEach((childNode) => { - validateTree(childNode, node, set, duplicates); - }); + validateTree(childNode, node, set, duplicates) + }) } }