summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Cacqueray <tdecacqu@redhat.com>2018-12-02 01:05:02 +0000
committerTristan Cacqueray <tdecacqu@redhat.com>2018-12-02 01:05:02 +0000
commit76867ca14aec3e7e6df8604e911826049b57946f (patch)
tree9ad76d6bee006fe76f194d37f986ef84e097d08d
parent300915538c04ebe782da31464fa937b204579d6a (diff)
downloadzuul-76867ca14aec3e7e6df8604e911826049b57946f.tar.gz
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
-rw-r--r--web/src/App.jsx3
-rw-r--r--web/src/App.test.jsx3
-rw-r--r--web/src/actions/configErrors.js28
-rw-r--r--web/src/actions/info.js27
-rw-r--r--web/src/actions/tenant.js37
-rw-r--r--web/src/containers/status/ChangePanel.test.jsx3
-rw-r--r--web/src/index.js7
-rw-r--r--web/src/pages/ConfigErrors.jsx2
-rw-r--r--web/src/reducers.js117
-rw-r--r--web/src/reducers/configErrors.js22
-rw-r--r--web/src/reducers/index.js27
-rw-r--r--web/src/reducers/info.js22
-rw-r--r--web/src/reducers/tenant.js22
-rw-r--r--web/src/store.js22
14 files changed, 218 insertions, 124 deletions
diff --git a/web/src/App.jsx b/web/src/App.jsx
index 5c22083ac..85d200960 100644
--- a/web/src/App.jsx
+++ b/web/src/App.jsx
@@ -29,7 +29,8 @@ import {
import logo from './images/logo.png'
import { routes } from './routes'
-import { fetchConfigErrorsAction, setTenantAction } from './reducers'
+import { fetchConfigErrorsAction } from './actions/configErrors'
+import { setTenantAction } from './actions/tenant'
class App extends React.Component {
diff --git a/web/src/App.test.jsx b/web/src/App.test.jsx
index 494e1596e..58a88f421 100644
--- a/web/src/App.test.jsx
+++ b/web/src/App.test.jsx
@@ -19,7 +19,8 @@ import ReactDOM from 'react-dom'
import { Link, BrowserRouter as Router } from 'react-router-dom'
import { Provider } from 'react-redux'
-import { createZuulStore, fetchInfoAction } from './reducers'
+import { fetchInfoAction } from './actions/info'
+import createZuulStore from './store'
import App from './App'
import TenantsPage from './pages/Tenants'
import StatusPage from './pages/Status'
diff --git a/web/src/actions/configErrors.js b/web/src/actions/configErrors.js
new file mode 100644
index 000000000..8f52f0632
--- /dev/null
+++ b/web/src/actions/configErrors.js
@@ -0,0 +1,28 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+import { fetchConfigErrors } from '../api'
+
+export function fetchConfigErrorsAction (tenant) {
+ return (dispatch) => {
+ return fetchConfigErrors(tenant.apiPrefix)
+ .then(response => {
+ dispatch({type: 'FETCH_CONFIGERRORS_SUCCESS',
+ errors: response.data})
+ })
+ .catch(error => {
+ throw (error)
+ })
+ }
+}
diff --git a/web/src/actions/info.js b/web/src/actions/info.js
new file mode 100644
index 000000000..8894eef1b
--- /dev/null
+++ b/web/src/actions/info.js
@@ -0,0 +1,27 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+import { fetchInfo } from '../api'
+
+export function fetchInfoAction () {
+ return (dispatch) => {
+ return fetchInfo()
+ .then(response => {
+ dispatch({type: 'FETCH_INFO_SUCCESS', info: response.data.info})
+ })
+ .catch(error => {
+ throw (error)
+ })
+ }
+}
diff --git a/web/src/actions/tenant.js b/web/src/actions/tenant.js
new file mode 100644
index 000000000..992f4e971
--- /dev/null
+++ b/web/src/actions/tenant.js
@@ -0,0 +1,37 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+export function setTenantAction (name, whiteLabel) {
+ let apiPrefix = ''
+ let linkPrefix = ''
+ let routePrefix = ''
+ let defaultRoute = '/status'
+ if (!whiteLabel) {
+ apiPrefix = 'tenant/' + name + '/'
+ linkPrefix = '/t/' + name
+ routePrefix = '/t/:tenant'
+ defaultRoute = '/tenants'
+ }
+ return {
+ type: 'SET_TENANT',
+ tenant: {
+ name: name,
+ whiteLabel: whiteLabel,
+ defaultRoute: defaultRoute,
+ linkPrefix: linkPrefix,
+ apiPrefix: apiPrefix,
+ routePrefix: routePrefix
+ }
+ }
+}
diff --git a/web/src/containers/status/ChangePanel.test.jsx b/web/src/containers/status/ChangePanel.test.jsx
index f36c6a74c..b4089fa26 100644
--- a/web/src/containers/status/ChangePanel.test.jsx
+++ b/web/src/containers/status/ChangePanel.test.jsx
@@ -18,7 +18,8 @@ import ReactTestUtils from 'react-dom/test-utils'
import { Link, BrowserRouter as Router } from 'react-router-dom'
import { Provider } from 'react-redux'
-import { createZuulStore, setTenantAction } from '../../reducers'
+import { setTenantAction } from '../../actions/tenant'
+import createZuulStore from '../../store'
import ChangePanel from './ChangePanel'
diff --git a/web/src/index.js b/web/src/index.js
index fb42857b4..933186e45 100644
--- a/web/src/index.js
+++ b/web/src/index.js
@@ -25,12 +25,13 @@ import './index.css'
import { getHomepageUrl } from './api'
import registerServiceWorker from './registerServiceWorker'
-import { createZuulStore, fetchInfoAction } from './reducers'
+import { fetchInfoAction } from './actions/info'
+import createZuulStore from './store'
import App from './App'
-// This calls the /api/info endpoint asynchronously, the App is connected
-// with redux and it will update the info prop when fetch succeed.
const store = createZuulStore()
+
+// Load info endpoint
store.dispatch(fetchInfoAction())
ReactDOM.render(
diff --git a/web/src/pages/ConfigErrors.jsx b/web/src/pages/ConfigErrors.jsx
index 04470c071..05aceec5d 100644
--- a/web/src/pages/ConfigErrors.jsx
+++ b/web/src/pages/ConfigErrors.jsx
@@ -19,7 +19,7 @@ import {
Icon
} from 'patternfly-react'
-import { fetchConfigErrorsAction } from '../reducers'
+import { fetchConfigErrorsAction } from '../actions/configErrors'
class ConfigErrorsPage extends React.Component {
static propTypes = {
diff --git a/web/src/reducers.js b/web/src/reducers.js
deleted file mode 100644
index b02c9f637..000000000
--- a/web/src/reducers.js
+++ /dev/null
@@ -1,117 +0,0 @@
-// Copyright 2018 Red Hat, Inc
-//
-// Licensed under the Apache License, Version 2.0 (the "License"); you may
-// not use this file except in compliance with the License. You may obtain
-// a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-// License for the specific language governing permissions and limitations
-// under the License.
-
-// Redux store enable to share global variables through state
-// To update the store, use a reducer and dispatch method,
-// see the App.setTenant method
-//
-// The store contains:
-// info: the info object, tenant is set when white-label api
-// tenant: the current tenant name, only used with multi-tenant api
-
-import { applyMiddleware, createStore, combineReducers } from 'redux'
-import thunk from 'redux-thunk'
-
-import { fetchConfigErrors, fetchInfo } from './api'
-
-const infoReducer = (state = {}, action) => {
- switch (action.type) {
- case 'FETCH_INFO_SUCCESS':
- return action.info
- default:
- return state
- }
-}
-
-const configErrorsReducer = (state = [], action) => {
- switch (action.type) {
- case 'FETCH_CONFIGERRORS_SUCCESS':
- return action.errors
- default:
- return state
- }
-}
-
-const tenantReducer = (state = {}, action) => {
- switch (action.type) {
- case 'SET_TENANT':
- return action.tenant
- default:
- return state
- }
-}
-
-function createZuulStore() {
- return createStore(combineReducers({
- info: infoReducer,
- tenant: tenantReducer,
- configErrors: configErrorsReducer,
- }), applyMiddleware(thunk))
-}
-
-// Reducer actions
-function fetchInfoAction () {
- return (dispatch) => {
- return fetchInfo()
- .then(response => {
- dispatch({type: 'FETCH_INFO_SUCCESS', info: response.data.info})
- })
- .catch(error => {
- throw (error)
- })
- }
-}
-function fetchConfigErrorsAction (tenant) {
- return (dispatch) => {
- return fetchConfigErrors(tenant.apiPrefix)
- .then(response => {
- dispatch({type: 'FETCH_CONFIGERRORS_SUCCESS',
- errors: response.data})
- })
- .catch(error => {
- throw (error)
- })
- }
-}
-
-function setTenantAction (name, whiteLabel) {
- let apiPrefix = ''
- let linkPrefix = ''
- let routePrefix = ''
- let defaultRoute = '/status'
- if (!whiteLabel) {
- apiPrefix = 'tenant/' + name + '/'
- linkPrefix = '/t/' + name
- routePrefix = '/t/:tenant'
- defaultRoute = '/tenants'
- }
- return {
- type: 'SET_TENANT',
- tenant: {
- name: name,
- whiteLabel: whiteLabel,
- defaultRoute: defaultRoute,
- linkPrefix: linkPrefix,
- apiPrefix: apiPrefix,
- routePrefix: routePrefix
- }
- }
-}
-
-export {
- createZuulStore,
- setTenantAction,
- fetchConfigErrorsAction,
- fetchInfoAction
-}
diff --git a/web/src/reducers/configErrors.js b/web/src/reducers/configErrors.js
new file mode 100644
index 000000000..dd0a28423
--- /dev/null
+++ b/web/src/reducers/configErrors.js
@@ -0,0 +1,22 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+export default (state = [], action) => {
+ switch (action.type) {
+ case 'FETCH_CONFIGERRORS_SUCCESS':
+ return action.errors
+ default:
+ return state
+ }
+}
diff --git a/web/src/reducers/index.js b/web/src/reducers/index.js
new file mode 100644
index 000000000..6f287aed5
--- /dev/null
+++ b/web/src/reducers/index.js
@@ -0,0 +1,27 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+import { combineReducers } from 'redux'
+
+import configErrors from './configErrors'
+import info from './info'
+import tenant from './tenant'
+
+const reducers = {
+ info,
+ configErrors,
+ tenant,
+}
+
+export default combineReducers(reducers)
diff --git a/web/src/reducers/info.js b/web/src/reducers/info.js
new file mode 100644
index 000000000..5386622a7
--- /dev/null
+++ b/web/src/reducers/info.js
@@ -0,0 +1,22 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+export default (state = {}, action) => {
+ switch (action.type) {
+ case 'FETCH_INFO_SUCCESS':
+ return action.info
+ default:
+ return state
+ }
+}
diff --git a/web/src/reducers/tenant.js b/web/src/reducers/tenant.js
new file mode 100644
index 000000000..b634b03b2
--- /dev/null
+++ b/web/src/reducers/tenant.js
@@ -0,0 +1,22 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+export default (state = {}, action) => {
+ switch (action.type) {
+ case 'SET_TENANT':
+ return action.tenant
+ default:
+ return state
+ }
+}
diff --git a/web/src/store.js b/web/src/store.js
new file mode 100644
index 000000000..bad6b1946
--- /dev/null
+++ b/web/src/store.js
@@ -0,0 +1,22 @@
+// Copyright 2018 Red Hat, Inc
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+import { applyMiddleware, createStore } from 'redux'
+import thunk from 'redux-thunk'
+
+import appReducers from './reducers'
+
+export default function createZuulStore() {
+ return createStore(appReducers, applyMiddleware(thunk))
+}