|
2 | 2 |
|
3 | 3 | import {FormController} from "@web/views/form/form_controller"; |
4 | 4 | import {patch} from "@web/core/utils/patch"; |
| 5 | +import {useService} from "@web/core/utils/hooks"; |
5 | 6 |
|
6 | 7 | patch(FormController.prototype, { |
| 8 | + setup() { |
| 9 | + super.setup(); |
| 10 | + this.actionService = useService("action"); |
| 11 | + }, |
| 12 | + |
7 | 13 | /** |
8 | 14 | * Override the saveButtonClicked method to trigger a reload after saving |
9 | 15 | * custom fields (ir.model.fields records with target_type). |
10 | 16 | */ |
11 | 17 | async saveButtonClicked(params = {}) { |
12 | | - const result = await super.saveButtonClicked(params); |
13 | | - |
14 | 18 | // 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); |
18 | 23 | } |
19 | 24 |
|
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 | + } |
21 | 59 | }, |
22 | 60 | }); |
0 commit comments