77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import {Component, Input, OnInit} from '@angular/core';
|
|
import { FormGroup } from '@angular/forms';
|
|
import { FormlyFieldConfig } from '@ngx-formly/core'
|
|
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
|
|
import { Router } from '@angular/router';
|
|
|
|
import { CrudService } from '../crud.service'
|
|
import {BehaviorSubject} from "rxjs";
|
|
|
|
export interface Model {
|
|
email: string;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'crud-card',
|
|
templateUrl: './card.component.html',
|
|
styleUrls: ['./card.component.css']
|
|
})
|
|
export class CardComponent implements OnInit {
|
|
@Input() resource_id: string | null = null;
|
|
form = new FormGroup({});
|
|
model = {};
|
|
fields: FormlyFieldConfig[] = [];
|
|
resource: string = "Entity";
|
|
|
|
schemas = JSON.parse(`{}`);
|
|
|
|
private _loading$ = new BehaviorSubject<boolean>(true);
|
|
|
|
get loading$() {
|
|
return this._loading$.asObservable();
|
|
}
|
|
|
|
constructor(private crudService: CrudService,
|
|
private formlyJsonschema: FormlyJsonschema,
|
|
private router: Router
|
|
) { }
|
|
|
|
ngOnInit(): void {
|
|
this._loading$.next(true);
|
|
this.crudService.getSchema().subscribe((jsonSchemas: any) => {
|
|
this.schemas = jsonSchemas.components.schemas.EntityCreate
|
|
this.schemas.components = {
|
|
schemas: {
|
|
EntityType: jsonSchemas.components.schemas.EntityType
|
|
}
|
|
};
|
|
|
|
this.fields = [this.formlyJsonschema.toFieldConfig(this.schemas)];
|
|
});
|
|
|
|
if (this.resource_id !== null) {
|
|
this.crudService.get(this.resource_id!).subscribe((model: any) => {
|
|
this.model = model
|
|
});
|
|
|
|
this._loading$.next(false);
|
|
}
|
|
}
|
|
|
|
onSubmit(model: any) {
|
|
this._loading$.next(true);
|
|
if (this.resource_id !== null) {
|
|
this.crudService.update(model).subscribe((model: any) => {
|
|
this.model = model;
|
|
this._loading$.next(false);
|
|
});
|
|
} else {
|
|
this.crudService.create(model).subscribe((response: any) => {
|
|
this._loading$.next(false);
|
|
this.router.navigateByUrl('/entities/' + response.id);
|
|
});
|
|
}
|
|
console.log(model);
|
|
}
|
|
}
|