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:
@@ -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<M extends Model<any, any>> ext
|
||||
}
|
||||
}
|
||||
|
||||
abstract save(): boolean | Promise<boolean>;
|
||||
|
||||
afterForm(): VnodeCollection {
|
||||
return collect<VnodeCollectionItem>({
|
||||
cancelButton: (
|
||||
@@ -123,11 +124,36 @@ export default abstract class AddEditRecordDialog<M extends Model<any, any>> ext
|
||||
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>> {
|
||||
return new Map(Object.entries(entries));
|
||||
}
|
||||
|
||||
get formStateRecord(): Record<string, unknown> {
|
||||
get modelAttributesFromFormState(): Record<string, unknown> {
|
||||
const state: Record<string, unknown> = {};
|
||||
for (const [key, value] of this.formState) {
|
||||
state[key] = value();
|
||||
|
@@ -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<User> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user