From 174e9e09e8539662df445b2c549c15286e72a840 Mon Sep 17 00:00:00 2001 From: Moon Date: Tue, 22 Feb 2022 10:27:42 -0800 Subject: [PATCH] Code clean --- src/experiments/data/reducer.js | 15 +++++++-------- src/experiments/data/resolvers.js | 16 ++++------------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/experiments/data/reducer.js b/src/experiments/data/reducer.js index 982ea63a256..fd6632d6bda 100644 --- a/src/experiments/data/reducer.js +++ b/src/experiments/data/reducer.js @@ -10,16 +10,15 @@ const DEFAULT_STATE = { const reducer = (state = DEFAULT_STATE, action) => { switch (action.type) { case TYPES.TOGGLE_EXPERIMENT: - let experiments = [...state.experiments]; - experiments = experiments.map((experiment) => { - if (experiment.name === action.experimentName) { - experiment.variation = action.newVariation; - } - return experiment; - }); return { ...state, - experiments, + experiments: state.experiments.map((experiment) => ({ + ...experiment, + variation: + experiment.name === action.experimentName + ? action.newVariation + : experiment.variation, + })), }; case TYPES.SET_EXPERIMENTS: return { diff --git a/src/experiments/data/resolvers.js b/src/experiments/data/resolvers.js index d2acdb5458c..99531b3a148 100644 --- a/src/experiments/data/resolvers.js +++ b/src/experiments/data/resolvers.js @@ -7,25 +7,17 @@ import { EXPERIMENT_NAME_PREFIX } from './constants'; export function* getExperiments() { const storageItems = Object.entries({ ...window.localStorage }).filter( (item) => { - if (item[0].indexOf(EXPERIMENT_NAME_PREFIX) === 0) { - return true; - } - return false; + return item[0].indexOf(EXPERIMENT_NAME_PREFIX) === 0; } ); - const experiments = []; - storageItems.forEach((storageItem) => { + const experiments = storageItems.map((storageItem) => { const [key, value] = storageItem; const objectValue = JSON.parse(value); - - const experiment = { + return { name: key.replace(EXPERIMENT_NAME_PREFIX, ''), - variation: objectValue.variationName - ? objectValue.variationName - : 'control', + variation: objectValue.variationName || 'control', }; - experiments.push(experiment); }); yield setExperiments(experiments);