Skip to content

Commit

Permalink
[FIX] web: WithSearch: filtering search defaults
Browse files Browse the repository at this point in the history
Before that commit, the search defaults would not be filtered when the
WithSearch is started with a search model state or updated by its parent.

closes #159406

X-original-commit: dac527b
Signed-off-by: Francois Georis (fge) <fge@odoo.com>
Signed-off-by: Mathieu Duckerts-Antoine (dam) <dam@odoo.com>
  • Loading branch information
Polymorphe57 committed Mar 27, 2024
1 parent 03743a7 commit 7ac5dcc
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 20 deletions.
47 changes: 27 additions & 20 deletions addons/web/static/src/search/search_model.js
Expand Up @@ -266,6 +266,9 @@ export class SearchModel extends EventBus {
this.searchViewId = searchViewDescription.viewId;
}

const { searchDefaults, searchPanelDefaults } =
this._extractSearchDefaultsFromGlobalContext();

if (config.state) {
this._importState(config.state);
this.__legacyParseSearchPanelArchAnyway(searchViewDescription, searchViewFields);
Expand All @@ -285,26 +288,6 @@ export class SearchModel extends EventBus {
this.nextGroupId = 1;
this.nextGroupNumber = 1;

const searchDefaults = {};
const searchPanelDefaults = {};

for (const key in this.globalContext) {
const defaultValue = this.globalContext[key];
const searchDefaultMatch = /^search_default_(.*)$/.exec(key);
if (searchDefaultMatch) {
if (defaultValue) {
searchDefaults[searchDefaultMatch[1]] = defaultValue;
}
delete this.globalContext[key];
continue;
}
const searchPanelDefaultMatch = /^searchpanel_default_(.*)$/.exec(key);
if (searchPanelDefaultMatch) {
searchPanelDefaults[searchPanelDefaultMatch[1]] = defaultValue;
delete this.globalContext[key];
}
}

const parser = new SearchArchParser(
searchViewDescription,
searchViewFields,
Expand Down Expand Up @@ -389,6 +372,8 @@ export class SearchModel extends EventBus {
this.globalGroupBy = groupBy || [];
this.globalOrderBy = orderBy || [];

this._extractSearchDefaultsFromGlobalContext();

await this._reloadSections();
}

Expand Down Expand Up @@ -1455,6 +1440,28 @@ export class SearchModel extends EventBus {
}
}

_extractSearchDefaultsFromGlobalContext() {
const searchDefaults = {};
const searchPanelDefaults = {};
for (const key in this.globalContext) {
const defaultValue = this.globalContext[key];
const searchDefaultMatch = /^search_default_(.*)$/.exec(key);
if (searchDefaultMatch) {
if (defaultValue) {
searchDefaults[searchDefaultMatch[1]] = defaultValue;
}
delete this.globalContext[key];
continue;
}
const searchPanelDefaultMatch = /^searchpanel_default_(.*)$/.exec(key);
if (searchPanelDefaultMatch) {
searchPanelDefaults[searchPanelDefaultMatch[1]] = defaultValue;
delete this.globalContext[key];
}
}
return { searchDefaults, searchPanelDefaults };
}

/**
* Fetches values for each category at startup. At reload a category is
* only fetched if needed.
Expand Down
52 changes: 52 additions & 0 deletions addons/web/static/tests/search/with_search_tests.js
Expand Up @@ -333,4 +333,56 @@ QUnit.module("Search", (hooks) => {

await nextTick();
});

QUnit.test("search defaults are removed from context at reload", async function (assert) {
assert.expect(4);

const context = {
search_default_x: true,
searchpanel_default_y: true,
};

class TestComponent extends Component {
setup() {
onWillStart(() => {
assert.deepEqual(this.props.context, { lang: "en", tz: "taht", uid: 7 });
});
onWillUpdateProps((nextProps) => {
assert.deepEqual(nextProps.context, { lang: "en", tz: "taht", uid: 7 });
});
}
}
TestComponent.template = xml`<div class="o_test_component">Test component content</div>`;
TestComponent.props = { context: Object };

const env = await makeTestEnv(serverData);
const target = getFixture();

class Parent extends Component {
setup() {
useSubEnv({ config: {} });
this.searchState = useState({
resModel: "animal",
domain: [["type", "=", "carnivorous"]],
context,
});
}
}
Parent.template = xml`
<WithSearch t-props="searchState" t-slot-scope="search">
<TestComponent
context="search.context"
/>
</WithSearch>
`;
Parent.components = { WithSearch, TestComponent };

const parent = await mount(Parent, target, { env });
assert.deepEqual(parent.searchState.context, context);

parent.searchState.domain = [["type", "=", "herbivorous"]];

await nextTick();
assert.deepEqual(parent.searchState.context, context);
});
});
34 changes: 34 additions & 0 deletions addons/web/static/tests/webclient/actions/misc_tests.js
Expand Up @@ -15,6 +15,8 @@ import {
import { listView } from "@web/views/list/list_view";
import { companyService } from "@web/webclient/company_service";
import { onWillStart } from "@odoo/owl";
import { GraphModel } from "@web/views/graph/graph_model";
import { switchView } from "../../search/helpers";

let serverData;
let target;
Expand Down Expand Up @@ -361,6 +363,38 @@ QUnit.module("ActionManager", (hooks) => {
}
);

QUnit.test(
"search defaults are removed from context when switching view",
async function (assert) {
assert.expect(1);
serverData.views["partner,false,graph"] = `<graph/>`;
serverData.views["partner,false,list"] = `<list/>`;
const context = {
search_default_x: true,
searchpanel_default_y: true,
};
patchWithCleanup(GraphModel.prototype, {
load(searchParams) {
assert.deepEqual(searchParams.context, { lang: "en", tz: "taht", uid: 7 });
return super.load(...arguments);
},
});

const webClient = await createWebClient({ serverData });
await doAction(webClient, {
res_model: "partner",
type: "ir.actions.act_window",
views: [
[false, "list"],
[false, "graph"],
],
context,
});
// list view is loaded, switch to graph view
await switchView(target, "graph");
}
);

QUnit.test(
"retrieving a stored action should remove 'allowed_company_ids' from its context",
async function (assert) {
Expand Down

0 comments on commit 7ac5dcc

Please sign in to comment.