forked from gpbl/isomorphic500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteStore.js
103 lines (76 loc) · 1.99 KB
/
RouteStore.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
100
101
102
103
import Fluxible from "fluxible/addons";
import Actions from "../constants/Actions";
class RouteStore extends Fluxible.BaseStore {
static storeName = "RouteStore"
static handlers = {
[Actions.CHANGE_ROUTE_SUCCESS]: "handleNavigate",
[Actions.CHANGE_ROUTE_START]: "changeRoute",
[Actions.STATUS_404]: "status404",
[Actions.STATUS_500]: "status500"
}
constructor(dispatcher) {
super(dispatcher);
this.currentRoute = null;
this.currentPageName = null;
this.err = null;
}
changeRoute(route) {
if (this.currentRoute && this.currentRoute.url === route.url) {
// Do nothing if trying to change to the same route
return;
}
this.currentRoute = route || {};
this.err = null;
this.currentRoute.isLoading = true;
this.currentPageName = null;
this.emitChange();
}
handleNavigate(route) {
if (route.url !== this.currentRoute.url) {
// Too late! This may happen when a route action has been finished
// to load, but the route did change again.
return;
}
this.currentPageName = null;
this.err = null;
this.currentRoute.isLoading = false;
this.emitChange();
}
status404() {
this.currentPageName = "404";
if (this.currentRoute) {
this.currentRoute.isLoading = false;
}
this.emitChange();
}
status500({ err }) {
this.err = err;
this.currentPageName = "500";
if (this.currentRoute) {
this.currentRoute.isLoading = false;
}
this.emitChange();
}
getCurrentRoute() {
return this.currentRoute;
}
getCurrentPageName() {
return this.currentPageName;
}
getNavigationError() {
return this.err;
}
dehydrate() {
return {
err: this.err,
currentRoute: this.currentRoute,
currentPageName: this.currentPageName
};
}
rehydrate({ err, currentRoute, currentPageName }) {
this.err = err;
this.currentRoute = currentRoute;
this.currentPageName = currentPageName;
}
}
export default RouteStore;