diff --git a/src/experiments/data/actions.js b/src/experiments/data/actions.js index 18e74e00b06..0db766f5760 100644 --- a/src/experiments/data/actions.js +++ b/src/experiments/data/actions.js @@ -9,42 +9,42 @@ import { apiFetch } from '@wordpress/data-controls'; import TYPES from './action-types'; import { EXPERIMENT_NAME_PREFIX, TRANSIENT_NAME_PREFIX } from './constants'; -function toggleFrontendExperiment( experimentName, newVariation ) { +function toggleFrontendExperiment(experimentName, newVariation) { const storageItem = JSON.parse( - window.localStorage.getItem( EXPERIMENT_NAME_PREFIX + experimentName ) + window.localStorage.getItem(EXPERIMENT_NAME_PREFIX + experimentName) ); storageItem.variationName = newVariation; window.localStorage.setItem( EXPERIMENT_NAME_PREFIX + experimentName, - JSON.stringify( storageItem ) + JSON.stringify(storageItem) ); } -function* toggleBackendExperiment( experimentName, newVariation ) { +function* toggleBackendExperiment(experimentName, newVariation) { try { const payload = {}; - payload[ TRANSIENT_NAME_PREFIX + experimentName ] = newVariation; - yield apiFetch( { + payload[TRANSIENT_NAME_PREFIX + experimentName] = newVariation; + yield apiFetch({ method: 'POST', path: '/wc-admin/options', headers: { 'content-type': 'application/json' }, - body: JSON.stringify( payload ), - } ); - } catch ( error ) { + body: JSON.stringify(payload), + }); + } catch (error) { throw new Error(); } } -export function* toggleExperiment( experimentName, currentVariation, source ) { +export function* toggleExperiment(experimentName, currentVariation, source) { const newVariation = currentVariation === 'control' ? 'treatment' : 'control'; - if ( source === 'frontend' ) { - toggleFrontendExperiment( experimentName, newVariation ); + if (source === 'frontend') { + toggleFrontendExperiment(experimentName, newVariation); } else { - yield toggleBackendExperiment( experimentName, newVariation ); + yield toggleBackendExperiment(experimentName, newVariation); } return { @@ -55,7 +55,7 @@ export function* toggleExperiment( experimentName, currentVariation, source ) { }; } -export function setExperiments( experiments ) { +export function setExperiments(experiments) { return { type: TYPES.SET_EXPERIMENTS, experiments, diff --git a/src/features/data/actions.js b/src/features/data/actions.js index 1746556042f..ec1c2264222 100644 --- a/src/features/data/actions.js +++ b/src/features/data/actions.js @@ -2,12 +2,13 @@ * External dependencies */ import { apiFetch } from '@wordpress/data-controls'; +import { controls } from '@wordpress/data'; /** * Internal dependencies */ import TYPES from './action-types'; -import { API_NAMESPACE } from './constants'; +import { API_NAMESPACE, STORE_KEY } from './constants'; export function* resetModifiedFeatures() { try { @@ -30,7 +31,12 @@ export function* toggleFeature(featureName) { path: API_NAMESPACE + '/features/' + featureName + '/toggle', headers: { 'content-type': 'application/json' }, }); - return yield setFeatures(response); + yield setFeatures(response); + yield controls.dispatch( + STORE_KEY, + 'invalidateResolutionForStoreSelector', + 'getModifiedFeatures' + ); } catch (error) { throw new Error(); } diff --git a/src/features/data/resolvers.js b/src/features/data/resolvers.js index 9b705333922..9014ea5146c 100644 --- a/src/features/data/resolvers.js +++ b/src/features/data/resolvers.js @@ -12,10 +12,14 @@ import { API_NAMESPACE, OPTION_NAME_PREFIX } from './constants'; export function* getModifiedFeatures() { try { const response = yield apiFetch({ - path: `${API_NAMESPACE}/options?search=` + OPTION_NAME_PREFIX, + path: `wc-admin/options?options=` + OPTION_NAME_PREFIX, }); - yield setModifiedFeatures(Object.keys(response)); + yield setModifiedFeatures( + response && response[OPTION_NAME_PREFIX] + ? Object.keys(response[OPTION_NAME_PREFIX]) + : [] + ); } catch (error) { throw new Error(); } diff --git a/src/features/index.js b/src/features/index.js index 784815b181b..3e2f67f5354 100644 --- a/src/features/index.js +++ b/src/features/index.js @@ -23,13 +23,17 @@ function Features() { return (
-

Features

- +

+ Features + +