From 0e97f512c7cc53f1455687543802edf5d7d349b4 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Wed, 26 Oct 2016 17:10:57 +0200 Subject: [PATCH] store cleanups --- .../component/feature/form-add-container.jsx | 3 +- .../src/store/error-store.js | 8 +- .../src/store/feature-actions.js | 89 +++++-------------- .../src/store/feature-api.js | 18 ++-- 4 files changed, 41 insertions(+), 77 deletions(-) diff --git a/packages/unleash-frontend-next/src/component/feature/form-add-container.jsx b/packages/unleash-frontend-next/src/component/feature/form-add-container.jsx index 99cfe2d7dd..9cb6aeef63 100644 --- a/packages/unleash-frontend-next/src/component/feature/form-add-container.jsx +++ b/packages/unleash-frontend-next/src/component/feature/form-add-container.jsx @@ -10,10 +10,9 @@ const prepare = (methods, dispatch) => { methods.onSubmit = (input) => ( (e) => { e.preventDefault(); - // TODO: should add error handling createFeatureToggles(input)(dispatch) .then(() => methods.clear()) - .then(() => window.history.back()); + .then(() => hashHistory.push('/features')); } ); diff --git a/packages/unleash-frontend-next/src/store/error-store.js b/packages/unleash-frontend-next/src/store/error-store.js index c2bccde45d..eab6f1492e 100644 --- a/packages/unleash-frontend-next/src/store/error-store.js +++ b/packages/unleash-frontend-next/src/store/error-store.js @@ -1,8 +1,9 @@ import { List, Map as $Map } from 'immutable'; import { MUTE_ERRORS } from './error-actions'; import { - ERROR_RECEIVE_FEATURE_TOGGLES, + ERROR_FETCH_FEATURE_TOGGLES, ERROR_CREATING_FEATURE_TOGGLE, + ERROR_REMOVE_FEATURE_TOGGLE, } from './feature-actions'; const debug = require('debug')('unleash:error-store'); @@ -17,10 +18,11 @@ function getInitState () { const strategies = (state = getInitState(), action) => { switch (action.type) { case ERROR_CREATING_FEATURE_TOGGLE: - case ERROR_RECEIVE_FEATURE_TOGGLES: + case ERROR_REMOVE_FEATURE_TOGGLE: + case ERROR_FETCH_FEATURE_TOGGLES: debug('Got error', action); return state - .update('list', (list) => list.push(action.errorMsg)) + .update('list', (list) => list.push(action.error.message)) .set('showError', true); case MUTE_ERRORS: debug('muting errors'); diff --git a/packages/unleash-frontend-next/src/store/feature-actions.js b/packages/unleash-frontend-next/src/store/feature-actions.js index 9f6cabdaf1..935ec7a4f8 100644 --- a/packages/unleash-frontend-next/src/store/feature-actions.js +++ b/packages/unleash-frontend-next/src/store/feature-actions.js @@ -5,46 +5,16 @@ export const ADD_FEATURE_TOGGLE = 'ADD_FEATURE_TOGGLE'; export const REMOVE_FEATURE_TOGGLE = 'REMOVE_FEATURE_TOGGLE'; export const UPDATE_FEATURE_TOGGLE = 'UPDATE_FEATURE_TOGGLE'; export const TOGGLE_FEATURE_TOGGLE = 'TOGGLE_FEATURE_TOGGLE'; -export const REQUEST_FEATURE_TOGGLES = 'REQUEST_FEATURE_TOGGLES'; +export const START_FETCH_FEATURE_TOGGLES = 'START_FETCH_FEATURE_TOGGLES'; export const START_UPDATE_FEATURE_TOGGLE = 'START_UPDATE_FEATURE_TOGGLE'; export const START_CREATE_FEATURE_TOGGLE = 'START_CREATE_FEATURE_TOGGLE'; +export const START_REMOVE_FEATURE_TOGGLE = 'START_REMOVE_FEATURE_TOGGLE'; export const RECEIVE_FEATURE_TOGGLES = 'RECEIVE_FEATURE_TOGGLES'; -export const ERROR_RECEIVE_FEATURE_TOGGLES = 'ERROR_RECEIVE_FEATURE_TOGGLES'; +export const ERROR_FETCH_FEATURE_TOGGLES = 'ERROR_FETCH_FEATURE_TOGGLES'; export const ERROR_CREATING_FEATURE_TOGGLE = 'ERROR_CREATING_FEATURE_TOGGLE'; -export const ERROR_UPDATING_FEATURE_TOGGLE = 'ERROR_UPDATING_FEATURE_TOGGLE'; +export const ERROR_UPDATE_FEATURE_TOGGLE = 'ERROR_UPDATE_FEATURE_TOGGLE'; export const ERROR_REMOVE_FEATURE_TOGGLE = 'ERROR_REMOVE_FEATURE_TOGGLE'; -function addFeatureToggle (featureToggle) { - return { - type: ADD_FEATURE_TOGGLE, - featureToggle, - }; -}; - -function updateFeatureToggle (featureToggle) { - return { - type: UPDATE_FEATURE_TOGGLE, - featureToggle, - }; -}; - -function errorCreatingFeatureToggle (error) { - return { - type: ERROR_CREATING_FEATURE_TOGGLE, - statusCode: error.statusCode, - errorMsg: error.msg, - receivedAt: Date.now(), - }; -} - -function errorUpdatingFeatureToggle (statusCode) { - return { - type: ERROR_UPDATING_FEATURE_TOGGLE, - statusCode, - receivedAt: Date.now(), - }; -} - export function toggleFeature (featureToggle) { debug('Toggle feature toggle ', featureToggle); return dispatch => { @@ -60,11 +30,6 @@ export function editFeatureToggle (featureToggle) { }; }; -function requestFeatureToggles () { - return { - type: REQUEST_FEATURE_TOGGLES, - }; -} function receiveFeatureToggles (json) { debug('reviced feature toggles', json); @@ -75,61 +40,51 @@ function receiveFeatureToggles (json) { }; } -function startUpdateFeatureToggle () { - return { - type: START_UPDATE_FEATURE_TOGGLE, - }; -} - -function startCreateFeatureToggle () { - return { - type: START_CREATE_FEATURE_TOGGLE, - }; -} - -function errorReceiveFeatureToggles (statusCode) { - return { - type: ERROR_RECEIVE_FEATURE_TOGGLES, - statusCode, - receivedAt: Date.now(), +function dispatchAndThrow (dispatch, type) { + return (error) => { + dispatch({ type, error, receivedAt: Date.now() }); + throw error; }; } export function fetchFeatureToggles () { debug('Start fetching feature toggles'); return dispatch => { - dispatch(requestFeatureToggles()); + dispatch({ type: START_FETCH_FEATURE_TOGGLES }); return api.fetchAll() .then(json => dispatch(receiveFeatureToggles(json))) - .catch(error => dispatch(errorReceiveFeatureToggles(error))); + .catch(dispatchAndThrow(dispatch, ERROR_FETCH_FEATURE_TOGGLES)); }; } export function createFeatureToggles (featureToggle) { return dispatch => { - dispatch(startCreateFeatureToggle()); + dispatch({ type: START_CREATE_FEATURE_TOGGLE }); return api.create(featureToggle) - .then(() => dispatch(addFeatureToggle(featureToggle))) - .catch(error => dispatch(errorCreatingFeatureToggle(error))); + .then(() => dispatch({ type: ADD_FEATURE_TOGGLE, featureToggle })) + .catch(dispatchAndThrow(dispatch, ERROR_CREATING_FEATURE_TOGGLE)); }; } export function requestUpdateFeatureToggle (featureToggle) { return dispatch => { - dispatch(startUpdateFeatureToggle()); + dispatch({ type: START_UPDATE_FEATURE_TOGGLE }); return api.update(featureToggle) - .then(() => dispatch(updateFeatureToggle(featureToggle))) - .catch(error => dispatch(errorUpdatingFeatureToggle(error))); + .then(() => dispatch({ type: UPDATE_FEATURE_TOGGLE, featureToggle })) + .catch(dispatchAndThrow(dispatch, ERROR_UPDATE_FEATURE_TOGGLE)); }; } export function removeFeatureToggle (featureToggleName) { - return dispatch => (api.remove(featureToggleName) + return dispatch => { + dispatch({ type: START_REMOVE_FEATURE_TOGGLE }); + + return api.remove(featureToggleName) .then(() => dispatch({ type: REMOVE_FEATURE_TOGGLE, featureToggleName })) - .catch(error => dispatch({ type: ERROR_REMOVE_FEATURE_TOGGLE, featureToggleName, error })) - ); + .catch(dispatchAndThrow(dispatch, ERROR_REMOVE_FEATURE_TOGGLE)); + }; } diff --git a/packages/unleash-frontend-next/src/store/feature-api.js b/packages/unleash-frontend-next/src/store/feature-api.js index 7e2b8e4d27..2716f9fc4c 100644 --- a/packages/unleash-frontend-next/src/store/feature-api.js +++ b/packages/unleash-frontend-next/src/store/feature-api.js @@ -7,12 +7,20 @@ const headers = { function throwIfNotSuccess (response) { if (!response.ok) { - let error = new Error('API call failed'); - error.statusCode = response.status; - error.msg = response.json(); - throw error; + if (response.status > 400 && response.status < 404) { + return new Promise((resolve, reject) => { + response.json().then(body => { + const errorMsg = body && body.length > 0 ? body[0].msg : 'API call failed'; + let error = new Error(errorMsg); + error.statusCode = response.status; + reject(error); + }); + }); + } else { + return Promise.reject(new Error(response.statusText)); + } } - return response; + return Promise.resolve(response); } function fetchAll () {