diff --git a/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx index 25fa78fd5b..d3c5f97183 100644 --- a/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx +++ b/packages/unleash-frontend-next/src/component/feature/AddFeatureToggle.jsx @@ -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 { diff --git a/packages/unleash-frontend-next/src/component/feature/FeatureListContainer.jsx b/packages/unleash-frontend-next/src/component/feature/FeatureListContainer.jsx index 94f0cd5275..d2da4d3b2e 100644 --- a/packages/unleash-frontend-next/src/component/feature/FeatureListContainer.jsx +++ b/packages/unleash-frontend-next/src/component/feature/FeatureListContainer.jsx @@ -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) => ({ diff --git a/packages/unleash-frontend-next/src/store/featureToggleActions.js b/packages/unleash-frontend-next/src/store/feature-actions.js similarity index 57% rename from packages/unleash-frontend-next/src/store/featureToggleActions.js rename to packages/unleash-frontend-next/src/store/feature-actions.js index 60fcdd281b..9caebb131d 100644 --- a/packages/unleash-frontend-next/src/store/featureToggleActions.js +++ b/packages/unleash-frontend-next/src/store/feature-actions.js @@ -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))); }; } diff --git a/packages/unleash-frontend-next/src/store/feature-api.js b/packages/unleash-frontend-next/src/store/feature-api.js new file mode 100644 index 0000000000..7d07aa2c6f --- /dev/null +++ b/packages/unleash-frontend-next/src/store/feature-api.js @@ -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, +}; diff --git a/packages/unleash-frontend-next/src/store/features.js b/packages/unleash-frontend-next/src/store/features.js index 3fc88882fa..39fdf70042 100644 --- a/packages/unleash-frontend-next/src/store/features.js +++ b/packages/unleash-frontend-next/src/store/features.js @@ -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) {