Create function that wraps updated settings to send it to BE

This commit is contained in:
Angelina Filippova 2019-12-04 01:19:46 +09:00
parent 492828b856
commit bb2a78ae52
2 changed files with 22 additions and 8 deletions

View file

@ -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]

View file

@ -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)
},