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 TYPES from './action-types';
import { EXPERIMENT_NAME_PREFIX, TRANSIENT_NAME_PREFIX } from './constants'; import { EXPERIMENT_NAME_PREFIX, TRANSIENT_NAME_PREFIX } from './constants';
function toggleFrontendExperiment( experimentName, newVariation ) { function toggleFrontendExperiment(experimentName, newVariation) {
const storageItem = JSON.parse( const storageItem = JSON.parse(
window.localStorage.getItem( EXPERIMENT_NAME_PREFIX + experimentName ) window.localStorage.getItem(EXPERIMENT_NAME_PREFIX + experimentName)
); );
storageItem.variationName = newVariation; storageItem.variationName = newVariation;
window.localStorage.setItem( window.localStorage.setItem(
EXPERIMENT_NAME_PREFIX + experimentName, EXPERIMENT_NAME_PREFIX + experimentName,
JSON.stringify( storageItem ) JSON.stringify(storageItem)
); );
} }
function* toggleBackendExperiment( experimentName, newVariation ) { function* toggleBackendExperiment(experimentName, newVariation) {
try { try {
const payload = {}; const payload = {};
payload[ TRANSIENT_NAME_PREFIX + experimentName ] = newVariation; payload[TRANSIENT_NAME_PREFIX + experimentName] = newVariation;
yield apiFetch( { yield apiFetch({
method: 'POST', method: 'POST',
path: '/wc-admin/options', path: '/wc-admin/options',
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
body: JSON.stringify( payload ), body: JSON.stringify(payload),
} ); });
} catch ( error ) { } catch (error) {
throw new Error(); throw new Error();
} }
} }
export function* toggleExperiment( experimentName, currentVariation, source ) { export function* toggleExperiment(experimentName, currentVariation, source) {
const newVariation = const newVariation =
currentVariation === 'control' ? 'treatment' : 'control'; currentVariation === 'control' ? 'treatment' : 'control';
if ( source === 'frontend' ) { if (source === 'frontend') {
toggleFrontendExperiment( experimentName, newVariation ); toggleFrontendExperiment(experimentName, newVariation);
} else { } else {
yield toggleBackendExperiment( experimentName, newVariation ); yield toggleBackendExperiment(experimentName, newVariation);
} }
return { return {
@ -55,7 +55,7 @@ export function* toggleExperiment( experimentName, currentVariation, source ) {
}; };
} }
export function setExperiments( experiments ) { export function setExperiments(experiments) {
return { return {
type: TYPES.SET_EXPERIMENTS, type: TYPES.SET_EXPERIMENTS,
experiments, experiments,

View File

@ -2,12 +2,13 @@
* External dependencies * External dependencies
*/ */
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import TYPES from './action-types'; import TYPES from './action-types';
import { API_NAMESPACE } from './constants'; import { API_NAMESPACE, STORE_KEY } from './constants';
export function* resetModifiedFeatures() { export function* resetModifiedFeatures() {
try { try {
@ -30,7 +31,12 @@ export function* toggleFeature(featureName) {
path: API_NAMESPACE + '/features/' + featureName + '/toggle', path: API_NAMESPACE + '/features/' + featureName + '/toggle',
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
}); });
return yield setFeatures(response); yield setFeatures(response);
yield controls.dispatch(
STORE_KEY,
'invalidateResolutionForStoreSelector',
'getModifiedFeatures'
);
} catch (error) { } catch (error) {
throw new Error(); throw new Error();
} }

View File

@ -12,10 +12,14 @@ import { API_NAMESPACE, OPTION_NAME_PREFIX } from './constants';
export function* getModifiedFeatures() { export function* getModifiedFeatures() {
try { try {
const response = yield apiFetch({ 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) { } catch (error) {
throw new Error(); throw new Error();
} }

View File

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