From d904690473b4161bcdb05dbba4ea568cf39881ec Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Wed, 15 Aug 2018 18:51:34 +0200 Subject: [PATCH] feat(update-toggle): Do not change route after feature toggle update --- .../src/component/error/error-container.jsx | 5 ++++- .../form/form-update-feature-container.jsx | 22 +++++++------------ frontend/src/store/error-store.js | 3 +++ frontend/src/store/feature-actions.js | 18 +++++++++++++++ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/frontend/src/component/error/error-container.jsx b/frontend/src/component/error/error-container.jsx index 566efa4093..8c536364f5 100644 --- a/frontend/src/component/error/error-container.jsx +++ b/frontend/src/component/error/error-container.jsx @@ -7,7 +7,10 @@ const mapDispatchToProps = { }; const mapStateToProps = state => ({ - errors: state.error.get('list').toArray(), + errors: state.error + .get('list') + .toArray() + .reverse(), }); export default connect(mapStateToProps, mapDispatchToProps)(ErrorComponent); diff --git a/frontend/src/component/feature/form/form-update-feature-container.jsx b/frontend/src/component/feature/form/form-update-feature-container.jsx index d8839142b6..b643bde3ae 100644 --- a/frontend/src/component/feature/form/form-update-feature-container.jsx +++ b/frontend/src/component/feature/form/form-update-feature-container.jsx @@ -1,6 +1,6 @@ import { connect } from 'react-redux'; -import { requestUpdateFeatureToggle } from '../../../store/feature-actions'; +import { requestUpdateFeatureToggleStrategies } from '../../../store/feature-actions'; import { createMapper, createActions } from '../../input-helpers'; import UpdateFeatureToggleComponent from './form-update-feature-component'; @@ -28,20 +28,14 @@ const prepare = (methods, dispatch, ownProps) => { methods.onSubmit = (input, features) => e => { e.preventDefault(); - if (Array.isArray(input.strategies)) { - input.strategies.forEach(s => { - delete s.id; - }); - } - delete input.description; - // take the status of the feature toggles from the central store in case `toggleFeature` function was called - const feat = features.find(f => f.name === input.name); - input.enabled = feat.enabled; + // This view will only update strategies! + const featureToggle = features.find(f => f.name === input.name); - // TODO: should add error handling - requestUpdateFeatureToggle(input)(dispatch) - .then(() => methods.clear()) - .then(() => ownProps.history.push(`/features`)); + const updatedStrategies = JSON.parse( + JSON.stringify(input.strategies, (key, value) => (key === 'id' ? undefined : value)) + ); + + requestUpdateFeatureToggleStrategies(featureToggle, updatedStrategies)(dispatch); }; methods.onCancel = evt => { diff --git a/frontend/src/store/error-store.js b/frontend/src/store/error-store.js index bdd2fb9b96..9a48f17c27 100644 --- a/frontend/src/store/error-store.js +++ b/frontend/src/store/error-store.js @@ -5,6 +5,7 @@ import { ERROR_CREATING_FEATURE_TOGGLE, ERROR_REMOVE_FEATURE_TOGGLE, ERROR_UPDATE_FEATURE_TOGGLE, + UPDATE_FEATURE_TOGGLE_STRATEGIES, } from './feature-actions'; import { ERROR_UPDATING_STRATEGY, ERROR_CREATING_STRATEGY, ERROR_RECEIVE_STRATEGIES } from './strategy/actions'; @@ -41,6 +42,8 @@ const strategies = (state = getInitState(), action) => { return addErrorIfNotAlreadyInList(state, action.error.message || '403 Forbidden'); case MUTE_ERROR: return state.update('list', list => list.remove(list.indexOf(action.error))); + case UPDATE_FEATURE_TOGGLE_STRATEGIES: + return addErrorIfNotAlreadyInList(state, action.info); default: return state; } diff --git a/frontend/src/store/feature-actions.js b/frontend/src/store/feature-actions.js index 23f29f9a29..03d2ba6275 100644 --- a/frontend/src/store/feature-actions.js +++ b/frontend/src/store/feature-actions.js @@ -1,6 +1,7 @@ import api from '../data/feature-api'; const debug = require('debug')('unleash:feature-actions'); import { dispatchAndThrow } from './util'; +import { MUTE_ERROR } from './error-actions'; export const ADD_FEATURE_TOGGLE = 'ADD_FEATURE_TOGGLE'; export const REMOVE_FEATURE_TOGGLE = 'REMOVE_FEATURE_TOGGLE'; @@ -15,6 +16,7 @@ export const ERROR_FETCH_FEATURE_TOGGLES = 'ERROR_FETCH_FEATURE_TOGGLES'; export const ERROR_CREATING_FEATURE_TOGGLE = 'ERROR_CREATING_FEATURE_TOGGLE'; export const ERROR_UPDATE_FEATURE_TOGGLE = 'ERROR_UPDATE_FEATURE_TOGGLE'; export const ERROR_REMOVE_FEATURE_TOGGLE = 'ERROR_REMOVE_FEATURE_TOGGLE'; +export const UPDATE_FEATURE_TOGGLE_STRATEGIES = 'UPDATE_FEATURE_TOGGLE_STRATEGIES'; export function toggleFeature(name) { debug('Toggle feature toggle ', name); @@ -84,6 +86,22 @@ export function requestUpdateFeatureToggle(featureToggle) { }; } +export function requestUpdateFeatureToggleStrategies(featureToggle, newStrategies) { + return dispatch => { + featureToggle.strategies = newStrategies; + dispatch({ type: START_UPDATE_FEATURE_TOGGLE }); + + return api + .update(featureToggle) + .then(() => { + const info = `${featureToggle.name} successfully updated!`; + setTimeout(() => dispatch({ type: MUTE_ERROR, error: info }), 1000); + return dispatch({ type: UPDATE_FEATURE_TOGGLE_STRATEGIES, featureToggle, info }); + }) + .catch(dispatchAndThrow(dispatch, ERROR_UPDATE_FEATURE_TOGGLE)); + }; +} + export function removeFeatureToggle(featureToggleName) { return dispatch => { dispatch({ type: START_REMOVE_FEATURE_TOGGLE });