Skip to content

Commit 59b16cb

Browse files
committed
update related lines (CRUD)
1 parent 7eb43a0 commit 59b16cb

2 files changed

Lines changed: 89 additions & 6 deletions

File tree

typesense_ir_exports/static/src/action_ir_export.esm.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,59 @@ import {useService} from "@web/core/utils/hooks";
88
class CustomExportDataDialog extends ExportDataDialog {
99
setup() {
1010
super.setup();
11-
this.props.root.resModel = this.props.resModel;
12-
console.log(this.props.root);
13-
console.log(this.props.root.resModel, this.props.resModel);
11+
this.title = this.env._t("Select Data for Indexing");
12+
this.props.root.resModel = this.props.context.resModel;
13+
if (this.props.context.exporter_id) {
14+
this.state.templateId = this.props.context.exporter_id[0];
15+
}
16+
}
17+
async onUpdateExportTemplate() {
18+
const oldRec = await this.orm.read(
19+
"ir.exports",
20+
[this.state.templateId],
21+
["name", "export_fields"]
22+
);
23+
let oldLines = [];
24+
if (
25+
oldRec.length &&
26+
oldRec[0].export_fields &&
27+
oldRec[0].export_fields.length
28+
) {
29+
oldLines = await this.orm.read("ir.exports.line", oldRec[0].export_fields, [
30+
"name",
31+
]);
32+
}
33+
// Get list of field names from exportList and existing lines
34+
const newFieldNames = this.state.exportList.map((field) => field.id);
35+
const oldFieldMap = Object.fromEntries(
36+
oldLines.map((line) => [line.name, line.id])
37+
);
38+
// Prepare commands
39+
const fieldCommands = [];
40+
// Keep or create
41+
for (const field of this.state.exportList) {
42+
if (oldFieldMap[field.id]) {
43+
// Link existing line
44+
fieldCommands.push([4, oldFieldMap[field.id]]);
45+
} else {
46+
// New line to create
47+
fieldCommands.push([0, 0, {name: field.id}]);
48+
}
49+
}
50+
// Unlink deleted fields
51+
for (const oldLine of oldLines) {
52+
if (!newFieldNames.includes(oldLine.name)) {
53+
fieldCommands.push([3, oldLine.id]); // Unlink and delete
54+
}
55+
}
56+
// Write back to ir.exports
57+
await this.orm.write("ir.exports", [this.state.templateId], {
58+
export_fields: fieldCommands,
59+
});
60+
this.state.isEditingTemplate = false;
1461
}
1562
}
63+
CustomExportDataDialog.template = "typesense_ir_exports.CustomExportDataDialog";
1664

1765
class IrExportWidget extends Many2OneField {
1866
setup() {
@@ -34,12 +82,15 @@ class IrExportWidget extends Many2OneField {
3482
}
3583
async onExportData() {
3684
const dialogProps = {
37-
context: this.props.record.context,
85+
context: {
86+
...this.props.record.context,
87+
resModel: this.props.record.data.model_name,
88+
exporter_id: this.props.record.data.exporter_id,
89+
},
3890
defaultExportList: [],
3991
download: this.downloadExport.bind(this),
4092
getExportedFields: this.getExportedFields.bind(this),
4193
root: this.props.record.model.root,
42-
resModel: this.props.record.data.model_name,
4394
};
4495
this.dialogService.add(CustomExportDataDialog, dialogProps);
4596
}

typesense_ir_exports/static/src/action_ir_export.xml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,43 @@
77
>
88
<xpath expr="//div[hasclass('o_field_many2one_extra')]" position="after">
99
<div>
10-
<button t-on-click="openIrExport" icon="fa-check" class="btn btn-link">
10+
<button
11+
t-on-click="openIrExport"
12+
icon="fa-check"
13+
class="btn btn-link p-0 text-left"
14+
>
1115
Select Fields to Index
1216
</button>
1317
</div>
1418
</xpath>
1519
</t>
20+
<t
21+
t-name="typesense_ir_exports.CustomExportDataDialog"
22+
t-inherit="web.ExportDataDialog"
23+
owl="1"
24+
>
25+
<xpath expr="//div[hasclass('o_import_compat')]" position="attributes">
26+
<attribute name="t-if">false</attribute>
27+
</xpath>
28+
<xpath expr="//div[hasclass('o_export_format')]" position="attributes">
29+
<attribute name="t-if">false</attribute>
30+
</xpath>
31+
<xpath expr="//t[@t-set-slot='footer']" position="attributes">
32+
<attribute name="t-if">false</attribute>
33+
</xpath>
1634

35+
<xpath
36+
expr="//button[@t-on-click.stop='onCancelExportTemplate']"
37+
position="after"
38+
>
39+
<button
40+
t-if="state.templateId != 'new_template'"
41+
type="button"
42+
class="btn btn-secondary ms-1 o_save_list_btn"
43+
t-on-click.stop="onUpdateExportTemplate"
44+
>
45+
<i class="fa fa-floppy-o" />
46+
</button>
47+
</xpath>
48+
</t>
1749
</templates>

0 commit comments

Comments
 (0)