This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
/
Admin.js
140 lines (132 loc) · 4.57 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
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
import React, { createElement } from 'react';
import PropTypes from 'prop-types';
import { createStore, compose, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createHistory from 'history/createHashHistory';
import { ConnectedRouter, routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import { all, fork } from 'redux-saga/effects';
import withContext from 'recompose/withContext';
import { Route, Switch } from 'react-router-dom';
import { USER_LOGOUT } from './actions/authActions';
import Login from './mui/auth/Login';
import createAppReducer from './reducer';
import { crudSaga } from './sideEffect/saga';
import Menu from './mui/layout/Menu';
import Logout from './mui/auth/Logout';
import TranslationProvider from './i18n/TranslationProvider';
import AdminRoutes from './AdminRoutes';
const Admin = ({
appLayout,
authClient,
children,
customReducers = {},
customSagas = [],
customRoutes = [],
dashboard,
history,
locale,
messages = {},
menu = Menu,
catchAll,
restClient,
theme,
title = 'Admin on REST',
loginPage,
logoutButton,
initialState,
}) => {
const appReducer = createAppReducer(customReducers, locale);
const resettableAppReducer = (state, action) =>
appReducer(action.type !== USER_LOGOUT ? state : undefined, action);
const saga = function* rootSaga() {
yield all([crudSaga(restClient, authClient), ...customSagas].map(fork));
};
const sagaMiddleware = createSagaMiddleware();
const routerHistory = history || createHistory();
const store = createStore(
resettableAppReducer,
initialState,
compose(
applyMiddleware(sagaMiddleware, routerMiddleware(routerHistory)),
typeof window !== 'undefined' && window.devToolsExtension
? window.devToolsExtension()
: f => f
)
);
sagaMiddleware.run(saga);
const logout = authClient ? createElement(logoutButton || Logout) : null;
return (
<Provider store={store}>
<TranslationProvider messages={messages}>
<ConnectedRouter history={routerHistory}>
<Switch>
{loginPage && (
<Route
exact
path="/login"
render={({ location }) =>
createElement(loginPage, {
location,
title,
theme,
})}
/>
)}
<Route
path="/"
render={routeProps => (
<AdminRoutes
appLayout={appLayout}
catchAll={catchAll}
customRoutes={customRoutes}
dashboard={dashboard}
logout={logout}
menu={menu}
theme={theme}
title={title}
{...routeProps}
>
{children}
</AdminRoutes>
)}
/>
</Switch>
</ConnectedRouter>
</TranslationProvider>
</Provider>
);
};
const componentPropType = PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]);
Admin.propTypes = {
appLayout: componentPropType,
authClient: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
catchAll: componentPropType,
customSagas: PropTypes.array,
customReducers: PropTypes.object,
customRoutes: PropTypes.array,
dashboard: componentPropType,
history: PropTypes.object,
loginPage: componentPropType,
logoutButton: componentPropType,
menu: componentPropType,
restClient: PropTypes.func,
theme: PropTypes.object,
title: PropTypes.node,
locale: PropTypes.string,
messages: PropTypes.object,
initialState: PropTypes.object,
};
Admin.defaultProps = {
loginPage: Login,
};
export default withContext(
{
authClient: PropTypes.func,
},
({ authClient }) => ({ authClient })
)(Admin);