From 74ef968a45e147069d044fbfde814886d7275aa3 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 6 Jul 2018 18:36:55 +0200 Subject: client/View: Move API call to action --- src/client/react/components/container/View.js | 51 ++++++--------------------- 1 file changed, 11 insertions(+), 40 deletions(-) (limited to 'src/client/react/components/container') diff --git a/src/client/react/components/container/View.js b/src/client/react/components/container/View.js index 2ec2dbd..a601226 100644 --- a/src/client/react/components/container/View.js +++ b/src/client/react/components/container/View.js @@ -26,6 +26,7 @@ import extractSchedule from '../../lib/extractSchedule'; import Schedule from '../presentational/Schedule'; import Loading from '../presentational/Loading'; +import * as actions from '../../store/actions'; class View extends React.Component { static propTypes = { @@ -33,51 +34,15 @@ class View extends React.Component { state: PropTypes.string.isRequired, htmlStr: PropTypes.string, })).isRequired, - - // react-router user: PropTypes.string.isRequired, week: PropTypes.number.isRequired, - // redux - dispatch: PropTypes.func.isRequired, + fetchScheduleIfNeeded: PropTypes.func.isRequired, }; componentDidMount() { - this.fetchScheduleIfNeeded(); - } - - fetchScheduleIfNeeded() { - const { - user, - week, - schedules, - dispatch, - } = this.props; - - const schedule = extractSchedule(schedules, user, week); - - if (schedule.state === 'NOT_REQUESTED') { - fetch(`/get/${user}?week=${week}`).then(r => r.text()).then( - // success - (htmlStr) => { - dispatch({ - type: 'VIEW/FETCH_SCHEDULE_SUCCESS', - user, - week, - htmlStr, - }); - }, - - // error - () => { - dispatch({ - type: 'VIEW/FETCH_SCHEDULE_FAILURE', - week, - user, - }); - }, - ); - } + const { fetchScheduleIfNeeded, user, week } = this.props; + fetchScheduleIfNeeded(user, week); } render() { @@ -112,4 +77,10 @@ const mapStateToProps = (state) => { }; }; -export default connect(mapStateToProps)(View); +const mapDispatchToProps = dispatch => ({ + fetchScheduleIfNeeded: (user, week) => ( + dispatch(actions.fetchScheduleIfNeeded(user, week)) + ), +}); + +export default connect(mapStateToProps, mapDispatchToProps)(View); -- cgit v1.1