11import { Component , OnInit , QueryList , ViewChildren } from '@angular/core' ;
22import { AssetService } from "../../../shared/services/asset.service" ;
33import { PolicyService } from "../../../shared/services/policy.service" ;
4- import { Asset , PolicyDefinition , ContractDefinitionInput } from "../../../shared/models/edc-connector-entities" ;
4+ import { Asset , PolicyDefinition , ContractDefinitionInput , QuerySpec } from "../../../shared/models/edc-connector-entities" ;
55import { NotificationService } from 'src/app/shared/services/notification.service' ;
66import { ContractDefinitionService } from 'src/app/shared/services/contractDefinition.service' ;
7- import { BehaviorSubject } from 'rxjs' ;
7+ import { BehaviorSubject , finalize , Observable , of , startWith , switchMap , tap } from 'rxjs' ;
88import { Router } from '@angular/router' ;
9- import { NgModel } from '@angular/forms' ;
9+ import { FormControl , NgModel } from '@angular/forms' ;
1010
1111
1212@Component ( {
@@ -16,13 +16,16 @@ import { NgModel } from '@angular/forms';
1616} )
1717export class ContractDefinitionNewComponent implements OnInit {
1818
19+ assetControl = new FormControl ( '' ) ;
20+ filteredAssets : Asset [ ] = [ ] ;
21+ selectedAssets : Asset [ ] = [ ] ;
22+ isLoading = false ;
1923 policies : Array < PolicyDefinition > = [ ] ;
2024 availableAssets : Asset [ ] = [ ] ;
2125 name : string = '' ;
2226 editMode = false ;
2327 accessPolicy ?: PolicyDefinition ;
2428 contractPolicy ?: PolicyDefinition ;
25- assets : Asset [ ] = [ ] ;
2629 contractDefinition : ContractDefinitionInput = {
2730 "@id" : '' ,
2831 assetsSelector : [ ] ,
@@ -45,14 +48,49 @@ export class ContractDefinitionNewComponent implements OnInit {
4548 this . accessPolicy = this . policies . find ( policy => policy [ '@id' ] === this . contractDefinition . accessPolicyId ) ;
4649 this . contractPolicy = this . policies . find ( policy => policy [ '@id' ] === this . contractDefinition . contractPolicyId ) ;
4750 } ) ;
48- this . assetService . requestAssets ( ) . subscribe ( assets => {
49- this . availableAssets = assets ;
50- // preselection
51- if ( this . contractDefinition ) {
52- const assetIds = this . contractDefinition . assetsSelector . map ( c => c . operandRight ?. toString ( ) ) ;
53- this . assets = this . availableAssets . filter ( asset => assetIds . includes ( asset . id ) ) ;
54- }
55- } )
51+
52+ this . assetControl . valueChanges . pipe (
53+ startWith ( '' ) ,
54+ tap ( ( ) => this . isLoading = true ) ,
55+ switchMap ( value => {
56+ const query = typeof value === 'string' ? value . trim ( ) : '' ;
57+ const querySpec : QuerySpec = {
58+ offset : 0 ,
59+ limit : 50 ,
60+ filterExpression : [
61+ {
62+ operandLeft : 'id' ,
63+ operator : 'ilike' ,
64+ operandRight : `%${ query } %`
65+ }
66+ ]
67+ } ;
68+ return this . assetService . requestAssets ( querySpec )
69+ . pipe (
70+ finalize ( ( ) => this . isLoading = false )
71+ ) ;
72+ } )
73+ ) . subscribe ( results => {
74+ this . filteredAssets = results . filter (
75+ asset => ! this . selectedAssets . some ( sel => sel . id === asset . id )
76+ ) ;
77+
78+ } ) ;
79+ }
80+
81+ displayAsset ( asset : Asset ) : string {
82+ return asset ? asset . id : '' ;
83+ }
84+
85+ addAsset ( event : any , asset : Asset ) : void {
86+ if ( event . isUserInput && ! this . selectedAssets . find ( a => a . id === asset . id ) ) {
87+ this . selectedAssets . push ( asset ) ;
88+ this . assetControl . setValue ( '' ) ;
89+ }
90+ }
91+
92+ removeAsset ( asset : Asset ) : void {
93+ this . selectedAssets = this . selectedAssets . filter ( a => a . id !== asset . id ) ;
5694 }
5795
5896 onSave ( ) {
@@ -69,8 +107,9 @@ export class ContractDefinitionNewComponent implements OnInit {
69107 this . contractDefinition . contractPolicyId = this . contractPolicy [ '@id' ] ! ;
70108 this . contractDefinition . assetsSelector = [ ] ;
71109
72- if ( this . assets . length > 0 ) {
73- const ids = this . assets . map ( asset => asset . id ) ;
110+ if ( this . selectedAssets . length > 0 ) {
111+ const ids = this . selectedAssets . map ( asset => asset . id ) ;
112+
74113 this . contractDefinition . assetsSelector = [ ...this . contractDefinition . assetsSelector , {
75114 operandLeft : 'https://w3id.org/edc/v0.0.1/ns/id' ,
76115 operator : 'in' ,
0 commit comments