[REF] web: RelationalModel: add option to display invalid notif

This commit removes a function from the Record datapoint, and
replaces it by an option to checkValidity, as we want Record to
have a few public methods as possible.

Part of task~3179751

closes odoo/odoo#130170

Signed-off-by: Jorge Pinna Puissant (jpp) <jpp@odoo.com>
This commit is contained in:
Aaron Bohy
2023-08-01 10:55:51 +02:00
parent 9d81cff6fd
commit 9ed6036861
2 changed files with 19 additions and 23 deletions
@@ -133,11 +133,11 @@ export class Record extends DataPoint {
return this.model.mutex.exec(() => this._toggleArchive(true));
}
async checkValidity() {
async checkValidity({ displayNotification } = {}) {
if (!this._urgentSave) {
await this.model._askChanges();
}
return this._checkValidity();
return this._checkValidity({ displayNotification });
}
delete() {
@@ -208,21 +208,6 @@ export class Record extends DataPoint {
return this.model.mutex.exec(() => this._load());
}
openInvalidFieldsNotification() {
if (this._invalidFields.size) {
const items = [...this._invalidFields].map((fieldName) => {
return `<li>${escape(this.fields[fieldName].string || fieldName)}</li>`;
}, this);
this._closeInvalidFieldsNotification = this.model.notification.add(
markup(`<ul>${items.join("")}</ul>`),
{
title: _t("Invalid fields: "),
type: "danger",
}
);
}
}
async save(options) {
await this.model._askChanges();
return this.model.mutex.exec(() => this._save(options));
@@ -321,7 +306,7 @@ export class Record extends DataPoint {
this._setEvalContext();
}
_checkValidity({ silent } = {}) {
_checkValidity({ silent, displayNotification } = {}) {
const unsetRequiredFields = [];
for (const fieldName in this.activeFields) {
const fieldType = this.fields[fieldName].type;
@@ -364,7 +349,20 @@ export class Record extends DataPoint {
this._unsetRequiredFields.add(fieldName);
this._setInvalidField(fieldName);
}
return !this._invalidFields.size;
const isValid = !this._invalidFields.size;
if (!isValid && displayNotification) {
const items = [...this._invalidFields].map((fieldName) => {
return `<li>${escape(this.fields[fieldName].string || fieldName)}</li>`;
}, this);
this._closeInvalidFieldsNotification = this.model.notification.add(
markup(`<ul>${items.join("")}</ul>`),
{
title: _t("Invalid fields: "),
type: "danger",
}
);
}
return isValid;
}
_computeDataContext() {
@@ -791,8 +789,7 @@ export class Record extends DataPoint {
this.data[fieldName]._abandonRecords();
}
}
if (!this._checkValidity()) {
this.openInvalidFieldsNotification();
if (!this._checkValidity({ displayNotification: true })) {
return false;
}
const changes = this._getChanges();
@@ -590,7 +590,7 @@ export class X2ManyFieldDialog extends Component {
async save({ saveAndNew }) {
const disabledButtons = this.disableButtons();
if (await this.record.checkValidity()) {
if (await this.record.checkValidity({ displayNotification: true })) {
try {
await this.props.save(this.record);
} catch (error) {
@@ -601,7 +601,6 @@ export class X2ManyFieldDialog extends Component {
this.record = await this.props.addNew();
}
} else {
this.record.openInvalidFieldsNotification();
this.enableButtons(disabledButtons);
return false;
}