Skip to content
Snippets Groups Projects
Commit 9eaf7144 authored by Angelina Filippova's avatar Angelina Filippova
Browse files

Fix processing nested values

parent d1688819
No related branches found
No related tags found
1 merge request!77Ability to remove settings from db
This commit is part of merge request !77. Comments created here will be created in the context of that merge request.
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment