1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

minor cleanup

This commit is contained in:
Ivar 2016-10-17 21:06:42 +02:00
parent 616da25d1b
commit d3090c123d
5 changed files with 69 additions and 52 deletions

View File

@ -1,7 +1,7 @@
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Input, Switch, Button } from 'react-toolbox';
import { createFeatureToggles } from '../../store/featureToggleActions';
import { createFeatureToggles } from '../../store/feature-actions';
class AddFeatureToggle extends React.Component {

View File

@ -1,5 +1,5 @@
import { connect } from 'react-redux';
import { toggleFeature, fetchFeatureToggles } from '../../store/featureToggleActions';
import { toggleFeature, fetchFeatureToggles } from '../../store/feature-actions';
import FeatureList from './FeatureList';
const mapStateToProps = (state) => ({

View File

@ -1,10 +1,11 @@
import { urls } from './urls';
import api from './feature-api';
export const ADD_FEATURE_TOGGLE = 'ADD_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 REQUEST_UPDATE_FEATURE_TOGGLES = 'REQUEST_UPDATE_FEATURE_TOGGLES';
export const START_UPDATE_FEATURE_TOGGLE = 'START_UPDATE_FEATURE_TOGGLE';
export const START_CREATE_FEATURE_TOGGLE = 'START_CREATE_FEATURE_TOGGLE';
export const RECEIVE_FEATURE_TOGGLES = 'RECEIVE_FEATURE_TOGGLES';
export const ERROR_RECEIVE_FEATURE_TOGGLES = 'ERROR_RECEIVE_FEATURE_TOGGLES';
export const ERROR_CREATING_FEATURE_TOGGLE = 'ERROR_CREATING_FEATURE_TOGGLE';
@ -62,9 +63,15 @@ function receiveFeatureToggles (json) {
};
}
function requestUpdateFeatureToggles () {
function startUpdateFeatureToggle () {
return {
type: REQUEST_UPDATE_FEATURE_TOGGLES,
type: START_UPDATE_FEATURE_TOGGLE,
};
}
function startCreateFeatureToggle () {
return {
type: START_CREATE_FEATURE_TOGGLE,
};
}
@ -79,63 +86,30 @@ function errorReceiveFeatureToggles (statusCode) {
export function fetchFeatureToggles () {
return dispatch => {
dispatch(requestFeatureToggles());
return fetch(urls.features)
.then(response => {
if (response.ok) {
return response.json();
} else {
let error = new Error('failed fetching');
error.status = response.status;
throw error;
}
})
return api.fetchAll()
.then(json => dispatch(receiveFeatureToggles(json)))
.catch(error => dispatch(errorReceiveFeatureToggles(error)));
};
}
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
export function createFeatureToggles (featureToggle) {
return dispatch => {
dispatch(requestUpdateFeatureToggles());
return fetch(urls.features, {
method: 'POST',
headers,
body: JSON.stringify(featureToggle),
})
.then(response => {
if (!response.ok) {
let error = new Error('failed fetching');
error.status = response.status;
throw error;
}
})
.then(() => dispatch(addFeatureToggle(featureToggle)))
.catch(error => dispatch(errorCreatingFeatureToggle(error)));
dispatch(startCreateFeatureToggle());
return api.create(featureToggle)
.then(() => dispatch(addFeatureToggle(featureToggle)))
.catch(error => dispatch(errorCreatingFeatureToggle(error)));
};
}
export function requestUpdateFeatureToggle (featureToggle) {
return dispatch => {
dispatch(requestUpdateFeatureToggles());
return fetch(`${urls.features}/${featureToggle.name}`, {
method: 'PUT',
headers,
body: JSON.stringify(featureToggle),
})
.then(response => {
if (!response.ok) {
let error = new Error('failed fetching');
error.status = response.status;
throw error;
}
})
.then(() => dispatch(updateFeatureToggle(featureToggle)))
.catch(error => dispatch(errorUpdatingFeatureToggle(error)));
dispatch(startUpdateFeatureToggle());
return api.update(featureToggle)
.then(() => dispatch(updateFeatureToggle(featureToggle)))
.catch(error => dispatch(errorUpdatingFeatureToggle(error)));
};
}

View File

@ -0,0 +1,43 @@
const URI = '/features';
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
function throwIfNotSuccess (response) {
if (!response.ok) {
let error = new Error('API call failed');
error.status = response.status;
throw error;
}
return response;
}
function fetchAll () {
return fetch(URI)
.then(throwIfNotSuccess)
.then(response => response.json());
}
function create (featureToggle) {
return fetch(URI, {
method: 'POST',
headers,
body: JSON.stringify(featureToggle),
}).then(throwIfNotSuccess);
}
function update (featureToggle) {
return fetch(`${URI}/${featureToggle.name}`, {
method: 'PUT',
headers,
body: JSON.stringify(featureToggle),
}).then(throwIfNotSuccess);
}
module.exports = {
fetchAll,
create,
update,
};

View File

@ -2,7 +2,7 @@ import {
ADD_FEATURE_TOGGLE,
RECEIVE_FEATURE_TOGGLES,
UPDATE_FEATURE_TOGGLE,
} from './featureToggleActions';
} from './feature-actions';
const feature = (state = {}, action) => {
switch (action.type) {