From dc6834b21652df0e2284c92eff410a85c3edde6c Mon Sep 17 00:00:00 2001 From: Mathieu Duckerts-Antoine Date: Wed, 29 Jun 2022 12:34:11 +0000 Subject: [PATCH] [FIX] web: list: save list orderBy in favorite When creating a favorite in a (new) list view, the list orderBy (if any) was not saved with the favorite. We fix that. Part-of: odoo/odoo#92475 --- addons/web/static/src/search/search_model.js | 7 +++- .../src/search/with_search/with_search.js | 3 ++ .../static/src/views/list/list_controller.js | 3 ++ .../src/webclient/actions/action_hook.js | 4 ++ .../web/static/tests/views/list_view_tests.js | 41 +++++++++---------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/addons/web/static/src/search/search_model.js b/addons/web/static/src/search/search_model.js index 72e1704ca0a..7e5f83341e5 100644 --- a/addons/web/static/src/search/search_model.js +++ b/addons/web/static/src/search/search_model.js @@ -1738,6 +1738,11 @@ export class SearchModel extends EventBus { const { description, isDefault, isShared } = params; const fns = this.env.__getContext__.callbacks; const localContext = Object.assign({}, ...fns.map((fn) => fn())); + const gs = this.env.__getOrderBy__.callbacks; + let localOrderBy; + if (gs.length) { + localOrderBy = gs.flatMap((g) => g()); + } const context = makeContext([this._getContext(), localContext]); const userContext = this.userService.context; for (const key in context) { @@ -1749,7 +1754,7 @@ export class SearchModel extends EventBus { const domain = this._getDomain({ raw: true, withGlobal: false }).toString(); const groupBys = this._getGroupBy(); const comparison = this.getFullComparison(); - const orderBy = this._getOrderBy(); + const orderBy = localOrderBy || this._getOrderBy(); const userId = isShared ? false : this.userService.userId; const preFavorite = { diff --git a/addons/web/static/src/search/with_search/with_search.js b/addons/web/static/src/search/with_search/with_search.js index b371ff93a51..1c7b3367d19 100644 --- a/addons/web/static/src/search/with_search/with_search.js +++ b/addons/web/static/src/search/with_search/with_search.js @@ -13,6 +13,9 @@ export class WithSearch extends Component { if (!this.env.__getContext__) { useSubEnv({ __getContext__: new CallbackRecorder() }); } + if (!this.env.__getOrderBy__) { + useSubEnv({ __getOrderBy__: new CallbackRecorder() }); + } const SearchModelClass = this.props.SearchModel || SearchModel; this.searchModel = new SearchModelClass(this.env, { diff --git a/addons/web/static/src/views/list/list_controller.js b/addons/web/static/src/views/list/list_controller.js index 1d4a585fd12..3d0a1f4a196 100644 --- a/addons/web/static/src/views/list/list_controller.js +++ b/addons/web/static/src/views/list/list_controller.js @@ -104,6 +104,9 @@ export class ListController extends Component { rootState: this.model.root.exportState(), }; }, + getOrderBy: () => { + return this.model.root.orderBy; + }, }); usePager(() => { diff --git a/addons/web/static/src/webclient/actions/action_hook.js b/addons/web/static/src/webclient/actions/action_hook.js index 43a7112abff..bb4e96d8c08 100644 --- a/addons/web/static/src/webclient/actions/action_hook.js +++ b/addons/web/static/src/webclient/actions/action_hook.js @@ -59,6 +59,7 @@ export function useSetupAction(params = {}) { __getGlobalState__, __getLocalState__, __getContext__, + __getOrderBy__, } = component.env; const { beforeUnload, beforeLeave, getGlobalState, getLocalState, rootRef } = params; @@ -150,4 +151,7 @@ export function useSetupAction(params = {}) { if (__getContext__ && params.getContext) { useCallbackRecorder(__getContext__, params.getContext); } + if (__getOrderBy__ && params.getOrderBy) { + useCallbackRecorder(__getOrderBy__, params.getOrderBy); + } } diff --git a/addons/web/static/tests/views/list_view_tests.js b/addons/web/static/tests/views/list_view_tests.js index 6f8ee81afa6..536d732c972 100644 --- a/addons/web/static/tests/views/list_view_tests.js +++ b/addons/web/static/tests/views/list_view_tests.js @@ -36,6 +36,7 @@ import { triggerHotkey, } from "../helpers/utils"; import { + editFavoriteName, getButtons, getFacetTexts, getPagerLimit, @@ -43,11 +44,13 @@ import { groupByMenu, pagerNext, pagerPrevious, + saveFavorite, toggleActionMenu, toggleFavoriteMenu, toggleFilterMenu, toggleGroupByMenu, toggleMenuItem, + toggleSaveFavorite, validateSearch, } from "../search/helpers"; import { createWebClient, doAction, loadState } from "../webclient/helpers"; @@ -1520,40 +1523,36 @@ QUnit.module("Views", (hooks) => { ); QUnit.test("ordered target, sort attribute in context", async function (assert) { - // Equivalent to saving a custom filter - // FIXME WOWL: no it isn't - serverData.models.foo.fields.foo.sortable = true; serverData.models.foo.fields.date.sortable = true; - const list = await makeView({ + await makeView({ type: "list", resModel: "foo", serverData, arch: '', + mockRPC: (route, args) => { + if (args.method === "create_or_replace") { + const favorite = args.args[0]; + assert.step(favorite.sort); + return 7; + } + }, }); // Descending order on Foo - await click($(target).find('th.o_column_sortable:contains("Foo")')[0]); - await click($(target).find('th.o_column_sortable:contains("Foo")')[0]); + await click(target, "th.o_column_sortable[data-name=foo]"); + await click(target, "th.o_column_sortable[data-name=foo]"); // Ascending order on Date - await click($(target).find('th.o_column_sortable:contains("Date")')[0]); + await click(target, "th.o_column_sortable[data-name=date]"); - assert.deepEqual( - list.model.root.orderBy, - [ - { - name: "date", - asc: true, - }, - { - name: "foo", - asc: false, - }, - ], - "the list should have the right orderedBy" - ); + await toggleFavoriteMenu(target); + await toggleSaveFavorite(target); + await editFavoriteName(target, "My favorite"); + await saveFavorite(target); + + assert.verifySteps(['["date","foo desc"]']); }); QUnit.test("Loading a filter with a sort attribute", async function (assert) {