From e9961dc5a59dc311a753e741c2c8659d1e7eb4e5 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 20 Sep 2021 13:30:59 +0000 Subject: [PATCH] [FIX] web: graph: keep pending filters when toggling a measure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consider the following scenario on a slow network in a graph view: - add a filter (triggers a reload) - toggle a measure (triggers another reload, before the first one completes) Before this commit, the new searchParams coming from the first reload were lost, even though they were still displayed in the search view. This commit ensures that we keep the last searchParams when reloading the graph view. closes odoo/odoo#76918 X-original-commit: 1719de73099eca0b765237cb47caf44813157b94 Related: odoo/enterprise#21046 Signed-off-by: Géry Debongnie (ged) Signed-off-by: Aaron Bohy (aab) --- .../web/static/src/views/graph/graph_model.js | 61 +++++++++++-------- .../static/tests/views/graph_view_tests.js | 4 +- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/addons/web/static/src/views/graph/graph_model.js b/addons/web/static/src/views/graph/graph_model.js index a784104520d..5fd8e1e745d 100644 --- a/addons/web/static/src/views/graph/graph_model.js +++ b/addons/web/static/src/views/graph/graph_model.js @@ -97,6 +97,7 @@ export class GraphModel extends Model { this.metaData = params; this.data = null; + this.searchParams = null; } //-------------------------------------------------------------------------- @@ -107,31 +108,11 @@ export class GraphModel extends Model { * @param {SearchParams} searchParams */ async load(searchParams) { - const { comparison, domain, context, groupBy } = searchParams; - const metaData = Object.assign({}, this.metaData, { context }); - if (comparison) { - metaData.domains = comparison.domains; - metaData.comparisonField = comparison.fieldName; - } else { - metaData.domains = [{ arrayRepr: domain, description: null }]; - } - metaData.measure = context.graph_measure || metaData.measure; - metaData.mode = context.graph_mode || metaData.mode; - + this.searchParams = searchParams; if (!this.initialGroupBy) { - this.initialGroupBy = context.graph_groupbys || this.metaData.groupBy; // = arch groupBy --> change that + this.initialGroupBy = searchParams.context.graph_groupbys || this.metaData.groupBy; // = arch groupBy --> change that } - metaData.groupBy = groupBy.length ? groupBy : this.initialGroupBy; - - this._normalize(metaData); - - metaData.measures = computeReportMeasures( - metaData.fields, - metaData.fieldAttrs, - [metaData.measure], - metaData.additionalMeasures - ); - + const metaData = this._buildMetaData(); return this._fetchDataPoints(metaData); } @@ -149,7 +130,8 @@ export class GraphModel extends Model { */ async updateMetaData(params) { if ("measure" in params) { - await this._fetchDataPoints(Object.assign({}, this.metaData, params)); + const metaData = this._buildMetaData(params); + await this._fetchDataPoints(metaData); } else { await this.race.getCurrentProm(); this.metaData = Object.assign({}, this.metaData, params); @@ -162,6 +144,37 @@ export class GraphModel extends Model { // Protected //-------------------------------------------------------------------------- + /** + * @protected + * @param {Object} [params={}] + * @returns {Object} + */ + _buildMetaData(params) { + const { comparison, domain, context, groupBy } = this.searchParams; + + const metaData = Object.assign({}, this.metaData, { context }); + if (comparison) { + metaData.domains = comparison.domains; + metaData.comparisonField = comparison.fieldName; + } else { + metaData.domains = [{ arrayRepr: domain, description: null }]; + } + metaData.measure = context.graph_measure || metaData.measure; + metaData.mode = context.graph_mode || metaData.mode; + metaData.groupBy = groupBy.length ? groupBy : this.initialGroupBy; + + this._normalize(metaData); + + metaData.measures = computeReportMeasures( + metaData.fields, + metaData.fieldAttrs, + [metaData.measure], + metaData.additionalMeasures + ); + + return Object.assign(metaData, params); + } + /** * Fetch the data points determined by the metaData. This function has * several side effects. It can alter this.metaData and set this.dataPoints. diff --git a/addons/web/static/tests/views/graph_view_tests.js b/addons/web/static/tests/views/graph_view_tests.js index 696b6ea6f38..76abb964ad3 100644 --- a/addons/web/static/tests/views/graph_view_tests.js +++ b/addons/web/static/tests/views/graph_view_tests.js @@ -3326,7 +3326,7 @@ QUnit.module("Views", (hooks) => { `, searchViewArch: ` - + `, mockRPC: function (route, args) { if (args.method === "web_read_group") { @@ -3363,7 +3363,7 @@ QUnit.module("Views", (hooks) => { await nextTick(); checkDatasets(assert, graph, ["data", "label"], { - data: [82, 157], + data: [82, 4], label: "Foo", }); }