diff --git a/src/experiments/data/actions.js b/src/experiments/data/actions.js index 0db766f5760..18e74e00b06 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,