Fully implement mutation

This commit is contained in:
Chi Vinh Le
2017-11-20 19:48:12 +01:00
parent 873d1915c3
commit 2d6143af62
7 changed files with 65 additions and 41 deletions
+1 -11
View File
@@ -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: {
@@ -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;
}
}
}),
},
};
@@ -379,6 +379,12 @@ export const withUpdateAssetSettings = withMutation(
id,
input,
},
optimisticResponse: {
updateAssetStatus: {
__typename: 'UpdateAssetSettingsResponse',
errors: null,
}
},
});
}}),
});
+11 -1
View File
@@ -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);
});
}
+3 -3
View File
@@ -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
});
+1 -26
View File
@@ -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.
+30
View File
@@ -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,
};