Merge pull request #45 from woocommerce/update/43-update-experiments-ttl

Update experiment cache time
This commit is contained in:
Moon 2022-05-12 14:10:10 -07:00 committed by GitHub
commit bf91037aaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 15 deletions

View File

@ -7,44 +7,52 @@ import { apiFetch } from '@wordpress/data-controls';
* Internal dependencies
*/
import TYPES from './action-types';
import { EXPERIMENT_NAME_PREFIX, TRANSIENT_NAME_PREFIX } from './constants';
import {
EXPERIMENT_NAME_PREFIX,
TRANSIENT_NAME_PREFIX,
TRANSIENT_TIMEOUT_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;
storageItem.ttl = 3600;
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;
payload[ TRANSIENT_TIMEOUT_NAME_PREFIX + experimentName ] =
Math.round( Date.now() / 1000 ) + 3600;
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 +63,7 @@ export function* toggleExperiment(experimentName, currentVariation, source) {
};
}
export function setExperiments(experiments) {
export function setExperiments( experiments ) {
return {
type: TYPES.SET_EXPERIMENTS,
experiments,

View File

@ -1,4 +1,6 @@
export const STORE_KEY = 'wc-admin-helper/experiments';
export const EXPERIMENT_NAME_PREFIX = 'explat-experiment--';
export const TRANSIENT_NAME_PREFIX = '_transient_abtest_variation_';
export const TRANSIENT_TIMEOUT_NAME_PREFIX =
'_transient_timeout_abtest_variation';
export const API_NAMESPACE = '/wc-admin-test-helper';