From 893013cdaedc61263529513ae4b29feb3d21a252 Mon Sep 17 00:00:00 2001 From: ewandor Date: Mon, 23 Jan 2023 03:58:39 +0100 Subject: [PATCH] working autocomplete, should implement search --- back/app/entity/schemas.py | 2 - .../crud/crud-formly-jsonschema.service.ts | 22 ++- front/app/src/common/crud/types/date.type.ts | 3 + .../src/common/crud/types/foreignkey.type.ts | 132 +++++++----------- 4 files changed, 66 insertions(+), 93 deletions(-) diff --git a/back/app/entity/schemas.py b/back/app/entity/schemas.py index 645b347f..8d41c70a 100644 --- a/back/app/entity/schemas.py +++ b/back/app/entity/schemas.py @@ -12,12 +12,10 @@ class EntityRead(Entity): class EntityCreate(Writer): - name: str address: str entity_data: Individual | Corporation = Field(..., discriminator='type') class EntityUpdate(BaseModel): - name: str address: str entity_data: Individual | Corporation = Field(..., discriminator='type') diff --git a/front/app/src/common/crud/crud-formly-jsonschema.service.ts b/front/app/src/common/crud/crud-formly-jsonschema.service.ts index 07dff9f1..4ae29808 100644 --- a/front/app/src/common/crud/crud-formly-jsonschema.service.ts +++ b/front/app/src/common/crud/crud-formly-jsonschema.service.ts @@ -2,9 +2,9 @@ import { FormlyJsonschema } from "@ngx-formly/core/json-schema"; import { FormlyJsonschemaOptions } from "@ngx-formly/core/json-schema/formly-json-schema.service"; import { FormlyFieldConfig } from "@ngx-formly/core"; import { JSONSchema7 } from 'json-schema'; -import {JsonschemasService } from "./jsonschemas.service"; -import {Observable, map, tap} from "rxjs"; -import {Injectable} from "@angular/core"; +import { JsonschemasService } from "./jsonschemas.service"; +import { Observable, map } from "rxjs"; +import { Injectable } from "@angular/core"; @Injectable({ providedIn: 'root', @@ -14,21 +14,29 @@ export class CrudFormlyJsonschemaService extends FormlyJsonschema { super(); } - override toFieldConfig(schema: JSONSchema7, options?: FormlyJsonschemaOptions): FormlyFieldConfig { - return super.toFieldConfig(schema, options); + override toFieldConfig(schema: JSONSchema7): FormlyFieldConfig { + let fieldConfig = super.toFieldConfig(schema, new CrudFormlyJsonschemaOptions()); + return fieldConfig; } getCreateFields(resourceName: string): Observable { return this.jsonSchemasService.getCreateResource(resourceName).pipe( map((schemas: any) => this.toFieldConfig(schemas)), - tap(console.log), ) } getUpdateFields(resourceName: string): Observable { return this.jsonSchemasService.getUpdateResource(resourceName).pipe( map((schemas: any) => this.toFieldConfig(schemas)), - tap(console.log), ) } } + +export class CrudFormlyJsonschemaOptions implements FormlyJsonschemaOptions { + map = (field: any, schema: any) => { + if (schema.hasOwnProperty('foreignKey')) { + field.foreignKey = schema['foreignKey']; + } + return field; + } +} diff --git a/front/app/src/common/crud/types/date.type.ts b/front/app/src/common/crud/types/date.type.ts index c58c25f3..46dc1789 100644 --- a/front/app/src/common/crud/types/date.type.ts +++ b/front/app/src/common/crud/types/date.type.ts @@ -12,6 +12,9 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core'; class="form-label">{{ props.label }} +
- - - -
+
+ (selectItem)="selectedItem($event)" + [(ngModel)]="foreignModel" + [ngbTypeahead]="search" + [inputFormatter]="formatter" + [resultFormatter]="formatter" + [editable]="false" />
- + +
+
+
+ +
+ +
+
+
`, }) -export class ForeignkeyTypeComponent extends FieldType implements OnInit +export class ForeignkeyTypeComponent extends FieldType implements AfterContentInit { + public foreignModel: any = {} + public foreignLabel: string = "" public foreignId: string = "" public foreignResource : string = ""; public errorMsg: string = ""; @@ -109,20 +61,24 @@ export class ForeignkeyTypeComponent extends FieldType implemen super(); } - asyncSelected: string = "toto"; typeaheadLoading: boolean = false; typeaheadNoResults: boolean = true; dataSource: Observable = new Observable(); + formatter = (foreignModel: any) => foreignModel.label; - ngOnInit() { - this.foreignResource = this.field.key!.toString().split('_')[0]; - this.dataSource = new Observable((observer: any) => { - observer.next(this.asyncSelected); - }) + + ngAfterContentInit() { + // @ts-ignore + this.foreignResource = this.field.foreignKey.reference.resource; + if (this.hasValue()) { + this.crudService.get(this.foreignResource, this.formControl.value).subscribe((value: any) => { + this.foreignModel = value; + this.foreignLabel = this.foreignModel.label; + }); + } } - // fired every time search string is changed search: OperatorFunction = (text$: Observable) => { return text$.pipe( debounceTime(200), @@ -136,7 +92,15 @@ export class ForeignkeyTypeComponent extends FieldType implemen return this.crudService.getList(this.foreignResource, 0, 10, '_id', 'asc') .pipe( map((result: any) => result["items"]), - tap(console.log), ); } + + selectedItem(event: any) { + this.formControl.setValue(event.item._id) + this.foreignLabel = event.item.label; + } + + hasValue() { + return this.formControl.value !== undefined; + } }