-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathQueryStateUpdater.js
95 lines (82 loc) · 2.59 KB
/
QueryStateUpdater.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import { injectIntl } from 'react-intl';
import { withRouter } from 'react-router';
import { isEqual } from 'lodash';
import { withModules } from '../Modules';
import {
updateQueryResource,
getLocationQuery,
updateLocation,
getCurrentModule,
isQueryResourceModule,
getQueryResourceState,
} from '../../locationService';
// onMount of stripes, sync the query state to the location.
class QueryStateUpdater extends React.Component {
static propTypes = {
history: PropTypes.shape({
listen: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
push: PropTypes.func.isRequired,
}).isRequired,
location: PropTypes.shape({
pathname: PropTypes.string,
}).isRequired,
modules: PropTypes.shape({
app: PropTypes.arrayOf(PropTypes.object),
}),
queryClient: PropTypes.shape({
removeQueries: PropTypes.func.isRequired,
}).isRequired,
stripes: PropTypes.shape({
store: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
}),
}),
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
}
constructor(props) {
super(props);
this.store = props.stripes.store;
}
componentDidMount() {
let curQuery = getLocationQuery(this.props.location);
const prevQueryState = {};
this._unsubscribe = this.store.subscribe(() => {
const { history, location } = this.props;
const module = this.curModule;
if (module && isQueryResourceModule(module, location)) {
const { moduleName } = module;
const queryState = getQueryResourceState(module, this.store);
// only update location if query state has changed
if (!isEqual(queryState, prevQueryState[moduleName])) {
curQuery = updateLocation(module, curQuery, this.store, history, location);
prevQueryState[moduleName] = queryState;
}
}
});
// remove QueryProvider cache to be 100% sure we're starting from a clean slate.
this.props.queryClient.removeQueries();
}
componentDidUpdate(prevProps) {
const { modules, location } = this.props;
this.curModule = getCurrentModule(modules, location);
if (this.curModule && !isEqual(location, prevProps.location)) {
updateQueryResource(location, this.curModule, this.store);
}
}
componentWillUnmount() {
this._unsubscribe();
}
render = () => this.props.children;
}
export default compose(
injectIntl,
withRouter,
withModules,
)(QueryStateUpdater);