This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
/
Admin.js
71 lines (62 loc) · 3.02 KB
/
Admin.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
/* eslint max-len: off */
/* global angular ADMIN_API_URL */
import React from 'react';
// redux, react-router, and saga form the 'kernel' on which admin-on-rest runs
import { combineReducers, createStore, compose, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { IndexRoute, Redirect, Router, Route, hashHistory } from 'react-router';
import { syncHistoryWithStore, routerMiddleware, routerReducer } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import { fork } from 'redux-saga/effects';
// prebuilt admin-on-rest features
import { adminReducer, crudSaga, CrudRoute } from 'admin-on-rest';
import { Delete } from 'admin-on-rest/lib/mui';
import { reducer as form } from 'redux-form';
import restClientFactory from './restClient';
import Layout from './Layout';
import LayoutWithMenu from './LayoutWithMenu';
import { OrderList, OrderEdit, OrderCreate } from './orders';
import { OrderProductList, OrderProductEdit } from './order-products';
import { ProductList, ProductEdit, ProductCreate } from './products';
import SignIn from './user/SignIn';
import userSagas from './user/sagas';
import userReducer from './user/reducer';
import { signOut } from './user/actions';
import redirectIfNotAuthenticatedFactory from './user/redirectIfNotAuthenticated';
// create a Redux app
const reducer = combineReducers({
admin: adminReducer([{ name: 'products' }, { name: 'orders' }, { name: 'order-products' }]),
routing: routerReducer,
user: userReducer(window.localStorage),
form,
});
const sagaMiddleware = createSagaMiddleware();
const store = createStore(reducer, undefined, compose(
applyMiddleware(routerMiddleware(hashHistory), sagaMiddleware),
window.devToolsExtension ? window.devToolsExtension() : f => f,
));
const redirectIfNotAuthenticated = redirectIfNotAuthenticatedFactory(store);
const restClient = restClientFactory(ADMIN_API_URL, () => window.localStorage.getItem('token'), () => store.dispatch(signOut.request()));
sagaMiddleware.run(function* () { // eslint-disable-line func-names
yield fork(crudSaga(restClient));
yield fork(userSagas);
});
// initialize the router
const history = syncHistoryWithStore(hashHistory, store);
// bootstrap redux and the routes
const App = () => (
<Provider store={store}>
<Router history={history}>
<Redirect from="/" to="/admin/orders" />
<Route path="/sign-in" component={Layout}>
<IndexRoute component={SignIn} />
</Route>
<Route path="/admin" component={LayoutWithMenu} onEnter={redirectIfNotAuthenticated}>
<CrudRoute path="products" list={ProductList} edit={ProductEdit} create={ProductCreate} remove={Delete} />
<CrudRoute path="orders" list={OrderList} edit={OrderEdit} create={OrderCreate} remove={Delete} />
<CrudRoute path="order-products" list={OrderProductList} edit={OrderProductEdit} remove={Delete} options={{ hideInMenu: true }} />
</Route>
</Router>
</Provider>
);
export default App;