From 125bd06f598c8eb57e60338aa7371124bbdb8b9b Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Thu, 3 Jan 2019 16:40:38 +0000 Subject: [PATCH] WIP: increment narrative through redux --- src/actions/index.js | 14 ++++ src/components/Dashboard.jsx | 46 +++++++------ src/components/NarrativeCard.js | 111 ++++++++++++-------------------- src/js/utilities.js | 18 ------ src/reducers/app.js | 2 +- 5 files changed, 80 insertions(+), 111 deletions(-) diff --git a/src/actions/index.js b/src/actions/index.js index 3a5e890..7f39a6d 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -213,6 +213,20 @@ export function updateNarrative(narrative) { } } +export const INCREMENT_NARRATIVE_CURRENT = 'INCREMENT_NARRATIVE_CURRENT'; +export function incrementNarrativeCurrent() { + return { + type: INCREMENT_NARRATIVE_CURRENT + } +} + +export const DECREMENT_NARRATIVE_CURRENT = 'DECREMENT_NARRATIVE_CURRENT'; +export function decrementNarrativeCurrent() { + return { + type: DECREMENT_NARRATIVE_CURRENT + } +} + export const RESET_ALLFILTERS = 'RESET_ALLFILTERS' export function resetAllFilters() { return { diff --git a/src/components/Dashboard.jsx b/src/components/Dashboard.jsx index ee8fac7..f73580a 100644 --- a/src/components/Dashboard.jsx +++ b/src/components/Dashboard.jsx @@ -71,64 +71,69 @@ class Dashboard extends React.Component { } render() { + const { actions, app, domain, ui } = this.props return (
this.getCategoryColor(category) }} /> this.props.actions.updateSelected([])} + onToggleCardstack={() => actions.updateSelected([])} getNarrativeLinks={event => this.getNarrativeLinks(event)} getCategoryColor={category => this.getCategoryColor(category)} /> this.props.actions.toggleInfoPopup()} + ui={ui} + app={app} + toggle={() => actions.toggleInfoPopup()} /> - {this.props.app.source ? ( + {app.source ? ( { - this.props.actions.updateSource(null)} + actions.updateSource(null)} } /> ) : null}
); @@ -153,6 +158,5 @@ function injectSource(id) { export default connect( state => state, - // injectNarrative(0), mapDispatchToProps, )(Dashboard); diff --git a/src/components/NarrativeCard.js b/src/components/NarrativeCard.js index 5d771a5..ab3fc87 100644 --- a/src/components/NarrativeCard.js +++ b/src/components/NarrativeCard.js @@ -2,87 +2,56 @@ import React from 'react'; import { connect } from 'react-redux' import { selectActiveNarrative } from '../selectors' -class NarrativeCard extends React.Component { - - constructor() { - super(); - - this.state = { - step: 0 - } - } - - goToPrevKeyFrame() { - if (this.state.step > 0) { - this.setState({ step: this.state.step - 1 }); - } - } - - goToNextKeyFrame() { - if (this.state.step < this.props.narrative.steps.length - 1) { - this.setState({ step: this.state.step + 1 }); - } - } - - componentDidMount() { - if (!this.props.narrative) return - - const step = this.props.narrative.steps[this.state.step]; - this.props.onSelect([step]); - } - - componentDidUpdate(prevProps, prevState) { - if (prevProps.narrative === this.props.narrative && this.state.step !== prevState.step) { - const step = this.props.narrative.steps[this.state.step]; - this.props.onSelect([step]); - } else if (prevProps.narrative !== this.props.narrative && this.props.narrative !== null) { - this.setState({ - step: 0 - }, () => { - const step = this.props.narrative.steps[this.state.step]; - this.props.onSelect([step]); - }); - } - } - - renderClose() { +function NarrativeCard ({ narrative, methods }) { + const { onSelectNarrative, onNext, onPrev } = methods + function renderClose() { return ( ) } - render() { - // no display if no narrative - if (!this.props.narrative) return null - - const { steps, current } = this.props.narrative - - if (steps[current]) { - const step = steps[current]; - - return ( -
- {this.renderClose()} -

{this.props.narrative.label}

-

{this.props.narrative.description}

-
- location_on - {this.state.step + 1}/{steps.length}. {step.location} -
-
-
this.goToPrevKeyFrame()}>←
-
= this.props.narrative.steps.length - 1) ? 'disabled ' : ''} action`} onClick={() => this.goToNextKeyFrame()}>→
-
+ function _renderActions(current, steps) { + return ( +
+
- ); - } else { - return null - } +
= steps.length - 1) ? 'disabled ' : ''} action`} + onClick={onNext}>→ +
+
+ ) + } + + // no display if no narrative + if (!narrative) return null + + const { steps, current } = narrative + + if (steps[current]) { + const step = steps[current]; + + return ( +
+ {renderClose()} +

{narrative.label}

+

{narrative.description}

+
+ location_on + {current + 1}/{steps.length}. {step.location} +
+ {_renderActions(current, steps)} +
+ ); + } else { + return null } } diff --git a/src/js/utilities.js b/src/js/utilities.js index 2c4b3c6..751a93d 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -93,21 +93,3 @@ export function injectSource(id) { } }) } - -/** - * Debugging function: put in place of a mapStateToProps function to - * view that narrative modal by default - */ -export function injectNarrative(idx) { - return state => { - console.log(state.domain.narratives) - return { - ...state, - app: { - ...state.app, - narrative: state.domain.narratives.length ? state.domain.narratives[idx] : null - } - } - } -} - diff --git a/src/reducers/app.js b/src/reducers/app.js index 1904703..d5ef6d1 100644 --- a/src/reducers/app.js +++ b/src/reducers/app.js @@ -37,7 +37,7 @@ function updateNarrative(appState, action) { ...appState, narrative: action.narrative, narrativeState: { - current: 0 + current: !!action.narrative ? 0 : null } } }