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

POST and PUT on toggles works now, but needs a bit cleanup

This commit is contained in:
ivaosthu 2016-10-17 17:26:01 +02:00
parent 990a54df2d
commit 616da25d1b
6 changed files with 88 additions and 28 deletions

View File

@ -7,7 +7,7 @@ const Feature = ({ onClick, name, enabled, strategies }) => (
<tr> <tr>
<td style={{ paddingTop: '1.5rem' }}><Switch onChange={onClick} checked={enabled} /></td> <td style={{ paddingTop: '1.5rem' }}><Switch onChange={onClick} checked={enabled} /></td>
<td><a href="/edit">{name}</a></td> <td><a href="/edit">{name}</a></td>
<td>{strategies.map(s => `${s.name}, `)}</td> <td>{strategies.map(s => s.name).join(', ')}</td>
<td style={{ textAlign: 'right' }}> <td style={{ textAlign: 'right' }}>
<FontIcon value="edit" /> <FontIcon value="edit" />
<FontIcon value="delete" style={{ color: 'red' }} /> <FontIcon value="delete" style={{ color: 'red' }} />

View File

@ -27,7 +27,7 @@ export default class FeatureList extends React.Component {
const features = this.props.features.map(featureToggle => const features = this.props.features.map(featureToggle =>
<Feature key={featureToggle.name} <Feature key={featureToggle.name}
{...featureToggle} {...featureToggle}
onClick={() => onFeatureClick(featureToggle.name)} onClick={() => onFeatureClick(featureToggle)}
/> />
); );

View File

@ -1,9 +1,14 @@
export const ADD_FEATURE_TOGGLE = 'ADD_FEATURE_TOGGLE'; import { urls } from './urls';
export const TOGGLE_FEATURE_TOGGLE = 'TOGGLE_FEATURE_TOGGLE';
export const REQUEST_FEATURE_TOGGLES = 'REQUEST_FEATURE_TOGGLES'; export const ADD_FEATURE_TOGGLE = 'ADD_FEATURE_TOGGLE';
export const RECEIVE_FEATURE_TOGGLES = 'RECEIVE_FEATURE_TOGGLES'; export const UPDATE_FEATURE_TOGGLE = 'UPDATE_FEATURE_TOGGLE';
export const ERROR_RECEIVE_FEATURE_TOGGLES = 'ERROR_RECEIVE_FEATURE_TOGGLES'; export const TOGGLE_FEATURE_TOGGLE = 'TOGGLE_FEATURE_TOGGLE';
export const ERROR_CREATING_FEATURE_TOGGLE = 'ERROR_CREATING_FEATURE_TOGGLE'; export const REQUEST_FEATURE_TOGGLES = 'REQUEST_FEATURE_TOGGLES';
export const REQUEST_UPDATE_FEATURE_TOGGLES = 'REQUEST_UPDATE_FEATURE_TOGGLES';
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';
export const ERROR_UPDATING_FEATURE_TOGGLE = 'ERROR_UPDATING_FEATURE_TOGGLE';
function addFeatureToggle (featureToggle) { function addFeatureToggle (featureToggle) {
return { return {
@ -12,6 +17,13 @@ function addFeatureToggle (featureToggle) {
}; };
}; };
function updateFeatureToggle (featureToggle) {
return {
type: UPDATE_FEATURE_TOGGLE,
featureToggle,
};
};
function errorCreatingFeatureToggle (statusCode) { function errorCreatingFeatureToggle (statusCode) {
return { return {
type: ERROR_CREATING_FEATURE_TOGGLE, type: ERROR_CREATING_FEATURE_TOGGLE,
@ -20,10 +32,20 @@ function errorCreatingFeatureToggle (statusCode) {
}; };
} }
export const toggleFeature = (featureName) => ({ function errorUpdatingFeatureToggle (statusCode) {
type: TOGGLE_FEATURE_TOGGLE, return {
name: featureName, type: ERROR_UPDATING_FEATURE_TOGGLE,
}); statusCode,
receivedAt: Date.now(),
};
}
export function toggleFeature (featureToggle) {
return dispatch => {
const newValue = Object.assign({}, featureToggle, { enabled: !featureToggle.enabled });
dispatch(requestUpdateFeatureToggle(newValue));
};
};
function requestFeatureToggles () { function requestFeatureToggles () {
@ -40,6 +62,12 @@ function receiveFeatureToggles (json) {
}; };
} }
function requestUpdateFeatureToggles () {
return {
type: REQUEST_UPDATE_FEATURE_TOGGLES,
};
}
function errorReceiveFeatureToggles (statusCode) { function errorReceiveFeatureToggles (statusCode) {
return { return {
type: ERROR_RECEIVE_FEATURE_TOGGLES, type: ERROR_RECEIVE_FEATURE_TOGGLES,
@ -51,7 +79,7 @@ function errorReceiveFeatureToggles (statusCode) {
export function fetchFeatureToggles () { export function fetchFeatureToggles () {
return dispatch => { return dispatch => {
dispatch(requestFeatureToggles()); dispatch(requestFeatureToggles());
return fetch('/features') return fetch(urls.features)
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
return response.json(); return response.json();
@ -66,16 +94,17 @@ export function fetchFeatureToggles () {
}; };
} }
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
export function createFeatureToggles (featureToggle) { export function createFeatureToggles (featureToggle) {
return dispatch => { return dispatch => {
dispatch(requestFeatureToggles()); dispatch(requestUpdateFeatureToggles());
return fetch('/features', { return fetch(urls.features, {
method: 'POST', method: 'POST',
headers: { headers,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(featureToggle), body: JSON.stringify(featureToggle),
}) })
.then(response => { .then(response => {
@ -89,3 +118,24 @@ export function createFeatureToggles (featureToggle) {
.catch(error => dispatch(errorCreatingFeatureToggle(error))); .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)));
};
}

View File

@ -1,22 +1,18 @@
import { import {
ADD_FEATURE_TOGGLE, ADD_FEATURE_TOGGLE,
TOGGLE_FEATURE_TOGGLE,
RECEIVE_FEATURE_TOGGLES, RECEIVE_FEATURE_TOGGLES,
UPDATE_FEATURE_TOGGLE,
} from './featureToggleActions'; } from './featureToggleActions';
const feature = (state = {}, action) => { const feature = (state = {}, action) => {
switch (action.type) { switch (action.type) {
case ADD_FEATURE_TOGGLE: case ADD_FEATURE_TOGGLE:
return action.featureToggle; return action.featureToggle;
case TOGGLE_FEATURE_TOGGLE: case UPDATE_FEATURE_TOGGLE:
if (state.name !== action.name) { if (state.name !== action.featureToggle.name) {
return state; return state;
} }
return action.featureToggle;
return Object.assign({}, state, {
enabled: !state.enabled,
});
default: default:
return state; return state;
} }
@ -29,7 +25,7 @@ const features = (state = [], action) => {
...state, ...state,
feature(undefined, action), feature(undefined, action),
]; ];
case TOGGLE_FEATURE_TOGGLE: case UPDATE_FEATURE_TOGGLE:
return state.map(t => return state.map(t =>
feature(t, action) feature(t, action)
); );

View File

@ -0,0 +1,6 @@
export const urls = {
features: '/features',
strategies: '/strategies',
events: '/events',
archive: '/archive/features',
};

View File

@ -62,6 +62,14 @@ module.exports = {
target: 'http://localhost:4242', target: 'http://localhost:4242',
secure: false, secure: false,
}, },
'/strategies': {
target: 'http://localhost:4242',
secure: false,
},
'/archive': {
target: 'http://localhost:4242',
secure: false,
},
}, },
}, },
}; };