Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] date_range: Allow select date_ranges on custom filters #717

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions date_range/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
Expand Down
129 changes: 62 additions & 67 deletions date_range/static/src/js/date_range.esm.js
Original file line number Diff line number Diff line change
@@ -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);
}
},
});
5 changes: 4 additions & 1 deletion date_range/static/src/xml/date_range.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
t-if="(fieldType === 'date' || fieldType === 'datetime') && selectedOperator.date_range"
>
<span class="o_generator_menu_value">
<select class="o_input" t-on-change="_onValueInput(condition)">
<select
class="o_input"
t-on-change="ev => this.onValueChange(condition, ev)"
>
<option
t-foreach="date_ranges[selectedOperator.date_range_type]"
t-as="option"
Expand Down
Loading