Skip to content

Commit

Permalink
[FIX] spreadsheet: filter clear button for automatic default value
Browse files Browse the repository at this point in the history
For the date filters with a default value set to "Automatically fitler
on the current period", the clear button don't work. It does not clear
the filter but revert it back to the default value.

The bug happens in the global filters side panel as well as in the
dashboards.

Odoo task 2989909

closes odoo#100579

Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
  • Loading branch information
hokolomopo committed Oct 17, 2022
1 parent abd79f0 commit 27e5805
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ export default class GlobalFiltersUIPlugin extends spreadsheet.UIPlugin {

const value = filterId in this.values ? this.values[filterId].value : filter.defaultValue;

if (filter.type === "date" && isEmpty(value) && filter.defaultsToCurrentPeriod) {
const preventAutomaticValue =
this.values[filterId] &&
this.values[filterId].value &&
this.values[filterId].value.preventAutomaticValue;
const defaultsToCurrentPeriod = !preventAutomaticValue && filter.defaultsToCurrentPeriod;

if (filter.type === "date" && isEmpty(value) && defaultsToCurrentPeriod) {
return this._getValueOfCurrentPeriod(filterId);
}

Expand Down Expand Up @@ -299,7 +305,7 @@ export default class GlobalFiltersUIPlugin extends spreadsheet.UIPlugin {
value = "";
break;
case "date":
value = { yearOffset: undefined };
value = { yearOffset: undefined, preventAutomaticValue: true };
break;
case "relation":
value = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,10 @@ QUnit.module("spreadsheet > Global filters model", {}, () => {
});
assert.deepEqual(model.getters.getGlobalFilterValue("42"), newValue);
model.dispatch("SET_MANY_GLOBAL_FILTER_VALUE", { filters: [{ filterId: "42" }] });
assert.deepEqual(model.getters.getGlobalFilterValue("42"), { yearOffset: undefined });
assert.deepEqual(model.getters.getGlobalFilterValue("42"), {
yearOffset: undefined,
preventAutomaticValue: true,
});
}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
/** @odoo-module */

import { getFixture, click, legacyExtraNextTick } from "@web/../tests/helpers/utils";
import { getFixture, click, legacyExtraNextTick, nextTick, editInput } from "@web/../tests/helpers/utils";
import { getDashboardServerData } from "../utils/data";
import { getBasicData, getBasicListArchs } from "@spreadsheet/../tests/utils/data";
import { createSpreadsheetDashboard } from "../utils/dashboard_action";

QUnit.module("spreadsheet_dashboard > Dashboard > Dashboard action");

function getServerData(spreadsheetData) {
const serverData = getDashboardServerData();
serverData.models = {
...serverData.models,
...getBasicData(),
};
serverData.views = getBasicListArchs();
serverData.models["spreadsheet.dashboard.group"].records = [
{
dashboard_ids: [789],
id: 1,
name: "Pivot",
},
];
serverData.models["spreadsheet.dashboard"].records = [
{
id: 789,
name: "Spreadsheet with Pivot",
json_data: JSON.stringify(spreadsheetData),
raw: JSON.stringify(spreadsheetData),
dashboard_group_id: 1,
},
];
return serverData;
}

QUnit.test("display available spreadsheets", async (assert) => {
await createSpreadsheetDashboard();
assert.containsN(getFixture(), ".o_search_panel section", 2);
Expand Down Expand Up @@ -108,28 +134,7 @@ QUnit.test(
},
},
};
const serverData = getDashboardServerData();
serverData.models = {
...serverData.models,
...getBasicData(),
};
serverData.views = getBasicListArchs();
serverData.models["spreadsheet.dashboard.group"].records = [
{
dashboard_ids: [789],
id: 1,
name: "Pivot",
},
];
serverData.models["spreadsheet.dashboard"].records = [
{
id: 789,
name: "Spreadsheet with Pivot",
json_data: JSON.stringify(spreadsheetData),
raw: JSON.stringify(spreadsheetData),
dashboard_group_id: 1,
},
];
const serverData = getServerData(spreadsheetData);
const fixture = getFixture();
await createSpreadsheetDashboard({ serverData });
await click(fixture, ".o_search_panel li:last-child");
Expand All @@ -141,3 +146,41 @@ QUnit.test(
assert.hasClass(fixture.querySelector(".o_search_panel li:last-child"), "active");
}
);

QUnit.test(
"Can clear filter date filter value that defaults to current period",
async function (assert) {
const spreadsheetData = {
globalFilters: [
{
id: "1",
type: "date",
label: "Date Filter",
rangeType: "year",
defaultValue: {},
defaultsToCurrentPeriod: true,
pivotFields: {},
},
],
};
const serverData = getServerData(spreadsheetData);
const fixture = getFixture();
await createSpreadsheetDashboard({ serverData });
const year = fixture.querySelector(".o_cp_top_right input.o_datepicker_input");
const this_year = luxon.DateTime.local().year;
assert.equal(year.value, String(this_year));
const input = fixture.querySelector(
"input.o_datepicker_input.o_input.datetimepicker-input"
);
await click(input);
await editInput(input, null, String(this_year - 1));
await nextTick();

assert.equal(year.value, String(this_year - 1));
assert.containsOnce(fixture, ".o_cp_top_right .fa-times");
await click(fixture.querySelector(".o_cp_top_right .fa-times"));

assert.containsNone(fixture, ".o_cp_top_right .fa-times");
assert.equal(year.value, "Select year...");
}
);

0 comments on commit 27e5805

Please sign in to comment.