mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
store cleanups
This commit is contained in:
parent
5a0d53befc
commit
0e97f512c7
@ -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'));
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -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');
|
||||
|
@ -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));
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user