Separating schema and resource names in frontend for more genericity

This commit is contained in:
2023-01-28 14:27:52 +01:00
parent 8951826e8e
commit 6f19f75a0b
6 changed files with 48 additions and 23 deletions

View File

@@ -19,6 +19,7 @@ export interface Model {
export class CardComponent implements OnInit {
@Input() resource: string | undefined;
@Input() resource_id: string | null = null;
@Input() schema: string | undefined;
@Input() is_modal: Boolean = false;
@Output() resourceCreated: EventEmitter<string> = new EventEmitter();
@@ -31,11 +32,16 @@ export class CardComponent implements OnInit {
schemas = JSON.parse(`{}`);
private _loading$ = new BehaviorSubject<boolean>(true);
private _formLoading$ = new BehaviorSubject<boolean>(true);
private _modelLoading$ = new BehaviorSubject<boolean>(true);
get loading$() {
return this._loading$.asObservable();
}
get formLoading$() {
return this._formLoading$.asObservable();
}
get modelLoading$() {
return this._modelLoading$.asObservable();
}
constructor(private crudService: CrudService,
private formlyJsonschema: CrudFormlyJsonschemaService,
@@ -44,28 +50,31 @@ export class CardComponent implements OnInit {
) { }
ngOnInit(): void {
this._loading$.next(true);
this._formLoading$.next(true);
this._modelLoading$.next(true);
let fields$;
if (this.isCreateForm()) {
fields$ = this.formlyJsonschema.getCreateFields(this.resource!);
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._loading$.next(false);
this.model = model;
this._modelLoading$.next(false);
});
fields$ = this.formlyJsonschema.getUpdateFields(this.resource!);
fields$ = this.formlyJsonschema.getUpdateFields(this.schema!);
}
fields$.subscribe((fields: any) => {
this.fields = [fields]
this._formLoading$.next(false);
});
}
onSubmit(model: any) {
this._loading$.next(true);
this._modelLoading$.next(true);
if (this.isCreateForm()) {
this.crudService.create(this.resource!, model).subscribe((response: any) => {
this._loading$.next(false);
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate([`../${response.id}`], {relativeTo: this.route});
} else {
@@ -75,15 +84,15 @@ export class CardComponent implements OnInit {
} else {
this.crudService.update(this.resource!, model).subscribe((model: any) => {
this.model = model;
this._loading$.next(false);
this._modelLoading$.next(false);
});
}
}
onDelete() {
this._loading$.next(true);
this._modelLoading$.next(true);
this.crudService.delete(this.resource!, this.model).subscribe((model: any) => {
this._loading$.next(false);
this._modelLoading$.next(false);
if (! this.is_modal) {
this.router.navigate(['../'], {relativeTo: this.route});
} else {