From 2d6143af625ed4027332287613d83eaccc07c593 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Mon, 20 Nov 2017 19:48:12 +0100 Subject: [PATCH] Fully implement mutation --- client/coral-admin/src/graphql/index.js | 12 +------- .../coral-embed-stream/src/graphql/index.js | 13 ++++++++ client/coral-framework/graphql/mutations.js | 6 ++++ client/coral-framework/utils/index.js | 12 +++++++- services/assets.js | 6 ++-- services/settings.js | 27 +---------------- services/utils.js | 30 +++++++++++++++++++ 7 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 services/utils.js diff --git a/client/coral-admin/src/graphql/index.js b/client/coral-admin/src/graphql/index.js index d7656d9a5..42e96f382 100644 --- a/client/coral-admin/src/graphql/index.js +++ b/client/coral-admin/src/graphql/index.js @@ -1,15 +1,5 @@ import update from 'immutability-helper'; -import mapValues from 'lodash/mapValues'; - -// Map nested object leaves. Array objects are considered leaves. -function mapLeaves(o, mapper) { - return mapValues(o, (val) => { - if (typeof val === 'object' && !Array.isArray(val)) { - return mapLeaves(val, mapper); - } - return mapper(val); - }); -} +import {mapLeaves} from 'coral-framework/utils'; export default { mutations: { diff --git a/client/coral-embed-stream/src/graphql/index.js b/client/coral-embed-stream/src/graphql/index.js index d18cdc435..d77e6356a 100644 --- a/client/coral-embed-stream/src/graphql/index.js +++ b/client/coral-embed-stream/src/graphql/index.js @@ -2,6 +2,7 @@ import {gql} from 'react-apollo'; import update from 'immutability-helper'; import uuid from 'uuid/v4'; import {insertCommentIntoEmbedQuery, removeCommentFromEmbedQuery} from './utils'; +import {mapLeaves} from 'coral-framework/utils'; export default { fragments: { @@ -218,6 +219,18 @@ export default { }, }, }), + UpdateAssetSettings: ({variables: {input}}) => ({ + updateQueries: { + CoralEmbedStream_Embed: (prev) => { + const updated = update(prev, { + asset: { + settings: mapLeaves(input, (leaf) => ({$set: leaf})), + }, + }); + return updated; + } + } + }), }, }; diff --git a/client/coral-framework/graphql/mutations.js b/client/coral-framework/graphql/mutations.js index ed4a6f782..2aa526a54 100644 --- a/client/coral-framework/graphql/mutations.js +++ b/client/coral-framework/graphql/mutations.js @@ -379,6 +379,12 @@ export const withUpdateAssetSettings = withMutation( id, input, }, + optimisticResponse: { + updateAssetStatus: { + __typename: 'UpdateAssetSettingsResponse', + errors: null, + } + }, }); }}), }); diff --git a/client/coral-framework/utils/index.js b/client/coral-framework/utils/index.js index 9f71b0913..c7225f492 100644 --- a/client/coral-framework/utils/index.js +++ b/client/coral-framework/utils/index.js @@ -3,7 +3,8 @@ import t from 'coral-framework/services/i18n'; import union from 'lodash/union'; import {capitalize} from 'coral-framework/helpers/strings'; import assignWith from 'lodash/assignWith'; -export * from 'coral-framework/helpers/strings'; +import mapValues from 'lodash/mapValues'; +export * from 'coral-framework/helpers/strings' export const getTotalActionCount = (type, comment) => { return comment.action_summaries @@ -207,3 +208,12 @@ export function mergeExcludingArrays(objValue, srcValue) { return srcValue; } +// Map nested object leaves. Array objects are considered leaves. +export function mapLeaves(o, mapper) { + return mapValues(o, (val) => { + if (typeof val === 'object' && !Array.isArray(val)) { + return mapLeaves(val, mapper); + } + return mapper(val); + }); +} diff --git a/services/assets.js b/services/assets.js index 4aac2addd..3ab7ec9ff 100644 --- a/services/assets.js +++ b/services/assets.js @@ -4,6 +4,7 @@ const SettingsService = require('./settings'); const domainlist = require('./domainlist'); const errors = require('../errors'); const merge = require('lodash/merge'); +const {dotize} = require('./utils'); module.exports = class AssetsService { @@ -99,10 +100,9 @@ module.exports = class AssetsService { * @return {[type]} [description] */ static overrideSettings(id, settings) { + console.log(settings, dotize({settings})); return AssetModel.findOneAndUpdate({id}, { - $set: { - settings - } + $set: dotize({settings}) }, { new: true }); diff --git a/services/settings.js b/services/settings.js index b11b86d91..310884002 100644 --- a/services/settings.js +++ b/services/settings.js @@ -1,31 +1,6 @@ const SettingModel = require('../models/setting'); const errors = require('../errors'); - -function dotizeRecurse(result, object, path = '') { - for (const key in object) { - const newPath = path ? `${path}.${key}` : key; - if (typeof object[key] === 'object' && !Array.isArray(object[key])) { - dotizeRecurse(result, object[key], newPath); - continue; - } - result[newPath] = object[key]; - } -} - -/** - * Dotize turns a nested object into flattened object with - * dotized key notation. Arrays do not become dotized. - * - * e.g. {a: {b: 'c'}} becomes {'a.b': 'c} - * - * @param {Object} object - * @return {Object} dotized object - */ -function dotize(object) { - const result = {}; - dotizeRecurse(result, object); - return result; -} +const {dotize} = require('./utils'); /** * The selector used to uniquely identify the settings document. diff --git a/services/utils.js b/services/utils.js new file mode 100644 index 000000000..0247e2889 --- /dev/null +++ b/services/utils.js @@ -0,0 +1,30 @@ + +function dotizeRecurse(result, object, path = '') { + for (const key in object) { + const newPath = path ? `${path}.${key}` : key; + if (typeof object[key] === 'object' && !Array.isArray(object[key])) { + dotizeRecurse(result, object[key], newPath); + continue; + } + result[newPath] = object[key]; + } +} + +/** + * Dotize turns a nested object into flattened object with + * dotized key notation. Arrays do not become dotized. + * + * e.g. {a: {b: 'c'}} becomes {'a.b': 'c} + * + * @param {Object} object + * @return {Object} dotized object + */ +function dotize(object) { + const result = {}; + dotizeRecurse(result, object); + return result; +} + +module.exports = { + dotize, +};