Move checking partial update to normalizers and fix bugs

This commit is contained in:
Angelina Filippova 2020-01-08 14:38:56 +07:00
parent edc8fe58d7
commit 1b368c7786
2 changed files with 27 additions and 20 deletions

View file

@ -1,3 +1,26 @@
export const checkPartialUpdate = (settings, updatedSettings, description) => {
return Object.keys(updatedSettings).reduce((acc, group) => {
acc[group] = Object.keys(updatedSettings[group]).reduce((acc, key) => {
debugger
if (!partialUpdate(group, key)) {
const updated = Object.keys(settings[group][key]).reduce((acc, settingName) => {
const setting = description
.find(element => element.group === group && element.key === key).children
.find(child => child.key === settingName)
const type = setting ? setting.type : ''
acc[settingName] = [type, settings[group][key][settingName]]
return acc
}, {})
acc[key] = updated
return acc
}
acc[key] = updatedSettings[group][key]
return acc
}, {})
return acc
}, {})
}
const getCurrentValue = (object, keys) => {
if (keys.length === 0) {
return object
@ -103,7 +126,7 @@ const parseProxyUrl = value => {
return { socks5: false, host: null, port: null }
}
export const partialUpdate = (group, key) => {
const partialUpdate = (group, key) => {
if (group === ':auto_linker' && key === ':opts') {
return false
}

View file

@ -1,5 +1,5 @@
import { fetchDescription, fetchSettings, removeSettings, updateSettings, uploadMedia } from '@/api/settings'
import { parseNonTuples, parseTuples, partialUpdate, valueHasTuples, wrapUpdatedSettings } from './normalizers'
import { checkPartialUpdate, parseNonTuples, parseTuples, valueHasTuples, wrapUpdatedSettings } from './normalizers'
const settings = {
state: {
@ -70,27 +70,11 @@ const settings = {
await removeSettings(configs, getters.authHost, getters.token)
},
async SubmitChanges({ getters, commit, state }) {
const updatedData = Object.keys(state.updatedSettings).reduce((acc, group) => {
acc[group] = Object.keys(state.updatedSettings[group]).reduce((acc, key) => {
if (!partialUpdate(group, key)) {
const updated = Object.keys(state.settings[group][key]).reduce((acc, settingName) => {
const settingType = state.description
.find(element => element.group === group && element.key === key).children
.find(child => child.key === settingName).type
acc[settingName] = [settingType, state.settings[group][key][settingName]]
return acc
}, {})
acc[key] = updated
return acc
}
acc[key] = state.updatedSettings[group][key]
return acc
}, {})
return acc
}, {})
const updatedData = checkPartialUpdate(state.settings, state.updatedSettings, state.description)
const configs = Object.keys(updatedData).reduce((acc, group) => {
return [...acc, ...wrapUpdatedSettings(group, updatedData[group], state.settings)]
}, [])
const response = await updateSettings(configs, getters.authHost, getters.token)
commit('SET_SETTINGS', response.data.configs)
commit('CLEAR_UPDATED_SETTINGS')