refactor!: ♻️ 🎨 Riorganizzazione listener

BREAKING CHANGE: `customSetter` rinominato in `setter`
This commit is contained in:
Maicol Battistini 2022-01-25 19:53:44 +01:00
parent 27644cc3a3
commit 59a5927834
1 changed files with 57 additions and 60 deletions

View File

@ -57,10 +57,6 @@ export class RecordsPage extends Page {
dialogs: Children[];
recordDialogMaxWidth: string | number = 'auto';
model: typeof Model;
customSetter: (
model: IModel,
fields: Collection<File | string>
) => void;
/**
* What fields should take precedence when saving the record
@ -303,7 +299,14 @@ export class RecordsPage extends Page {
const form: Cash = dialog.find('form');
// Open "New record" dialog
fab.on('click', () => {
fab.on('click', this.openNewRecordDialog.bind(this, form, dialog));
const button = dialog.find('mwc-button[type="submit"]');
button.on('click', () => form.trigger('submit'));
form.on('submit', this.submitForm.bind(this, button, dialog, form));
}
openNewRecordDialog(form: Cash, dialog: Cash) {
form
.find('text-field, text-area, material-select')
.each(async (index, field) => {
@ -317,16 +320,11 @@ export class RecordsPage extends Page {
if (dialogElement) {
(dialogElement as MWCDialog).show();
}
});
}
const button = dialog.find('mwc-button[type="submit"]');
button.on('click', () => {
form.trigger('submit');
});
const loading: Cash = button.find('mwc-circular-progress');
form.on('submit', async (event: SubmitEvent) => {
async submitForm(button: Cash, dialog: Cash, form: Cash, event: SubmitEvent) {
event.preventDefault();
const loading: Cash = button.find('mwc-circular-progress');
loading.show();
if (isFormValid(form)) {
@ -335,25 +333,12 @@ export class RecordsPage extends Page {
// eslint-disable-next-line new-cap
const instance = this.rows.get(data.get('id'), new this.model() as IModel) as IModel;
if (this.customSetter) {
// eslint-disable-next-line @typescript-eslint/await-thenable
await this.customSetter(instance, data);
} else {
const filtered = data
.filter((item: any, id: string) => this.fieldsPrecedence.includes(id));
// @ts-ignore
(filtered.isEmpty() ? filtered : data).each((value: string, id: string) => {
instance[id] = value;
});
}
const response = await instance.save();
const modelId = response.getModelId();
const modelId = await this.setter(instance, data);
if (modelId) {
// @ts-ignore
const newResponse = await this.model.with(this.model.relationships).find(modelId);
const newResponse = await this.model.with(this.model.relationships)
.find(modelId);
const model = newResponse.getData() as IModel;
const dialogElement = dialog.get(0);
@ -370,7 +355,19 @@ export class RecordsPage extends Page {
loading.hide();
await showSnackbar(__('Campi non validi. Controlla i dati inseriti'));
}
}
async setter(model: IModel, data: Collection<File | string>) {
const filtered = data
.filter((item: any, id: string) => this.fieldsPrecedence.includes(id));
// @ts-ignore
(filtered.isEmpty() ? filtered : data).each((value: string, id: string) => {
model[id] = value;
});
const response = await model.save();
return response.getModelId();
}
getModelValue(model: IModel, field: string, raw = false): any {