Commit b04374d5 authored by Denys Mishunov's avatar Denys Mishunov Committed by Natalia Tepluhina

Refactor Snippets application to update Apollo cache in immutable way

parent b5edb1c7
...@@ -29,15 +29,6 @@ export default { ...@@ -29,15 +29,6 @@ export default {
update(data) { update(data) {
return this.onContentUpdate(data); return this.onContentUpdate(data);
}, },
result() {
if (this.activeViewerType === RICH_BLOB_VIEWER) {
// eslint-disable-next-line vue/no-mutating-props
this.blob.richViewer.renderError = null;
} else {
// eslint-disable-next-line vue/no-mutating-props
this.blob.simpleViewer.renderError = null;
}
},
skip() { skip() {
return this.viewer.renderError; return this.viewer.renderError;
}, },
......
...@@ -14,7 +14,13 @@ export default function appFactory(el, Component) { ...@@ -14,7 +14,13 @@ export default function appFactory(el, Component) {
} }
const apolloProvider = new VueApollo({ const apolloProvider = new VueApollo({
defaultClient: createDefaultClient({}, { batchMax: 1 }), defaultClient: createDefaultClient(
{},
{
batchMax: 1,
assumeImmutableResults: true,
},
),
}); });
const { const {
......
import { isEmpty } from 'lodash';
import GetSnippetQuery from 'shared_queries/snippet/snippet.query.graphql'; import GetSnippetQuery from 'shared_queries/snippet/snippet.query.graphql';
const blobsDefault = []; const blobsDefault = [];
...@@ -12,20 +13,18 @@ export const getSnippetMixin = { ...@@ -12,20 +13,18 @@ export const getSnippetMixin = {
}; };
}, },
update(data) { update(data) {
const res = data.snippets.nodes[0]; const res = { ...data.snippets.nodes[0] };
// Set `snippet.blobs` since some child components are coupled to this. // Set `snippet.blobs` since some child components are coupled to this.
if (res) { if (!isEmpty(res)) {
// It's possible for us to not get any blobs in a response. // It's possible for us to not get any blobs in a response.
// In this case, we should default to current blobs. // In this case, we should default to current blobs.
res.blobs = res.blobs ? res.blobs.nodes : this.blobs; res.blobs = res.blobs ? res.blobs.nodes : blobsDefault;
res.description = res.description || '';
} }
return res; return res;
}, },
result(res) {
this.blobs = res.data.snippets.nodes[0]?.blobs || blobsDefault;
},
skip() { skip() {
return this.newSnippet; return this.newSnippet;
}, },
...@@ -41,12 +40,14 @@ export const getSnippetMixin = { ...@@ -41,12 +40,14 @@ export const getSnippetMixin = {
return { return {
snippet: {}, snippet: {},
newSnippet: !this.snippetGid, newSnippet: !this.snippetGid,
blobs: blobsDefault,
}; };
}, },
computed: { computed: {
isLoading() { isLoading() {
return this.$apollo.queries.snippet.loading; return this.$apollo.queries.snippet.loading;
}, },
blobs() {
return this.snippet?.blobs || [];
},
}, },
}; };
...@@ -71,7 +71,9 @@ describe('Snippet view app', () => { ...@@ -71,7 +71,9 @@ describe('Snippet view app', () => {
it('renders correct snippet-blob components', () => { it('renders correct snippet-blob components', () => {
createComponent({ createComponent({
data: { data: {
blobs: [Blob, BinaryBlob], snippet: {
blobs: [Blob, BinaryBlob],
},
}, },
}); });
const blobs = wrapper.findAll(SnippetBlob); const blobs = wrapper.findAll(SnippetBlob);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment