-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
69 lines (58 loc) · 1.67 KB
/
index.jsx
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
import appApi from 'app/api';
import autobind from 'autobind-decorator';
import createStore from 'app/createStore';
import MiddlewareMap from 'app/utils/middlewareMap';
import React from 'react';
import runRouteHandlers from 'app/utils/runRouteHandlers';
import { PRE_MIDDLEWARE, ROUTES } from 'app/routes';
import { Provider } from 'react-redux';
export default class App {
constructor(renderer, onRedirect, api = appApi) {
this.api = api;
this.onRedirect = onRedirect;
this.renderer = renderer;
this.store = createStore();
this.preMiddleware = new MiddlewareMap(PRE_MIDDLEWARE);
}
@autobind
dispatchAction(actionCreator, ...args) {
let action = actionCreator(this.api, this.store, this.dispatchAction, ...args),
actionPromise;
if (action) {
this.store.dispatch(action);
actionPromise = action.payload.promise;
}
return actionPromise || Promise.resolve(action && action.payload);
}
@autobind
redirect(path) {
this.onRedirect(path);
}
@autobind
render(Component, props) {
this.renderer(
<Provider store={this.store}>
<Component {...props}/>
</Provider>
);
}
route(path, req) {
let preMiddleware = this.preMiddleware.match(req.path),
routeHandler = ROUTES[path],
handlers = [],
{ store } = this;
let res = {
dispatchAction: this.dispatchAction,
redirect: this.redirect,
render: this.render
};
if (preMiddleware) {
handlers = handlers.concat(preMiddleware);
}
if (routeHandler) {
handlers.push(routeHandler);
}
runRouteHandlers(handlers, [req, res, store]);
}
}
App.routes = Object.keys(ROUTES);