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) => (
|
methods.onSubmit = (input) => (
|
||||||
(e) => {
|
(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
// TODO: should add error handling
|
|
||||||
createFeatureToggles(input)(dispatch)
|
createFeatureToggles(input)(dispatch)
|
||||||
.then(() => methods.clear())
|
.then(() => methods.clear())
|
||||||
.then(() => window.history.back());
|
.then(() => hashHistory.push('/features'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { List, Map as $Map } from 'immutable';
|
import { List, Map as $Map } from 'immutable';
|
||||||
import { MUTE_ERRORS } from './error-actions';
|
import { MUTE_ERRORS } from './error-actions';
|
||||||
import {
|
import {
|
||||||
ERROR_RECEIVE_FEATURE_TOGGLES,
|
ERROR_FETCH_FEATURE_TOGGLES,
|
||||||
ERROR_CREATING_FEATURE_TOGGLE,
|
ERROR_CREATING_FEATURE_TOGGLE,
|
||||||
|
ERROR_REMOVE_FEATURE_TOGGLE,
|
||||||
} from './feature-actions';
|
} from './feature-actions';
|
||||||
|
|
||||||
const debug = require('debug')('unleash:error-store');
|
const debug = require('debug')('unleash:error-store');
|
||||||
@ -17,10 +18,11 @@ function getInitState () {
|
|||||||
const strategies = (state = getInitState(), action) => {
|
const strategies = (state = getInitState(), action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case ERROR_CREATING_FEATURE_TOGGLE:
|
case ERROR_CREATING_FEATURE_TOGGLE:
|
||||||
case ERROR_RECEIVE_FEATURE_TOGGLES:
|
case ERROR_REMOVE_FEATURE_TOGGLE:
|
||||||
|
case ERROR_FETCH_FEATURE_TOGGLES:
|
||||||
debug('Got error', action);
|
debug('Got error', action);
|
||||||
return state
|
return state
|
||||||
.update('list', (list) => list.push(action.errorMsg))
|
.update('list', (list) => list.push(action.error.message))
|
||||||
.set('showError', true);
|
.set('showError', true);
|
||||||
case MUTE_ERRORS:
|
case MUTE_ERRORS:
|
||||||
debug('muting 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 REMOVE_FEATURE_TOGGLE = 'REMOVE_FEATURE_TOGGLE';
|
||||||
export const UPDATE_FEATURE_TOGGLE = 'UPDATE_FEATURE_TOGGLE';
|
export const UPDATE_FEATURE_TOGGLE = 'UPDATE_FEATURE_TOGGLE';
|
||||||
export const TOGGLE_FEATURE_TOGGLE = 'TOGGLE_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_UPDATE_FEATURE_TOGGLE = 'START_UPDATE_FEATURE_TOGGLE';
|
||||||
export const START_CREATE_FEATURE_TOGGLE = 'START_CREATE_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 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_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';
|
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) {
|
export function toggleFeature (featureToggle) {
|
||||||
debug('Toggle feature toggle ', featureToggle);
|
debug('Toggle feature toggle ', featureToggle);
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
@ -60,11 +30,6 @@ export function editFeatureToggle (featureToggle) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function requestFeatureToggles () {
|
|
||||||
return {
|
|
||||||
type: REQUEST_FEATURE_TOGGLES,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function receiveFeatureToggles (json) {
|
function receiveFeatureToggles (json) {
|
||||||
debug('reviced feature toggles', json);
|
debug('reviced feature toggles', json);
|
||||||
@ -75,61 +40,51 @@ function receiveFeatureToggles (json) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function startUpdateFeatureToggle () {
|
function dispatchAndThrow (dispatch, type) {
|
||||||
return {
|
return (error) => {
|
||||||
type: START_UPDATE_FEATURE_TOGGLE,
|
dispatch({ type, error, receivedAt: Date.now() });
|
||||||
};
|
throw error;
|
||||||
}
|
|
||||||
|
|
||||||
function startCreateFeatureToggle () {
|
|
||||||
return {
|
|
||||||
type: START_CREATE_FEATURE_TOGGLE,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function errorReceiveFeatureToggles (statusCode) {
|
|
||||||
return {
|
|
||||||
type: ERROR_RECEIVE_FEATURE_TOGGLES,
|
|
||||||
statusCode,
|
|
||||||
receivedAt: Date.now(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchFeatureToggles () {
|
export function fetchFeatureToggles () {
|
||||||
debug('Start fetching feature toggles');
|
debug('Start fetching feature toggles');
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
dispatch(requestFeatureToggles());
|
dispatch({ type: START_FETCH_FEATURE_TOGGLES });
|
||||||
|
|
||||||
return api.fetchAll()
|
return api.fetchAll()
|
||||||
.then(json => dispatch(receiveFeatureToggles(json)))
|
.then(json => dispatch(receiveFeatureToggles(json)))
|
||||||
.catch(error => dispatch(errorReceiveFeatureToggles(error)));
|
.catch(dispatchAndThrow(dispatch, ERROR_FETCH_FEATURE_TOGGLES));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createFeatureToggles (featureToggle) {
|
export function createFeatureToggles (featureToggle) {
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
dispatch(startCreateFeatureToggle());
|
dispatch({ type: START_CREATE_FEATURE_TOGGLE });
|
||||||
|
|
||||||
return api.create(featureToggle)
|
return api.create(featureToggle)
|
||||||
.then(() => dispatch(addFeatureToggle(featureToggle)))
|
.then(() => dispatch({ type: ADD_FEATURE_TOGGLE, featureToggle }))
|
||||||
.catch(error => dispatch(errorCreatingFeatureToggle(error)));
|
.catch(dispatchAndThrow(dispatch, ERROR_CREATING_FEATURE_TOGGLE));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function requestUpdateFeatureToggle (featureToggle) {
|
export function requestUpdateFeatureToggle (featureToggle) {
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
dispatch(startUpdateFeatureToggle());
|
dispatch({ type: START_UPDATE_FEATURE_TOGGLE });
|
||||||
|
|
||||||
return api.update(featureToggle)
|
return api.update(featureToggle)
|
||||||
.then(() => dispatch(updateFeatureToggle(featureToggle)))
|
.then(() => dispatch({ type: UPDATE_FEATURE_TOGGLE, featureToggle }))
|
||||||
.catch(error => dispatch(errorUpdatingFeatureToggle(error)));
|
.catch(dispatchAndThrow(dispatch, ERROR_UPDATE_FEATURE_TOGGLE));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeFeatureToggle (featureToggleName) {
|
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 }))
|
.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) {
|
function throwIfNotSuccess (response) {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
let error = new Error('API call failed');
|
if (response.status > 400 && response.status < 404) {
|
||||||
error.statusCode = response.status;
|
return new Promise((resolve, reject) => {
|
||||||
error.msg = response.json();
|
response.json().then(body => {
|
||||||
throw error;
|
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 () {
|
function fetchAll () {
|
||||||
|
Loading…
Reference in New Issue
Block a user