-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithReduxFlow.js
More file actions
64 lines (55 loc) · 1.66 KB
/
Copy pathwithReduxFlow.js
File metadata and controls
64 lines (55 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from "react";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
const withReduxFlow = ({ getFetchAction, flag="", component }) => {
return WrappedComponent => {
@connect(({ loading }) => {
const isLoading = loading[`${flag}Loading`];
const dataLoaded = loading[`${flag}Loaded`];
return {
isLoading,
dataLoaded
};
})
@withRouter
class ApiFlowWrapper extends React.Component {
componentDidMount() {
const { history, dataLoaded } = this.props;
let action = getFetchAction(this.props);
// detects back button pressing, dont fetch on back button
if (history.action === "POP" && !!dataLoaded) {
return;
}
action.refs = {
isLoading: `${flag}Loading`,
dataLoaded: `${flag}Loaded`
};
this.props.dispatch(action);
}
componentWillReceiveProps(nextProps) {
const { history, location } = this.props;
// detects refresh by navigating to same route
let action = getFetchAction(nextProps);
if (
history.action === "PUSH" &&
nextProps.location.key !== location.key &&
location.pathname === nextProps.location.pathname
) {
action.refs = {
isLoading: `${flag}Loading`,
dataLoaded: `${flag}Loaded`
};
this.props.dispatch(action);
}
}
render() {
if (this.props.isLoading) {
return component;
}
return <WrappedComponent {...this.props} />;
}
}
return ApiFlowWrapper;
};
};
export default withReduxFlow;