diff --git a/resources/ts/Components/Dialogs/AddEditRecordDialog.tsx b/resources/ts/Components/Dialogs/AddEditRecordDialog.tsx index b64580ecb..4087747f3 100644 --- a/resources/ts/Components/Dialogs/AddEditRecordDialog.tsx +++ b/resources/ts/Components/Dialogs/AddEditRecordDialog.tsx @@ -6,11 +6,14 @@ import { VnodeCollection, VnodeCollectionItem } from '@osm/typings/jsx'; +import {JSONAPI} from '@osm/typings/request'; import { isFormValid, - isVnode + isVnode, + showSnackbar } from '@osm/utils/misc'; import collect, {Collection} from 'collect.js'; +import {SaveResponse} from 'coloquent'; import { Children, Vnode, @@ -101,8 +104,6 @@ export default abstract class AddEditRecordDialog> ext } } - abstract save(): boolean | Promise; - afterForm(): VnodeCollection { return collect({ cancelButton: ( @@ -123,11 +124,36 @@ export default abstract class AddEditRecordDialog> ext this.formElement?.requestSubmit(); } + async save(): Promise { + this.record.setAttributes(this.modelAttributesFromFormState); + try { + const response = await this.record.save(); + this.afterSave(response); + return response.getModelId() !== undefined; + } catch (error) { + this.onSaveError(error as JSONAPI.RequestError); + return false; + } + } + + afterSave(response: SaveResponse): void { + const responseModel = response.getModel() as M; + if (responseModel !== undefined) { + this.record = responseModel; + void showSnackbar(__('Record salvato con successo')); + } + } + + onSaveError(error: JSONAPI.RequestError): void { + const message = error.response.errors.map((error_) => error_.detail).join('; '); + void showSnackbar(message, false); + } + protected static createFormState(entries: Record>): Map> { return new Map(Object.entries(entries)); } - get formStateRecord(): Record { + get modelAttributesFromFormState(): Record { const state: Record = {}; for (const [key, value] of this.formState) { state[key] = value(); diff --git a/resources/ts/Views/Users/UsersRecordDialog.tsx b/resources/ts/Views/Users/UsersRecordDialog.tsx index b9600166d..b7c8e93dc 100644 --- a/resources/ts/Views/Users/UsersRecordDialog.tsx +++ b/resources/ts/Views/Users/UsersRecordDialog.tsx @@ -6,9 +6,7 @@ import { } from '@mdi/js'; import AddEditRecordDialog from '@osm/Components/Dialogs/AddEditRecordDialog'; import MdIcon from '@osm/Components/MdIcon'; -import User, {UserAttributes} from '@osm/Models/User'; -import {JSONAPI} from '@osm/typings/request'; -import {showSnackbar} from '@osm/utils/misc'; +import User from '@osm/Models/User'; import collect, {Collection} from 'collect.js'; import {Children} from 'mithril'; import Stream from 'mithril/stream'; @@ -40,20 +38,6 @@ export default class UsersRecordDialog extends AddEditRecordDialog { if (this.record.isNew()) { this.record.setAttribute('password', 'default'); } - - this.record.setAttributes(this.formStateRecord as UserAttributes); - try { - const response = await this.record.save(); - const responseModel = response.getModel() as User; - if (responseModel !== undefined) { - this.record = responseModel; - void showSnackbar(__('Record salvato con successo')); - } - return response.getModelId() !== undefined; - } catch (error) { - const message = (error as JSONAPI.RequestError).response.errors.map((error_) => error_.detail).join('; '); - void showSnackbar(message, false); - return false; - } + return super.save(); } }