Browse Source

web: break the reducers module into logical units

This change applies best practices to split the current reducers module
in logical unit. Each reducer and its actions are moved into different
modules to ease further refactor and follow-up tests.

Change-Id: I75cc41ca3d31a61046868aafbc84505de661a99d
tags/3.4.0
Tristan Cacqueray 5 months ago
parent
commit
76867ca14a

+ 2
- 1
web/src/App.jsx View File

@@ -29,7 +29,8 @@ import {
29 29
 
30 30
 import logo from './images/logo.png'
31 31
 import { routes } from './routes'
32
-import { fetchConfigErrorsAction, setTenantAction } from './reducers'
32
+import { fetchConfigErrorsAction } from './actions/configErrors'
33
+import { setTenantAction } from './actions/tenant'
33 34
 
34 35
 
35 36
 class App extends React.Component {

+ 2
- 1
web/src/App.test.jsx View File

@@ -19,7 +19,8 @@ import ReactDOM from 'react-dom'
19 19
 import { Link, BrowserRouter as Router } from 'react-router-dom'
20 20
 import { Provider } from 'react-redux'
21 21
 
22
-import { createZuulStore, fetchInfoAction } from './reducers'
22
+import { fetchInfoAction } from './actions/info'
23
+import createZuulStore from './store'
23 24
 import App from './App'
24 25
 import TenantsPage from './pages/Tenants'
25 26
 import StatusPage from './pages/Status'

+ 28
- 0
web/src/actions/configErrors.js View File

@@ -0,0 +1,28 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+import { fetchConfigErrors } from '../api'
16
+
17
+export function fetchConfigErrorsAction (tenant) {
18
+  return (dispatch) => {
19
+    return fetchConfigErrors(tenant.apiPrefix)
20
+      .then(response => {
21
+        dispatch({type: 'FETCH_CONFIGERRORS_SUCCESS',
22
+                  errors: response.data})
23
+      })
24
+      .catch(error => {
25
+        throw (error)
26
+      })
27
+  }
28
+}

+ 27
- 0
web/src/actions/info.js View File

@@ -0,0 +1,27 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+import { fetchInfo } from '../api'
16
+
17
+export function fetchInfoAction () {
18
+  return (dispatch) => {
19
+    return fetchInfo()
20
+      .then(response => {
21
+        dispatch({type: 'FETCH_INFO_SUCCESS', info: response.data.info})
22
+      })
23
+      .catch(error => {
24
+        throw (error)
25
+      })
26
+  }
27
+}

+ 37
- 0
web/src/actions/tenant.js View File

@@ -0,0 +1,37 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+export function setTenantAction (name, whiteLabel) {
16
+  let apiPrefix = ''
17
+  let linkPrefix = ''
18
+  let routePrefix = ''
19
+  let defaultRoute = '/status'
20
+  if (!whiteLabel) {
21
+    apiPrefix = 'tenant/' + name + '/'
22
+    linkPrefix = '/t/' + name
23
+    routePrefix = '/t/:tenant'
24
+    defaultRoute = '/tenants'
25
+  }
26
+  return {
27
+    type: 'SET_TENANT',
28
+    tenant: {
29
+      name: name,
30
+      whiteLabel: whiteLabel,
31
+      defaultRoute: defaultRoute,
32
+      linkPrefix: linkPrefix,
33
+      apiPrefix: apiPrefix,
34
+      routePrefix: routePrefix
35
+    }
36
+  }
37
+}

+ 2
- 1
web/src/containers/status/ChangePanel.test.jsx View File

@@ -18,7 +18,8 @@ import ReactTestUtils from 'react-dom/test-utils'
18 18
 import { Link, BrowserRouter as Router } from 'react-router-dom'
19 19
 import { Provider } from 'react-redux'
20 20
 
21
-import { createZuulStore, setTenantAction } from '../../reducers'
21
+import { setTenantAction } from '../../actions/tenant'
22
+import createZuulStore from '../../store'
22 23
 import ChangePanel from './ChangePanel'
23 24
 
24 25
 

+ 4
- 3
web/src/index.js View File

@@ -25,12 +25,13 @@ import './index.css'
25 25
 
26 26
 import { getHomepageUrl } from './api'
27 27
 import registerServiceWorker from './registerServiceWorker'
28
-import { createZuulStore, fetchInfoAction } from './reducers'
28
+import { fetchInfoAction } from './actions/info'
29
+import createZuulStore from './store'
29 30
 import App from './App'
30 31
 
31
-// This calls the /api/info endpoint asynchronously, the App is connected
32
-// with redux and it will update the info prop when fetch succeed.
33 32
 const store = createZuulStore()
33
+
34
+// Load info endpoint
34 35
 store.dispatch(fetchInfoAction())
35 36
 
36 37
 ReactDOM.render(

+ 1
- 1
web/src/pages/ConfigErrors.jsx View File

@@ -19,7 +19,7 @@ import {
19 19
   Icon
20 20
 } from 'patternfly-react'
21 21
 
22
-import { fetchConfigErrorsAction } from '../reducers'
22
+import { fetchConfigErrorsAction } from '../actions/configErrors'
23 23
 
24 24
 class ConfigErrorsPage extends React.Component {
25 25
   static propTypes = {

+ 0
- 117
web/src/reducers.js View File

@@ -1,117 +0,0 @@
1
-// Copyright 2018 Red Hat, Inc
2
-//
3
-// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
-// not use this file except in compliance with the License. You may obtain
5
-// a copy of the License at
6
-//
7
-//      http://www.apache.org/licenses/LICENSE-2.0
8
-//
9
-// Unless required by applicable law or agreed to in writing, software
10
-// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
-// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
-// License for the specific language governing permissions and limitations
13
-// under the License.
14
-
15
-// Redux store enable to share global variables through state
16
-// To update the store, use a reducer and dispatch method,
17
-// see the App.setTenant method
18
-//
19
-// The store contains:
20
-//   info: the info object, tenant is set when white-label api
21
-//   tenant: the current tenant name, only used with multi-tenant api
22
-
23
-import { applyMiddleware, createStore, combineReducers } from 'redux'
24
-import thunk from 'redux-thunk'
25
-
26
-import { fetchConfigErrors, fetchInfo } from './api'
27
-
28
-const infoReducer = (state = {}, action) => {
29
-  switch (action.type) {
30
-    case 'FETCH_INFO_SUCCESS':
31
-      return action.info
32
-    default:
33
-      return state
34
-  }
35
-}
36
-
37
-const configErrorsReducer = (state = [], action) => {
38
-  switch (action.type) {
39
-    case 'FETCH_CONFIGERRORS_SUCCESS':
40
-      return action.errors
41
-    default:
42
-      return state
43
-  }
44
-}
45
-
46
-const tenantReducer = (state = {}, action) => {
47
-  switch (action.type) {
48
-    case 'SET_TENANT':
49
-      return action.tenant
50
-    default:
51
-      return state
52
-  }
53
-}
54
-
55
-function createZuulStore() {
56
-  return createStore(combineReducers({
57
-    info: infoReducer,
58
-    tenant: tenantReducer,
59
-    configErrors: configErrorsReducer,
60
-  }), applyMiddleware(thunk))
61
-}
62
-
63
-// Reducer actions
64
-function fetchInfoAction () {
65
-  return (dispatch) => {
66
-    return fetchInfo()
67
-      .then(response => {
68
-        dispatch({type: 'FETCH_INFO_SUCCESS', info: response.data.info})
69
-      })
70
-      .catch(error => {
71
-        throw (error)
72
-      })
73
-  }
74
-}
75
-function fetchConfigErrorsAction (tenant) {
76
-  return (dispatch) => {
77
-    return fetchConfigErrors(tenant.apiPrefix)
78
-      .then(response => {
79
-        dispatch({type: 'FETCH_CONFIGERRORS_SUCCESS',
80
-                  errors: response.data})
81
-      })
82
-      .catch(error => {
83
-        throw (error)
84
-      })
85
-  }
86
-}
87
-
88
-function setTenantAction (name, whiteLabel) {
89
-  let apiPrefix = ''
90
-  let linkPrefix = ''
91
-  let routePrefix = ''
92
-  let defaultRoute = '/status'
93
-  if (!whiteLabel) {
94
-    apiPrefix = 'tenant/' + name + '/'
95
-    linkPrefix = '/t/' + name
96
-    routePrefix = '/t/:tenant'
97
-    defaultRoute = '/tenants'
98
-  }
99
-  return {
100
-    type: 'SET_TENANT',
101
-    tenant: {
102
-      name: name,
103
-      whiteLabel: whiteLabel,
104
-      defaultRoute: defaultRoute,
105
-      linkPrefix: linkPrefix,
106
-      apiPrefix: apiPrefix,
107
-      routePrefix: routePrefix
108
-    }
109
-  }
110
-}
111
-
112
-export {
113
-  createZuulStore,
114
-  setTenantAction,
115
-  fetchConfigErrorsAction,
116
-  fetchInfoAction
117
-}

+ 22
- 0
web/src/reducers/configErrors.js View File

@@ -0,0 +1,22 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+export default (state = [], action) => {
16
+  switch (action.type) {
17
+    case 'FETCH_CONFIGERRORS_SUCCESS':
18
+      return action.errors
19
+    default:
20
+      return state
21
+  }
22
+}

+ 27
- 0
web/src/reducers/index.js View File

@@ -0,0 +1,27 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+import { combineReducers } from 'redux'
16
+
17
+import configErrors from './configErrors'
18
+import info from './info'
19
+import tenant from './tenant'
20
+
21
+const reducers = {
22
+  info,
23
+  configErrors,
24
+  tenant,
25
+}
26
+
27
+export default combineReducers(reducers)

+ 22
- 0
web/src/reducers/info.js View File

@@ -0,0 +1,22 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+export default (state = {}, action) => {
16
+  switch (action.type) {
17
+    case 'FETCH_INFO_SUCCESS':
18
+      return action.info
19
+    default:
20
+      return state
21
+  }
22
+}

+ 22
- 0
web/src/reducers/tenant.js View File

@@ -0,0 +1,22 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+export default (state = {}, action) => {
16
+  switch (action.type) {
17
+    case 'SET_TENANT':
18
+      return action.tenant
19
+    default:
20
+      return state
21
+  }
22
+}

+ 22
- 0
web/src/store.js View File

@@ -0,0 +1,22 @@
1
+// Copyright 2018 Red Hat, Inc
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+// not use this file except in compliance with the License. You may obtain
5
+// a copy of the License at
6
+//
7
+//      http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+// License for the specific language governing permissions and limitations
13
+// under the License.
14
+
15
+import { applyMiddleware, createStore } from 'redux'
16
+import thunk from 'redux-thunk'
17
+
18
+import appReducers from './reducers'
19
+
20
+export default function createZuulStore() {
21
+  return createStore(appReducers, applyMiddleware(thunk))
22
+}

Loading…
Cancel
Save