This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Root.js
100 lines (78 loc) · 2.93 KB
/
Root.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
96
97
98
99
import React, { PropTypes, Component } from "react";
import { provideContext, connectToStores } from "fluxible-addons-react";
import { handleHistory } from "fluxible-router";
import Immutable from "immutable";
import Page from "../components/Page";
import NotFoundPage from "../containers/NotFoundPage";
import ErrorPage from "../containers/ErrorPage";
import LoadingPage from "../containers/LoadingPage";
import trackPageView from "../utils/trackPageView";
if (process.env.BROWSER) {
require("../style/Root.scss");
}
// Wrap Root with the fluxible context.
@provideContext
// Wrap with fluxible-router's history handler (required for routing)
// This also passes `currentRoute` as prop to the component
@handleHistory
// Listen to HtmlHeadStore and pass the document title to the component
@connectToStores(["HtmlHeadStore"], context =>
({ documentTitle: context.getStore("HtmlHeadStore").getTitle() })
)
export default class Root extends Component {
static propTypes = {
// props coming from fluxible-router's handleHistory
isNavigateComplete: PropTypes.bool,
currentRoute: PropTypes.object,
currentNavigateError: PropTypes.shape({
statusCode: PropTypes.number.isRequired,
message: PropTypes.string.isRequired
}),
// prop coming from HtmlHeadStore
documentTitle: PropTypes.string
}
componentDidUpdate(prevProps) {
const { documentTitle, currentRoute } = this.props;
if (prevProps.documentTitle !== documentTitle) {
document.title = documentTitle;
}
if (!Immutable.is(prevProps.currentRoute, currentRoute)) {
trackPageView(currentRoute.url);
}
}
render() {
const { currentRoute, currentNavigateError, isNavigateComplete } = this.props;
const Handler = currentRoute && currentRoute.handler;
let content;
if (currentNavigateError && currentNavigateError.statusCode === 404) {
// This "not found" error comes from a page init actions (InitActions.js)
// e.g. when a 500px API responds 404
content = <NotFoundPage />;
}
else if (currentNavigateError) {
// Generic error, usually always with statusCode 500
content = <ErrorPage err={ currentNavigateError } />;
}
else if (!Handler) {
// No handler: this is another case where a route is not found (e.g.
// is not defined in the routes.js config)
content = <NotFoundPage />;
}
else if (!isNavigateComplete) {
// Render a loading page while waiting the route's action to finish
content = <LoadingPage />;
}
else {
// Render the Handler (aka the page) for the current route. The route params
// (e.g. values from the URLs) are props being sent to the page component,
// for example the `id` of a photo for the `PhotoPage` component.
const params = currentRoute.params;
content = <Handler {...params} />;
}
return (
<Page footer={ isNavigateComplete }>
{ content }
</Page>
);
}
}