This repository was archived by the owner on May 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 923
Expand file tree
/
Copy pathRoutes.js
More file actions
196 lines (178 loc) · 6.1 KB
/
Routes.js
File metadata and controls
196 lines (178 loc) · 6.1 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, { Component } from 'react';
import { arrayOf, bool, object, func, shape, string } from 'prop-types';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { Switch, Route, withRouter } from 'react-router-dom';
import { NotFoundPage } from './containers';
import { NamedRedirect, LoadableComponentErrorBoundary } from './components';
import { locationChanged } from './ducks/Routing.duck';
import { propTypes } from './util/types';
import * as log from './util/log';
import { canonicalRoutePath } from './util/routes';
import routeConfiguration from './routeConfiguration';
const canShowComponent = props => {
const { isAuthenticated, route } = props;
const { auth } = route;
return !auth || isAuthenticated;
};
const callLoadData = props => {
const { match, location, route, dispatch, logoutInProgress } = props;
const { loadData, name } = route;
const shouldLoadData =
typeof loadData === 'function' && canShowComponent(props) && !logoutInProgress;
if (shouldLoadData) {
dispatch(loadData(match.params, location.search))
.then(() => {
// eslint-disable-next-line no-console
console.log(`loadData success for ${name} route`);
})
.catch(e => {
log.error(e, 'load-data-failed', { routeName: name });
});
}
};
const setPageScrollPosition = location => {
if (!location.hash) {
// No hash, scroll to top
window.scroll({
top: 0,
left: 0,
});
} else {
const el = document.querySelector(location.hash);
if (el) {
// Found element from the current page with the given fragment identifier,
// scrolling to that element.
//
// NOTE: This only works on in-app navigation within the same page.
// If smooth scrolling is needed between different pages, one needs to wait
// 1. loadData fetch and
// 2. code-chunk fetch
// before making el.scrollIntoView call.
el.scrollIntoView({
block: 'start',
behavior: 'smooth',
});
}
}
};
const handleLocationChanged = (dispatch, location) => {
setPageScrollPosition(location);
const path = canonicalRoutePath(routeConfiguration(), location);
dispatch(locationChanged(location, path));
};
/**
* RouteComponentRenderer handles loadData calls on client-side.
* It also checks authentication and redirects unauthenticated users
* away from routes that are for authenticated users only
* (aka "auth: true" is set in routeConfiguration.js)
*
* This component is a container: it needs to be connected to Redux.
*/
class RouteComponentRenderer extends Component {
componentDidMount() {
// Calling loadData on initial rendering (on client side).
callLoadData(this.props);
handleLocationChanged(this.props.dispatch, this.props.location);
}
componentDidUpdate(prevProps) {
// Call for handleLocationChanged affects store/state
// and it generates an unnecessary update.
if (prevProps.location !== this.props.location) {
// Calling loadData after initial rendering (on client side).
// This makes it possible to use loadData as default client side data loading technique.
// However it is better to fetch data before location change to avoid "Loading data" state.
callLoadData(this.props);
handleLocationChanged(this.props.dispatch, this.props.location);
}
}
render() {
const { route, match, location, staticContext } = this.props;
const { component: RouteComponent, authPage = 'SignupPage', extraProps } = route;
const canShow = canShowComponent(this.props);
if (!canShow) {
staticContext.unauthorized = true;
}
return canShow ? (
<LoadableComponentErrorBoundary>
<RouteComponent params={match.params} location={location} {...extraProps} />
</LoadableComponentErrorBoundary>
) : (
<NamedRedirect
name={authPage}
state={{ from: `${location.pathname}${location.search}${location.hash}` }}
/>
);
}
}
RouteComponentRenderer.defaultProps = { staticContext: {} };
RouteComponentRenderer.propTypes = {
isAuthenticated: bool.isRequired,
logoutInProgress: bool.isRequired,
route: propTypes.route.isRequired,
match: shape({
params: object.isRequired,
url: string.isRequired,
}).isRequired,
location: shape({
search: string.isRequired,
}).isRequired,
staticContext: object,
dispatch: func.isRequired,
};
const mapStateToProps = state => {
const { isAuthenticated, logoutInProgress } = state.Auth;
return { isAuthenticated, logoutInProgress };
};
const RouteComponentContainer = compose(connect(mapStateToProps))(RouteComponentRenderer);
/**
* Routes component creates React Router rendering setup.
* It needs routeConfiguration (named as "routes") through props.
* Using that configuration it creates navigation on top of page-level
* components. Essentially, it's something like:
* <Switch>
* <Route render={pageA} />
* <Route render={pageB} />
* </Switch>
*/
const Routes = (props, context) => {
const { isAuthenticated, logoutInProgress, routes } = props;
const toRouteComponent = route => {
const renderProps = {
isAuthenticated,
logoutInProgress,
route,
};
// By default, our routes are exact.
// https://reacttraining.com/react-router/web/api/Route/exact-bool
const isExact = route.exact != null ? route.exact : true;
return (
<Route
key={route.name}
path={route.path}
exact={isExact}
render={matchProps => (
<RouteComponentContainer
{...renderProps}
match={matchProps.match}
location={matchProps.location}
staticContext={matchProps.staticContext}
/>
)}
/>
);
};
// N.B. routes prop within React Router needs to stay the same,
// so that React is is not rerendering page component.
// That's why we pass-in props.routes instead of calling routeConfiguration here.
return (
<Switch>
{routes.map(toRouteComponent)}
<Route component={NotFoundPage} />
</Switch>
);
};
Routes.propTypes = {
routes: arrayOf(propTypes.route).isRequired,
};
export default withRouter(Routes);