From bb2a78ae52ca151ce7d2eaa807e26097ef9f5176 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Wed, 4 Dec 2019 01:19:46 +0900 Subject: [PATCH] Create function that wraps updated settings to send it to BE --- src/store/modules/normalizers.js | 24 ++++++++++++++++++------ src/store/modules/settings.js | 6 ++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/store/modules/normalizers.js b/src/store/modules/normalizers.js index c52d5c81..f051b163 100644 --- a/src/store/modules/normalizers.js +++ b/src/store/modules/normalizers.js @@ -56,12 +56,6 @@ const parseObject = (object) => { }, {}) } -export const parseValue = (input, value, type) => { - if (type === 'string' || type === 'boolean' || type === 'integer') { - return [{ tuple: [input, value] }] - } -} - export const valueHasTuples = (key, value) => { const valueIsArrayOfNonObjects = Array.isArray(value) && value.length > 0 && typeof value[0] !== 'object' return key === ':meta' || @@ -75,6 +69,24 @@ export const valueHasTuples = (key, value) => { valueIsArrayOfNonObjects } +export const wrapUpdatedSettings = (group, settings) => { + return Object.keys(settings).map((key) => { + const value = wrapValues(settings[key]) + return { group, key, value } + }) +} + +const wrapValues = settings => { + return Object.keys(settings).map(setting => { + const [type, value] = settings[setting] + if (type === 'keyword') { + return { 'tuple': [setting, wrapValues(setting, value)] } + } else { + return { 'tuple': [setting, value] } + } + }) +} + const wrapNestedTuples = setting => { return Object.keys(setting).reduce((acc, settingName) => { const data = setting[settingName] diff --git a/src/store/modules/settings.js b/src/store/modules/settings.js index 838bf1fd..0c42d807 100644 --- a/src/store/modules/settings.js +++ b/src/store/modules/settings.js @@ -1,5 +1,5 @@ import { fetchDescription, fetchSettings, migrateToDB, updateSettings, uploadMedia } from '@/api/settings' -import { parseTuples, valueHasTuples } from './normalizers' +import { parseTuples, valueHasTuples, wrapUpdatedSettings } from './normalizers' const settings = { state: { @@ -72,7 +72,9 @@ const settings = { commit('REWRITE_CONFIG', { tab, data }) }, async SubmitChanges({ getters, commit, state }) { - const configs = state.updatedSettings + const configs = Object.keys(state.updatedSettings).reduce((acc, group) => { + return [...acc, ...wrapUpdatedSettings(group, state.updatedSettings[group])] + }, []) const response = await updateSettings(configs, getters.authHost, getters.token) commit('SET_SETTINGS', response.data.configs) },