Update reset to default functionality

This commit is contained in:
Lourens Schep 2022-03-15 11:29:43 -03:00
parent 46cba23313
commit 66073a6b8f
4 changed files with 39 additions and 25 deletions

View File

@ -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,

View File

@ -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();
}

View File

@ -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();
}

View File

@ -23,13 +23,17 @@ function Features() {
return (
<div id="wc-admin-test-helper-features">
<h2>Features</h2>
<Button
disabled={modifiedFeatures.length === 0}
onClick={() => resetModifiedFeatures()}
>
Reset to defaults
</Button>
<h2>
Features
<Button
disabled={modifiedFeatures.length === 0}
onClick={() => resetModifiedFeatures()}
isSecondary
style={{ marginLeft: '24px' }}
>
Reset to defaults
</Button>
</h2>
<table className="features wp-list-table striped table-view-list widefat">
<thead>
<tr>