From d11cee3474eeb331f8326da81a13229d739a5031 Mon Sep 17 00:00:00 2001 From: Kartik Chavda Date: Tue, 9 May 2023 05:41:29 +0000 Subject: [PATCH] [FIX] web: add auto save option for state and priority field Before this commit state selection and priority both field are saved we change its value even though we are not saving records and because of that changing state or priority in sub-task or block-by task and discarding changes does not set them to their previous state and it'll create new record without saving if we change one of those field. This commit add 'auto_save' option to prevent auto save on state and priority from sub-task and block-by task page. task-3282848 Part-of: odoo/odoo#120925 --- .../views/fields/priority/priority_field.js | 8 +++-- .../state_selection/state_selection_field.js | 6 +++- .../views/fields/priority_field_tests.js | 28 ++++++++++++++++++ .../fields/state_selection_field_tests.js | 29 +++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/views/fields/priority/priority_field.js b/addons/web/static/src/views/fields/priority/priority_field.js index 71d67a79260..10798cf6e39 100644 --- a/addons/web/static/src/views/fields/priority/priority_field.js +++ b/addons/web/static/src/views/fields/priority/priority_field.js @@ -12,6 +12,7 @@ export class PriorityField extends Component { static props = { ...standardFieldProps, withCommand: { type: Boolean, optional: true }, + autosave: { type: Boolean, optional: true }, }; setup() { @@ -82,7 +83,9 @@ export class PriorityField extends Component { async updateRecord(value) { await this.props.record.update({ [this.props.name]: value }); - return this.props.record.save(); + if (this.props.autosave) { + return this.props.record.save(); + } } } @@ -90,10 +93,11 @@ export const priorityField = { component: PriorityField, displayName: _lt("Priority"), supportedTypes: ["selection"], - extractProps({ viewType }, dynamicInfo) { + extractProps({ options, viewType }, dynamicInfo) { return { withCommand: viewType === "form", readonly: dynamicInfo.readonly, + autosave: "autosave" in options ? !!options.autosave : true, }; }, }; diff --git a/addons/web/static/src/views/fields/state_selection/state_selection_field.js b/addons/web/static/src/views/fields/state_selection/state_selection_field.js index ed31d19763c..1e27ed34830 100644 --- a/addons/web/static/src/views/fields/state_selection/state_selection_field.js +++ b/addons/web/static/src/views/fields/state_selection/state_selection_field.js @@ -20,6 +20,7 @@ export class StateSelectionField extends Component { ...standardFieldProps, showLabel: { type: Boolean, optional: true }, withCommand: { type: Boolean, optional: true }, + autosave: { type: Boolean, optional: true }, }; static defaultProps = { showLabel: true, @@ -72,7 +73,9 @@ export class StateSelectionField extends Component { async updateRecord(value) { await this.props.record.update({ [this.props.name]: value }); - return this.props.record.save(); + if (this.props.autosave) { + return this.props.record.save(); + } } } @@ -85,6 +88,7 @@ export const stateSelectionField = { showLabel: viewType === "list" && !options.hide_label, withCommand: viewType === "form", readonly: dynamicInfo.readonly, + autosave: "autosave" in options ? !!options.autosave : true, }; }, }; diff --git a/addons/web/static/tests/views/fields/priority_field_tests.js b/addons/web/static/tests/views/fields/priority_field_tests.js index b1586067b47..07634f61447 100644 --- a/addons/web/static/tests/views/fields/priority_field_tests.js +++ b/addons/web/static/tests/views/fields/priority_field_tests.js @@ -639,4 +639,32 @@ QUnit.module("Fields", (hooks) => { await click(stars[stars.length - 1]); assert.verifySteps(["write"]); }); + + QUnit.test("PriorityField - prevent auto save with autosave option", async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + resId: 1, + serverData, + arch: ` +
+ + + + + +
`, + mockRPC(_route, { method }) { + if (method === "write") { + assert.step("write"); + } + }, + }); + + const stars = target.querySelectorAll( + ".o_field_widget .o_priority a.o_priority_star.fa-star-o" + ); + await click(stars[stars.length - 1]); + assert.verifySteps([]); + }); }); diff --git a/addons/web/static/tests/views/fields/state_selection_field_tests.js b/addons/web/static/tests/views/fields/state_selection_field_tests.js index 06455545e6d..b23a023e160 100644 --- a/addons/web/static/tests/views/fields/state_selection_field_tests.js +++ b/addons/web/static/tests/views/fields/state_selection_field_tests.js @@ -570,4 +570,33 @@ QUnit.module("Fields", (hooks) => { assert.verifySteps(["write"]); } ); + + QUnit.test( + "StateSelectionField - prevent auto save with autosave option", + async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + + + + +
`, + resId: 1, + mockRPC(_route, { method }) { + if (method === "write") { + assert.step("write"); + } + }, + }); + + await click(target, ".o_field_widget.o_field_state_selection .o_status"); + await click(target, ".dropdown-menu .dropdown-item:last-child"); + assert.verifySteps([]); + } + ); });