Skip to content

Commit

Permalink
[FIX][17.0] date_range : error opening conditional on studio
Browse files Browse the repository at this point in the history
  • Loading branch information
micheledic committed Aug 9, 2024
1 parent 63950e1 commit 7e9636e
Showing 1 changed file with 65 additions and 61 deletions.
126 changes: 65 additions & 61 deletions date_range/static/src/js/tree_editor.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,77 +46,81 @@ patch(TreeEditor.prototype, {
) {
info.component = Select;
}
let dateRanges = this.env.domain.dateRanges;
if (this.update_operator && this.update_operator.split("daterange_")[1]) {
dateRanges = this.env.domain.dateRanges.filter(
(range) =>
range.type_id[0] ===
Number(this.update_operator.split("daterange_")[1])
);
}
patch(info, {
extractProps({value, update}) {
const props = super.extractProps.apply(this, arguments);
if (
fieldDef &&
(fieldDef.type === "date" || fieldDef.type === "datetime") &&
node.operator.includes("daterange")
) {
let selected = dateRanges.find(
(range) =>
range.date_start ===
fromDateTime(value[1], fieldDef.type) &&
range.date_end === fromDateTime(value[0], fieldDef.type)
);
if (!selected) {
selected = dateRanges[0];
update([
toDateTime(selected.date_end, fieldDef.type),
toDateTime(selected.date_start, fieldDef.type, true),
]);
}

return {
options: dateRanges.map((dt) => [dt.id, dt.name]),
update: (v) => {
const range = dateRanges.find((r) => r.id === v);
if (typeof this.env.domain !== "undefined") {
let dateRanges = this.env.domain.dateRanges;
if (this.update_operator && this.update_operator.split("daterange_")[1]) {
dateRanges = this.env.domain.dateRanges.filter(
(range) =>
range.type_id[0] ===
Number(this.update_operator.split("daterange_")[1])
);
}
patch(info, {
extractProps({value, update}) {
const props = super.extractProps.apply(this, arguments);
if (
fieldDef &&
(fieldDef.type === "date" || fieldDef.type === "datetime") &&
node.operator.includes("daterange")
) {
let selected = dateRanges.find(
(range) =>
range.date_start ===
fromDateTime(value[1], fieldDef.type) &&
range.date_end === fromDateTime(value[0], fieldDef.type)
);
if (!selected) {
selected = dateRanges[0];
update([
toDateTime(range.date_end, fieldDef.type),
toDateTime(range.date_start, fieldDef.type, true),
toDateTime(selected.date_end, fieldDef.type),
toDateTime(selected.date_start, fieldDef.type, true),
]);
},
value: selected.id,
};
}
}

return props;
},
isSupported(value) {
if (node.operator.includes("daterange")) {
return Array.isArray(value) && value.length === 2;
}
return super.isSupported.apply(this, arguments);
},
});
return {
options: dateRanges.map((dt) => [dt.id, dt.name]),
update: (v) => {
const range = dateRanges.find((r) => r.id === v);
update([
toDateTime(range.date_end, fieldDef.type),
toDateTime(range.date_start, fieldDef.type, true),
]);
},
value: selected.id,
};
}

return props;
},
isSupported(value) {
if (node.operator.includes("daterange")) {
return Array.isArray(value) && value.length === 2;
}
return super.isSupported.apply(this, arguments);
},
});
}
return info;
},

updateLeafOperator(node, operator) {
super.updateLeafOperator.apply(this, arguments);
this.update_operator = operator;
const fieldDef = this.getFieldDef(node.path);
let dateRanges = this.env.domain.dateRanges.filter(
(range) => range.type_id[0] === Number(operator.split("daterange_")[1])
);
if (!dateRanges.length) {
dateRanges = this.env.domain.dateRanges;
}
if (operator.includes("daterange") && dateRanges) {
node.value = [
toDateTime(dateRanges[0].date_end, fieldDef.type),
toDateTime(dateRanges[0].date_start, fieldDef.type, true),
];
this.notifyChanges();
if (typeof this.env.domain !== "undefined") {
let dateRanges = this.env.domain.dateRanges.filter(
(range) => range.type_id[0] === Number(operator.split("daterange_")[1])
);
if (!dateRanges.length) {
dateRanges = this.env.domain.dateRanges;
}
if (operator.includes("daterange") && dateRanges) {
node.value = [
toDateTime(dateRanges[0].date_end, fieldDef.type),
toDateTime(dateRanges[0].date_start, fieldDef.type, true),
];
this.notifyChanges();
}
}
},
});

0 comments on commit 7e9636e

Please sign in to comment.