aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/components/container
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-01-28 19:11:00 +0100
committerNoah Loomans <noahloomans@gmail.com>2018-01-28 19:11:00 +0100
commit7dd214d57c7dab9626abef1516a862f46c1e02bd (patch)
tree9fb9c0060d0e595550a064caa4efb4289d2b7eb8 /src/client/react/components/container
parent9f438225db07b7214e4a41d133634309cba80073 (diff)
Refactor View.js
Diffstat (limited to 'src/client/react/components/container')
-rw-r--r--src/client/react/components/container/View.js68
1 files changed, 29 insertions, 39 deletions
diff --git a/src/client/react/components/container/View.js b/src/client/react/components/container/View.js
index 7539609..2823fe9 100644
--- a/src/client/react/components/container/View.js
+++ b/src/client/react/components/container/View.js
@@ -5,55 +5,49 @@ import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { fetchSchedule } from '../../actions/view';
-
-function cleanMeetingpointHTML(htmlStr) {
- const DOMPurify = createDOMPurify(window);
-
- return DOMPurify.sanitize(htmlStr, {
- ADD_ATTR: ['rules'],
- });
-}
+import extractSchedule from '../../lib/extractSchedule';
class View extends React.Component {
- componentDidMount() {
- if (!this.loadingFinished(this.props.user, this.props.week)) {
- this.props.dispatch(fetchSchedule(this.props.user, this.props.week));
- }
- }
-
- componentWillReceiveProps(nextProps) {
- if ((nextProps.user !== this.props.user || nextProps.week !== this.props.week)
- && !this.loadingFinished(nextProps.user, nextProps.week)) {
- this.props.dispatch(fetchSchedule(nextProps.user, nextProps.week));
- }
- }
-
- loadingFinished(user, week) {
- return this.props.schedules.hasOwnProperty(user) &&
- this.props.schedules[user].hasOwnProperty(week) &&
- this.props.schedules[user][week].state === 'finished';
+ renderLoading() {
+ return (
+ <div>
+ Loading...
+ </div>
+ );
}
- render() {
- if (!this.loadingFinished(this.props.user, this.props.week)) {
- return (
- <div>
- Loading...
- </div>
- );
- }
+ renderSchedule(htmlStr) {
+ const DOMPurify = createDOMPurify(window);
- const cleanHTML = cleanMeetingpointHTML(this.props.schedules[this.props.user][this.props.week].htmlStr);
+ const cleanHTML = DOMPurify.sanitize(htmlStr, {
+ ADD_ATTR: ['rules'],
+ });
return (
// eslint-disable-next-line react/no-danger
<div dangerouslySetInnerHTML={{ __html: cleanHTML }} />
);
}
+
+ render() {
+ const schedule = extractSchedule(this.props.schedules, this.props.user, this.props.week);
+
+ switch (schedule.state) {
+ case 'NOT_REQUESTED':
+ this.props.dispatch(fetchSchedule(this.props.user, this.props.week));
+ return this.renderLoading();
+ case 'FETCHING':
+ return this.renderLoading();
+ case 'FINISHED':
+ return this.renderSchedule(schedule.htmlStr);
+ default:
+ throw new Error(`${schedule.state} is not a valid schedule state.`);
+ }
+ }
}
View.propTypes = {
- user: PropTypes.string,
+ user: PropTypes.string.isRequired,
week: PropTypes.number.isRequired,
dispatch: PropTypes.func.isRequired,
schedules: PropTypes.objectOf(PropTypes.objectOf(PropTypes.shape({
@@ -62,10 +56,6 @@ View.propTypes = {
}))).isRequired,
};
-View.defaultProps = {
- user: null,
-};
-
const mapStateToProps = state => ({
schedules: state.view.schedules,
});