From 2a02fbf8b0a635617c80dd306bdb94e1ee8bd29a Mon Sep 17 00:00:00 2001 From: "Victor Piryns (pivi)" Date: Thu, 15 Jun 2023 11:49:25 +0000 Subject: [PATCH] [FIX] web: save all changes in many2many modal Current behaviour: When creating a new record with a many2many widget, in the modal window, any modification done after record creation (for example clicking on the status bar buttons) aren't being saved once we "Save & Close" the modal window. Expected behaviour: All changes should be saved, regardless if we clicked the status button that trigger *some* action. Steps to reproduce: - Install Project - Settings > Sub-tasks or Task Dependencies - In a task on a project, add a subtask > New - Set the title to "Title 1" > Change the stage > Set the title to "Title 2" > Save & Close - Observe the title of the task is "Title 1", not "Title 2". Reason for the problem: When we click on a status button, since it can trigger *some* action in the backend, it forces the creation of the record front-end side, which generates a `resId`. Upon saving & closing the modal window, the record is not saved, only it's resId is appended to the list of `many2many` records. Fix: Before appending the record to the list, we save the record if it's dirty. Affected versions: - 16.0 - saas-16.1 - saas-16.2 - saas-16.3 - master opw-3289908 closes odoo/odoo#127009 X-original-commit: 29aeafda0f75764ec5b30147fec422b47a4da164 Signed-off-by: Aaron Bohy (aab) Signed-off-by: Piryns Victor (pivi) --- addons/web/static/src/views/fields/relational_utils.js | 5 ++++- .../web/static/tests/views/fields/many2many_field_tests.js | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/views/fields/relational_utils.js b/addons/web/static/src/views/fields/relational_utils.js index 9e6d855dc9a..2a41ca35c98 100644 --- a/addons/web/static/src/views/fields/relational_utils.js +++ b/addons/web/static/src/views/fields/relational_utils.js @@ -752,13 +752,16 @@ export function useOpenX2ManyRecord({ export function useX2ManyCrud(getList, isMany2Many) { let saveRecord; if (isMany2Many) { - saveRecord = (object) => { + saveRecord = async (object) => { const list = getList(); const currentIds = list.currentIds; let resIds; if (Array.isArray(object)) { resIds = [...currentIds, ...object]; } else if (object.resId) { + if (object.isDirty) { + await object.save(); + } resIds = [...currentIds, object.resId]; } else { return list.add(object, { isM2M: isMany2Many }); diff --git a/addons/web/static/tests/views/fields/many2many_field_tests.js b/addons/web/static/tests/views/fields/many2many_field_tests.js index 3f77e695226..0c4a911a810 100644 --- a/addons/web/static/tests/views/fields/many2many_field_tests.js +++ b/addons/web/static/tests/views/fields/many2many_field_tests.js @@ -740,16 +740,18 @@ QUnit.module("Fields", (hooks) => { ["Save & Close", "Save & New", "Discard"] ); + await editInput(modal, "[name='display_name'] input", "Hello (edited)"); + await click(modal.querySelector(".modal-footer button")); assert.containsNone(target, ".modal"); assert.deepEqual( [...target.querySelectorAll("[name='timmy'] .o_data_row")].map( (row) => row.textContent ), - ["Hello"] + ["Hello (edited)"] ); - assert.verifySteps(["create", "read", "action: myaction", "read"]); + assert.verifySteps(["create", "read", "action: myaction", "write", "read", "read"]); } );