Skip to content

Commit 4ab14b5

Browse files
committed
[IMP] reload handling on creation
1 parent 3829b88 commit 4ab14b5

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

spp_custom_fields_ui/static/src/js/custom_fields_ui_reload.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,59 @@
22

33
import {FormController} from "@web/views/form/form_controller";
44
import {patch} from "@web/core/utils/patch";
5+
import {useService} from "@web/core/utils/hooks";
56

67
patch(FormController.prototype, {
8+
setup() {
9+
super.setup();
10+
this.actionService = useService("action");
11+
},
12+
713
/**
814
* Override the saveButtonClicked method to trigger a reload after saving
915
* custom fields (ir.model.fields records with target_type).
1016
*/
1117
async saveButtonClicked(params = {}) {
12-
const result = await super.saveButtonClicked(params);
13-
1418
// Check if we're editing ir.model.fields with target_type (custom fields UI)
15-
if (this.props.resModel === "ir.model.fields" && this.model.root.data.target_type) {
16-
// Reload the page to refresh the model registry
17-
window.location.reload();
19+
const isCustomField = this.props.resModel === "ir.model.fields" && this.model.root.data.target_type;
20+
21+
if (!isCustomField) {
22+
return super.saveButtonClicked(params);
1823
}
1924

20-
return result;
25+
// Store record ID before save (will be false for new records)
26+
const recordIdBeforeSave = this.model.root.resId;
27+
28+
// Try to save
29+
try {
30+
const result = await super.saveButtonClicked(params);
31+
32+
// Only reload if save was successful
33+
// If result is defined and not false, save was successful
34+
if (result !== false) {
35+
const recordIdAfterSave = this.model.root.resId;
36+
37+
if (!recordIdBeforeSave && recordIdAfterSave) {
38+
// New record was created - reload and navigate to the saved record
39+
// Use actionService to reload the current action with the new record ID
40+
this.actionService.doAction({
41+
type: "ir.actions.act_window",
42+
res_model: this.props.resModel,
43+
res_id: recordIdAfterSave,
44+
views: [[false, "form"]],
45+
target: "current",
46+
});
47+
} else if (recordIdBeforeSave) {
48+
// Existing record was edited - just reload the page
49+
window.location.reload();
50+
}
51+
}
52+
53+
return result;
54+
} catch (error) {
55+
// Save failed (validation error, required fields missing, etc.)
56+
// Don't reload, let the user fix the errors
57+
throw error;
58+
}
2159
},
2260
});

0 commit comments

Comments
 (0)