diff --git a/web/src/App.jsx b/web/src/App.jsx index 5c22083ac1..85d2009606 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 494e1596ee..58a88f421d 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 0000000000..8f52f06328 --- /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 0000000000..8894eef1b5 --- /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 0000000000..992f4e9714 --- /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 f36c6a74c7..b4089fa267 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 fb42857b46..933186e454 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 04470c0716..05aceec5dd 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 b02c9f6378..0000000000 --- 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 0000000000..dd0a284231 --- /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 0000000000..6f287aed55 --- /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 0000000000..5386622a7f --- /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 0000000000..b634b03b27 --- /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 0000000000..bad6b19466 --- /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)) +}