[REF] web: remove dirtyTranslatableFields from model

The logic of dirtyTranslatableFields is only needed in one place in
the form view. We will therefore remove this function from the model.
We'll take the opportunity to replace dirtyFields with isFieldDirty
because all uses of dirtyFields want to check with the name of a field
if it is dirty or not.

Part of Task: 3179751

closes odoo/odoo#114560

Related: odoo/enterprise#37866
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2023-03-08 08:24:21 +01:00
parent 283c1f102d
commit d060853b7a
4 changed files with 16 additions and 33 deletions
@@ -12,8 +12,7 @@ export class EmployeeProfileController extends formView.Controller {
}
onWillSaveRecord(record) {
const dirtyFields = record.dirtyFields.map((f) => f.name);
this.mustReload = dirtyFields.includes("lang");
this.mustReload = record.isFieldDirty("lang");
}
onRecordSaved(record) {
@@ -14,7 +14,6 @@ import {
mapViews,
mapActiveFieldsToFieldsInfo,
} from "@web/views/legacy_utils";
import { localization } from "@web/core/l10n/localization";
import BasicModel from "web.BasicModel";
import Context from "web.Context";
@@ -150,27 +149,6 @@ export class Record extends DataPoint {
return this.model.__bm__.isDirty(this.__bm_handle__);
}
get dirtyFields() {
const changes = this.model.__bm__.localData[this.__bm_handle__]._changes;
if (!changes) {
return [];
}
return Object.keys(changes).map((change) => this.activeFields[change]);
}
get translatableFields() {
if (!localization.multiLang) {
return [];
}
return Object.values(this.fields)
.filter((f) => f.translate)
.map((f) => this.activeFields[f.name]);
}
get dirtyTranslatableFields() {
return this.translatableFields.filter((f) => this.dirtyFields.includes(f));
}
get isInEdition() {
return this.mode === "edit";
}
@@ -290,6 +268,11 @@ export class Record extends DataPoint {
return true;
}
isFieldDirty(fieldName) {
const changes = this.model.__bm__.localData[this.__bm_handle__]._changes;
return changes && changes[fieldName];
}
/**
* FIXME: memoize this at some point?
* @param {string} fieldName
@@ -4,6 +4,7 @@ import { hasTouch } from "@web/core/browser/feature_detection";
import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
import { makeContext } from "@web/core/context";
import { useDebugCategory } from "@web/core/debug/debug_context";
import { localization } from "@web/core/l10n/localization";
import { registry } from "@web/core/registry";
import { SIZES } from "@web/core/ui/ui_service";
import { useBus, useService } from "@web/core/utils/hooks";
@@ -442,11 +443,14 @@ export class FormController extends Component {
// Before we save, we gather dirty translate fields data. It needs to be done before the
// save as nothing will be dirty after. It is why there is a compute part and a show part.
if (record.dirtyTranslatableFields.length) {
const dirtyTranslatableFields = localization.multiLang
? Object.values(record.fields).filter((f) => f.translate && record.isFieldDirty(f.name))
: [];
if (dirtyTranslatableFields.length) {
const { resId } = record;
this.fieldsToTranslate[resId] = new Set([
...toRaw(this.fieldsToTranslate[resId] || []),
...record.dirtyTranslatableFields,
...dirtyTranslatableFields,
]);
}
if (this.props.saveRecord) {
@@ -523,13 +523,6 @@ export class Record extends DataPoint {
return false;
}
get dirtyFields() {
if (!this.isDirty) {
return [];
}
return this._changes.map((change) => this.activeFields[change]);
}
get isInEdition() {
return this.mode === "edit";
}
@@ -770,6 +763,10 @@ export class Record extends DataPoint {
);
}
isFieldDirty(fieldName) {
return this.isDirty && !!this._changes[fieldName];
}
isInvalid(fieldName) {
return this._invalidFields.has(fieldName);
}