diff options
Diffstat (limited to 'web/src/reducers.js')
-rw-r--r-- | web/src/reducers.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/web/src/reducers.js b/web/src/reducers.js new file mode 100644 index 000000000..0ce01c3b2 --- /dev/null +++ b/web/src/reducers.js @@ -0,0 +1,94 @@ +// 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 { fetchInfo } from './api' + +const infoReducer = (state = {}, action) => { + switch (action.type) { + case 'FETCH_INFO_SUCCESS': + return action.info + 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 + }), 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 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, + fetchInfoAction +} |