aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-02-02 16:11:27 +0100
committerNoah Loomans <noahloomans@gmail.com>2018-02-02 16:11:27 +0100
commitf7c886a6e1e9f82873747e8fa7ae732ba57665e4 (patch)
treedf6b7af4f82197dd00b7b1be609ccc2aadd92021 /src/client
parentfeb7e197e691d39595d814e7ee59813b10ef4eca (diff)
Don't send fetch request during render
Diffstat (limited to 'src/client')
-rw-r--r--src/client/react/components/container/View.js67
1 files changed, 47 insertions, 20 deletions
diff --git a/src/client/react/components/container/View.js b/src/client/react/components/container/View.js
index bd75c9c..f919dbc 100644
--- a/src/client/react/components/container/View.js
+++ b/src/client/react/components/container/View.js
@@ -10,28 +10,55 @@ import extractSchedule from '../../lib/extractSchedule';
import Schedule from '../presentational/Schedule';
import Loading from '../presentational/Loading';
-const View = ({
- schedules,
- match,
- location,
- dispatch,
-}) => {
- const user = userFromMatch(match);
- const week = weekFromLocation(location);
- const schedule = extractSchedule(schedules, user, week);
-
- switch (schedule.state) {
- case 'NOT_REQUESTED':
+class View extends React.Component {
+ componentDidMount() {
+ this.fetchScheduleIfNeeded();
+ }
+
+ componentDidUpdate() {
+ this.fetchScheduleIfNeeded();
+ }
+
+ fetchScheduleIfNeeded() {
+ const {
+ schedules,
+ match,
+ location,
+ dispatch,
+ } = this.props;
+
+ const user = userFromMatch(match);
+ const week = weekFromLocation(location);
+ const schedule = extractSchedule(schedules, user, week);
+
+ if (schedule.state === 'NOT_REQUESTED') {
dispatch(fetchSchedule(user, week));
- return <Loading />;
- case 'FETCHING':
- return <Loading />;
- case 'FINISHED':
- return <Schedule htmlStr={schedule.htmlStr} />;
- default:
- throw new Error(`${schedule.state} is not a valid schedule state.`);
+ }
}
-};
+
+ render() {
+ const {
+ schedules,
+ match,
+ location,
+ } = this.props;
+
+ const user = userFromMatch(match);
+ const week = weekFromLocation(location);
+ const schedule = extractSchedule(schedules, user, week);
+
+ switch (schedule.state) {
+ case 'NOT_REQUESTED':
+ return <Loading />;
+ case 'FETCHING':
+ return <Loading />;
+ case 'FINISHED':
+ return <Schedule htmlStr={schedule.htmlStr} />;
+ default:
+ throw new Error(`${schedule.state} is not a valid schedule state.`);
+ }
+ }
+}
View.propTypes = {
schedules: PropTypes.objectOf(PropTypes.objectOf(PropTypes.shape({