[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) <aab@odoo.com>
Signed-off-by: Piryns Victor (pivi) <pivi@odoo.com>
This commit is contained in:
Victor Piryns (pivi)
2023-07-03 07:30:40 +02:00
parent d70da05100
commit 2a02fbf8b0
2 changed files with 8 additions and 3 deletions
@@ -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 });
@@ -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"]);
}
);