From 7b8b81c788ce238db17219d3f3fa59eae1ee83d2 Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Thu, 13 Dec 2018 11:56:04 +0000 Subject: [PATCH] interpolate sources using selector --- src/components/CardStack.jsx | 2 +- src/selectors/index.js | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/components/CardStack.jsx b/src/components/CardStack.jsx index 98185be..bb47d71 100644 --- a/src/components/CardStack.jsx +++ b/src/components/CardStack.jsx @@ -89,7 +89,7 @@ class CardStack extends React.Component { function mapStateToProps(state) { return { - selected: state.app.selected, + selected: selectors.selectSelected(state), sourceError: state.app.errors.source, language: state.app.language, isCardstack: state.app.flags.isCardstack, diff --git a/src/selectors/index.js b/src/selectors/index.js index 76f8f40..8969136 100644 --- a/src/selectors/index.js +++ b/src/selectors/index.js @@ -1,16 +1,19 @@ -import { - createSelector -} from 'reselect' +import { createSelector} from 'reselect' // Input selectors export const getEvents = state => state.domain.events; export const getLocations = state => state.domain.locations; export const getCategories = state => state.domain.categories; export const getNarratives = state => state.domain.narratives; +export const getSelected = state => state.app.selected; export const getSites = (state) => { if (process.env.features.USE_SITES) return state.domain.sites; return []; } +export const getSources = state => { + if (process.env.features.USE_SOURCES) return state.domain.sources; + return []; +} export const getNotifications = state => state.domain.notifications; export const getTagTree = state => state.domain.tags; export const getTagsFilter = state => state.app.filters.tags; @@ -152,6 +155,23 @@ export const selectLocations = createSelector( } ); +/** + * Of all the sources, select those that are relevant to the selected events. + */ +export const selectSelected = createSelector( + [getSelected, getSources], + (selected, sources) => { + if (selected.length === 0) { + return [] + } + const sourceIds = selected.map(e => e.source) + const srcs = sources.filter(s => s.id.indexOf(sourceIds) > -1) + return selected.map((s, idx) => ({ + ...s, + source: srcs[idx] + })) + } +) /* * Select categories, return them as a list