From 9fbc04923d69219b1a90af423a25044bc3b99b5e Mon Sep 17 00:00:00 2001 From: Carlos Roca Date: Mon, 4 Sep 2023 10:53:22 +0200 Subject: [PATCH] [FIX] date_range: Allow select date_ranges on custom filters This commit solves the problems reported on Issue #597 --- date_range/__manifest__.py | 2 - date_range/static/src/js/date_range.esm.js | 129 ++++++++++----------- date_range/static/src/xml/date_range.xml | 5 +- 3 files changed, 66 insertions(+), 70 deletions(-) diff --git a/date_range/__manifest__.py b/date_range/__manifest__.py index 24bb7ca9a2..627440a729 100644 --- a/date_range/__manifest__.py +++ b/date_range/__manifest__.py @@ -22,8 +22,6 @@ "assets": { "web.assets_backend": [ "date_range/static/src/js/date_range.esm.js", - ], - "web.assets_qweb": [ "date_range/static/src/xml/date_range.xml", ], }, diff --git a/date_range/static/src/js/date_range.esm.js b/date_range/static/src/js/date_range.esm.js index effdef2ec0..4bea72ee1e 100644 --- a/date_range/static/src/js/date_range.esm.js +++ b/date_range/static/src/js/date_range.esm.js @@ -1,77 +1,72 @@ /** @odoo-module **/ import {FIELD_OPERATORS, FIELD_TYPES} from "web.searchUtils"; -import CustomFilterItem from "web.CustomFilterItem"; +import {CustomFilterItem} from "@web/search/filter_menu/custom_filter_item"; import {_lt} from "@web/core/l10n/translation"; -import {patch} from "web.utils"; -import {session} from "@web/session"; +import {patch} from "@web/core/utils/patch"; +import {useService} from "@web/core/utils/hooks"; -patch( - CustomFilterItem.prototype, - "date_range/static/src/js/date_range.esm.js", - (T) => - class extends T { - constructor() { - super(...arguments); - this._computeDateRangeOperators(); - } +const {DateTime} = luxon; // eslint-disable-line no-undef - async _computeDateRangeOperators() { - this.OPERATORS = Object.assign({}, FIELD_OPERATORS); - this.OPERATORS.date = [...FIELD_OPERATORS.date]; - this.OPERATORS.datetime = [...FIELD_OPERATORS.datetime]; - this.date_ranges = {}; - const result = await this.rpc({ - model: "date.range", - method: "search_read", - fields: ["name", "type_id", "date_start", "date_end"], - context: session.user_context, - }); - result.forEach((range) => { - const range_type = range.type_id[0]; - if (this.date_ranges[range_type] === undefined) { - const r = { - symbol: "between", - description: `${_lt("in")} ${range.type_id[1]}`, - date_range: true, - date_range_type: range_type, - }; - this.OPERATORS.date.push(r); - this.OPERATORS.datetime.push(r); - this.date_ranges[range_type] = []; - } - this.date_ranges[range_type].push(range); - }); - } +patch(CustomFilterItem.prototype, "date_range.CustomFilterItem", { + setup() { + this._super(...arguments); + this.orm = useService("orm"); + this._computeDateRangeOperators(); + }, - _setDefaultValue(condition) { - const type = this.fields[condition.field].type; - const operator = this.OPERATORS[FIELD_TYPES[type]][condition.operator]; - if (operator.date_range) { - const default_range = this.date_ranges[operator.date_range_type][0]; - const d_start = moment(`${default_range.date_start} 00:00:00`); - const d_end = moment(`${default_range.date_end} 23:59:59`); - condition.value = [d_start, d_end]; - } else { - super._setDefaultValue(...arguments); - } + async _computeDateRangeOperators() { + this.OPERATORS = Object.assign({}, FIELD_OPERATORS); + this.OPERATORS.date = [...FIELD_OPERATORS.date]; + this.OPERATORS.datetime = [...FIELD_OPERATORS.datetime]; + this.date_ranges = {}; + const result = await this.orm.searchRead( + "date.range", + [], + ["name", "type_id", "date_start", "date_end"], + {} + ); + result.forEach((range) => { + const range_type = range.type_id[0]; + if (this.date_ranges[range_type] === undefined) { + const r = { + symbol: "between", + description: `${_lt("in")} ${range.type_id[1]}`, + date_range: true, + date_range_type: range_type, + }; + this.OPERATORS.date.push(r); + this.OPERATORS.datetime.push(r); + this.date_ranges[range_type] = []; } + this.date_ranges[range_type].push(range); + }); + }, - _onValueInputChange(condition, ev) { - const type = this.fields[condition.field].type; - const operator = this.OPERATORS[FIELD_TYPES[type]][condition.operator]; - if (operator.date_range) { - /* eslint-disable radix */ - const eid = parseInt(ev.target.value); - const ranges = this.date_ranges[operator.date_range_type]; - const range = ranges.find((x) => x.id === eid); - const d_start = moment(`${range.date_start} 00:00:00`); - const d_end = moment(`${range.date_end} 23:59:59`); - condition.value = [d_start, d_end]; - } else { - super._onValueInputChange(...arguments); - } - } + setDefaultValue(condition) { + const type = this.fields[condition.field].type; + const operator = this.OPERATORS[FIELD_TYPES[type]][condition.operator]; + if (operator.date_range) { + const default_range = this.date_ranges[operator.date_range_type][0]; + const d_start = DateTime.fromSQL(`${default_range.date_start} 00:00:00`); + const d_end = DateTime.fromSQL(`${default_range.date_end} 23:59:59`); + condition.value = [d_start, d_end]; + } else { + this._super(...arguments); } -); + }, -export default CustomFilterItem; + onValueChange(condition, ev) { + const type = this.fields[condition.field].type; + const operator = this.OPERATORS[FIELD_TYPES[type]][condition.operator]; + if (operator.date_range) { + const eid = parseInt(ev.target.value); + const ranges = this.date_ranges[operator.date_range_type]; + const range = ranges.find((x) => x.id === eid); + const d_start = DateTime.fromSQL(`${range.date_start} 00:00:00`); + const d_end = DateTime.fromSQL(`${range.date_end} 23:59:59`); + condition.value = [d_start, d_end]; + } else { + this._super(...arguments); + } + }, +}); diff --git a/date_range/static/src/xml/date_range.xml b/date_range/static/src/xml/date_range.xml index c4370fffc1..811c2a2616 100644 --- a/date_range/static/src/xml/date_range.xml +++ b/date_range/static/src/xml/date_range.xml @@ -8,7 +8,10 @@ t-if="(fieldType === 'date' || fieldType === 'datetime') && selectedOperator.date_range" > -