[FIX] web: client action reload

This commit aims to solve two problems:
1. Keeping the hash if no action_id or menu_id is passed as a parameter.
2. Always force a reload of the page.

Description of the issues:
1. When reloading from an action, we just want to force a reload of the
page while staying on the same action. We only want to modify the hash
if we want to reload the page by opening another action.

2. When installing the website module, an error dialog is displayed and
the page is not reloaded because location.assign(...) only causes
a reload when the url has changed (path or search, it ignores the hash).
The error comes from trying to access a client action that is not yet
in the assets. It will be added when the page is reloaded.

Solution:
1. Modify the hash only when you have action_id or menu_id in the action params

2. We have thought of two solutions:
- Do a location.reload(...) when the url has not changed. The error
  appears during the reload time because location.assign(...) modifies
  the hash. The service action will then try to execute the client action
  which does not yet exist. (legacy solution)
- Always have a different url so that location.assign(...) causes a reload.
  So we decided to add/remove the reload key in the url search. This will
  always force a reload. We will opt for this solution because it avoids
  displaying a crash.

closes odoo/odoo#112077

Taskid: 3144132
X-original-commit: 77772c0e73d9f561fed63847175e2dbc9b809fa1
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2023-02-07 13:34:45 +01:00
parent c1f8c4abda
commit 97a641a750
2 changed files with 26 additions and 13 deletions
@@ -55,14 +55,24 @@ function reload(env, action) {
const { router } = env.services;
const route = { ...router.current };
route.hash = {};
if (menu_id) {
route.hash.menu_id = menu_id;
}
if (action_id) {
route.hash.action = action_id;
if (menu_id || action_id) {
route.hash = {};
if (menu_id) {
route.hash.menu_id = menu_id;
}
if (action_id) {
route.hash.action = action_id;
}
}
// We want to force location.assign(...) (in router.redirect(...)) to do a page reload.
// To do this, we need to make sure that the url is different.
route.search = { ...route.search };
if ("reload" in route.search) {
delete route.search.reload;
} else {
route.search.reload = true;
}
const url = browser.location.origin + routeToUrl(route);
env.bus.trigger("CLEAR-CACHES");
@@ -79,6 +89,7 @@ function home(env, action) {
const { wait } = action.params || {};
const url = "/" + (browser.location.search || "");
env.services.router.redirect(url, wait);
browser.location.reload(url);
}
registry.category("actions").add("home", home);
@@ -612,15 +612,17 @@ QUnit.module("ActionManager", (hooks) => {
});
QUnit.test("test reload client action", async function (assert) {
patchWithCleanup(browser.location, {
origin: "",
hash: "#test=42",
});
const webClient = await createWebClient({ serverData });
patchWithCleanup(webClient.env.services.router, {
redirect: (url) => {
assert.step(url);
},
});
patchWithCleanup(browser.location, {
origin: "",
});
await doAction(webClient, {
type: "ir.actions.client",
@@ -649,10 +651,10 @@ QUnit.module("ActionManager", (hooks) => {
},
});
assert.verifySteps([
"/web/tests",
"/web/tests#action=2",
"/web/tests#menu_id=1",
"/web/tests#menu_id=2&action=1",
"/web/tests?reload=true#test=42",
"/web/tests?reload=true#action=2",
"/web/tests?reload=true#menu_id=1",
"/web/tests?reload=true#menu_id=2&action=1",
]);
});
});