1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-06-05 22:09:38 +02:00

feat: Aggiunto save condiviso

This commit is contained in:
Maicol Battistini
2023-05-11 15:18:50 +02:00
parent 4a3a4f45e6
commit e151c0a67c
2 changed files with 32 additions and 22 deletions

View File

@@ -6,11 +6,14 @@ import {
VnodeCollection, VnodeCollection,
VnodeCollectionItem VnodeCollectionItem
} from '@osm/typings/jsx'; } from '@osm/typings/jsx';
import {JSONAPI} from '@osm/typings/request';
import { import {
isFormValid, isFormValid,
isVnode isVnode,
showSnackbar
} from '@osm/utils/misc'; } from '@osm/utils/misc';
import collect, {Collection} from 'collect.js'; import collect, {Collection} from 'collect.js';
import {SaveResponse} from 'coloquent';
import { import {
Children, Children,
Vnode, Vnode,
@@ -101,8 +104,6 @@ export default abstract class AddEditRecordDialog<M extends Model<any, any>> ext
} }
} }
abstract save(): boolean | Promise<boolean>;
afterForm(): VnodeCollection { afterForm(): VnodeCollection {
return collect<VnodeCollectionItem>({ return collect<VnodeCollectionItem>({
cancelButton: ( cancelButton: (
@@ -123,11 +124,36 @@ export default abstract class AddEditRecordDialog<M extends Model<any, any>> ext
this.formElement?.requestSubmit(); this.formElement?.requestSubmit();
} }
async save(): Promise<boolean> {
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<M>): 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<string, Stream<any>>): Map<string, Stream<unknown>> { protected static createFormState(entries: Record<string, Stream<any>>): Map<string, Stream<unknown>> {
return new Map(Object.entries(entries)); return new Map(Object.entries(entries));
} }
get formStateRecord(): Record<string, unknown> { get modelAttributesFromFormState(): Record<string, unknown> {
const state: Record<string, unknown> = {}; const state: Record<string, unknown> = {};
for (const [key, value] of this.formState) { for (const [key, value] of this.formState) {
state[key] = value(); state[key] = value();

View File

@@ -6,9 +6,7 @@ import {
} from '@mdi/js'; } from '@mdi/js';
import AddEditRecordDialog from '@osm/Components/Dialogs/AddEditRecordDialog'; import AddEditRecordDialog from '@osm/Components/Dialogs/AddEditRecordDialog';
import MdIcon from '@osm/Components/MdIcon'; import MdIcon from '@osm/Components/MdIcon';
import User, {UserAttributes} from '@osm/Models/User'; import User from '@osm/Models/User';
import {JSONAPI} from '@osm/typings/request';
import {showSnackbar} from '@osm/utils/misc';
import collect, {Collection} from 'collect.js'; import collect, {Collection} from 'collect.js';
import {Children} from 'mithril'; import {Children} from 'mithril';
import Stream from 'mithril/stream'; import Stream from 'mithril/stream';
@@ -40,20 +38,6 @@ export default class UsersRecordDialog extends AddEditRecordDialog<User> {
if (this.record.isNew()) { if (this.record.isNew()) {
this.record.setAttribute('password', 'default'); this.record.setAttribute('password', 'default');
} }
return super.save();
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;
}
} }
} }