Propagating errors and displaying flashmessages

This commit is contained in:
2023-02-12 16:17:50 +01:00
parent d4e1acb4e5
commit 57a282b004
8 changed files with 83 additions and 38 deletions

View File

@@ -23,6 +23,7 @@ export class CardComponent implements OnInit {
@Output() resourceCreated: EventEmitter<string> = new EventEmitter();
@Output() resourceUpdated: EventEmitter<string> = new EventEmitter();
@Output() resourceDeleted: EventEmitter<string> = new EventEmitter();
@Output() error: EventEmitter<string> = new EventEmitter();
form = new FormGroup({});
@@ -56,48 +57,63 @@ export class CardComponent implements OnInit {
this._modelLoading$.next(false);
fields$ = this.formlyJsonschema.getCreateFields(this.schema!);
} else {
this.crudService.get(this.resource!, this.resource_id!).subscribe((model: any) => {
this.model = model;
this._modelLoading$.next(false);
this.crudService.get(this.resource!, this.resource_id!).subscribe({
next :(model: any) => {
this.model = model;
this._modelLoading$.next(false);
},
error: (err) => this.error.emit("Error loading the model:" + err)
});
fields$ = this.formlyJsonschema.getUpdateFields(this.schema!);
}
fields$.subscribe((fields: any) => {
this.fields = [fields];
this._formLoading$.next(false);
fields$.subscribe({
next: (fields: any) => {
this.fields = [fields];
this._formLoading$.next(false);
},
error: (err) => this.error.emit("Error loading the form:" + err)
});
}
onSubmit(model: any) {
this._modelLoading$.next(true);
if (this.isCreateForm()) {
this.crudService.create(this.resource!, model).subscribe((response: any) => {
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate([`../${response.id}`], {relativeTo: this.route});
} else {
this.resourceCreated.emit(response.id)
}
this.crudService.create(this.resource!, model).subscribe({
next: (response: any) => {
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate([`../${response.id}`], {relativeTo: this.route});
} else {
this.resourceCreated.emit(response.id)
}
},
error: (err) => this.error.emit("Error creating the entity:" + err)
});
} else {
this.crudService.update(this.resource!, model).subscribe((model: any) => {
this.model = model;
this._modelLoading$.next(false);
this.resourceUpdated.emit(model._id)
this.crudService.update(this.resource!, model).subscribe( {
next: (model: any) => {
this.model = model;
this._modelLoading$.next(false);
this.resourceUpdated.emit(model._id)
},
error: (err) => this.error.emit("Error updating the entity:" + err)
});
}
}
onDelete() {
this._modelLoading$.next(true);
this.crudService.delete(this.resource!, this.model).subscribe((model: any) => {
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate(['../'], {relativeTo: this.route});
} else {
this.resourceDeleted.emit("")
}
this.crudService.delete(this.resource!, this.model).subscribe({
next: (model: any) => {
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate(['../'], {relativeTo: this.route});
} else {
this.resourceDeleted.emit("")
}
},
error: (err) => this.error.emit("Error deleting the entity:" + err)
});
}