From 9eaf714470fa584fe67cdec909d9a0117c218148 Mon Sep 17 00:00:00 2001
From: Angelina Filippova <linakirsanova@gmail.com>
Date: Mon, 20 Jan 2020 22:37:13 +0700
Subject: [PATCH] Fix processing nested values

---
 src/store/modules/normalizers.js | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/store/modules/normalizers.js b/src/store/modules/normalizers.js
index 69e335d2..796e0e09 100644
--- a/src/store/modules/normalizers.js
+++ b/src/store/modules/normalizers.js
@@ -22,12 +22,22 @@ export const checkPartialUpdate = (settings, updatedSettings, description) => {
   }, {})
 }
 
-const getCurrentValue = (object, keys) => {
-  if (keys.length === 0) {
-    return object
+const getCurrentValue = (type, value, path) => {
+  if (type === 'state') {
+    return _.get(value, path)
+  } else {
+    const [firstSettingName, ...restKeys] = path
+    const firstSegment = value[firstSettingName]
+    if (restKeys.length === 0 || !firstSegment) {
+      return firstSegment || false
+    } else {
+      const secondSegment = (value, keys) => {
+        const [element, ...rest] = keys
+        return keys.length === 0 ? value : secondSegment(value[1][element], rest)
+      }
+      return secondSegment(firstSegment, restKeys)
+    }
   }
-  const [currentKey, ...restKeys] = keys
-  return getCurrentValue(object[currentKey], restKeys)
 }
 
 const getValueWithoutKey = (key, [type, value]) => {
@@ -139,11 +149,11 @@ export const processNested = (valueForState, valueForUpdatedSettings, group, par
   const path = [group, parentKey, ...parents.reverse().map(parent => parent.key).slice(0, -1)]
 
   let updatedValueForState = valueExists('state', settings, path)
-    ? { ...getCurrentValue(settings[group][parentKey], parents.map(el => el.key).slice(0, -1)),
+    ? { ...getCurrentValue('state', settings[group][parentKey], parents.map(el => el.key).slice(0, -1)),
       ...{ [key]: valueForState }}
     : { [key]: valueForState }
   let updatedValueForUpdatedSettings = valueExists('updatedSettings', updatedSettings, path)
-    ? { ...getCurrentValue(updatedSettings[group][parentKey], parents.map(el => el.key).slice(0, -1))[1],
+    ? { ...getCurrentValue('updatedSettings', updatedSettings[group][parentKey], parents.map(el => el.key).slice(0, -1))[1],
       ...{ [key]: [type, valueForUpdatedSettings] }}
     : { [key]: [type, valueForUpdatedSettings] }
 
@@ -178,7 +188,7 @@ const valueExists = (type, value, path) => {
         if (keys.length === 0) {
           return true
         }
-        const [element, ...rest] = path
+        const [element, ...rest] = keys
         return value[1][element] ? secondSegment(value[1][element], rest) : false
       }
       return secondSegment(firstSegment, restKeys)
-- 
GitLab