@@ -35,7 +35,8 @@ import { MatIconModule } from '@angular/material/icon';
3535import { MatRadioModule } from '@angular/material/radio' ;
3636
3737interface TreeNode {
38- name : string ;
38+ value1 : string ;
39+ value2 : string ;
3940 children ?: TreeNode [ ] ;
4041}
4142
@@ -71,7 +72,7 @@ export class FunctionSearchComponent {
7172 ) ;
7273 protected dataSource = new MatTreeNestedDataSource < TreeNode > ( ) ;
7374 protected responseText = '' ;
74- protected responseJson = { author : "" , books : [ ] } as JsonResult ;
75+ protected responseJson = { value1 : "" , value2 : "" } as TreeNode ;
7576 protected resultFormats = [ 'text' , 'json' ] ;
7677 protected resultFormatControl = new FormControl ( this . resultFormats [ 0 ] ) ;
7778
@@ -112,61 +113,21 @@ export class FunctionSearchComponent {
112113 tap ( ( ) => ( this . searching = false ) )
113114 )
114115 . subscribe ( value => this . resultFormatControl . value === this . resultFormats [ 0 ] ?
115- this . responseText = value . result || '' : this . responseJson = value . jsonResult || this . responseJson
116+ this . responseText = value . result || '' : this . responseJson = this . addToDataSource ( this . mapResult ( value . jsonResult || { author : "" , books : [ ] } as JsonResult ) )
116117 ) ;
117118 }
118119
119- private mapResult ( functionResponse : FunctionResponse ) : TreeNode [ ] {
120- return functionResponse . docs . map ( ( myBook ) => this . mapBook ( myBook ) ) ;
120+ private addToDataSource ( treeNode : TreeNode ) : TreeNode {
121+ this . dataSource . data = [ treeNode ] ;
122+ return treeNode ;
121123 }
122-
123- private mapBook ( book : Book ) : TreeNode {
124- const rootNode = { name : book . title , children : [ ] } as TreeNode ;
125- rootNode . children ?. push ( { name : 'Title: ' + book . title } as TreeNode ) ;
126- rootNode . children ?. push ( { name : 'Type: ' + book . type } as TreeNode ) ;
127- rootNode . children ?. push ( {
128- name : 'Average Ratings: ' + book . ratings_average ,
129- } as TreeNode ) ;
130- rootNode . children ?. push ( {
131- name : 'Authors' ,
132- children : this . mapArray ( book . author_name ) ,
133- } as TreeNode ) ;
134- rootNode . children ?. push ( {
135- name : 'Languages' ,
136- children : this . mapArray ( book . language ) ,
137- } as TreeNode ) ;
138- rootNode . children ?. push ( {
139- name : 'Persons' ,
140- children : this . mapArray ( book . person ) ,
141- } as TreeNode ) ;
142- rootNode . children ?. push ( {
143- name : 'Places' ,
144- children : this . mapArray ( book . place ) ,
145- } as TreeNode ) ;
146- rootNode . children ?. push ( {
147- name : 'Publishdates' ,
148- children : this . mapArray ( book . publish_date ) ,
149- } as TreeNode ) ;
150- rootNode . children ?. push ( {
151- name : 'Publishers' ,
152- children : this . mapArray ( book . publisher ) ,
153- } as TreeNode ) ;
154- rootNode . children ?. push ( {
155- name : 'Subjects' ,
156- children : this . mapArray ( book . subject ) ,
157- } as TreeNode ) ;
158- rootNode . children ?. push ( {
159- name : 'Times' ,
160- children : this . mapArray ( book . time ) ,
161- } as TreeNode ) ;
162- console . log ( rootNode ) ;
124+
125+ private mapResult ( jsonResult : JsonResult ) : TreeNode {
126+ const children = jsonResult ?. books . map ( value => ( { value1 : value . title , value2 : value . summary } as TreeNode ) ) ;
127+ const rootNode = { value1 : jsonResult . author , value2 : "" , children : children } as TreeNode ;
163128 return rootNode ;
164129 }
165130
166- private mapArray ( values : string [ ] ) : TreeNode [ ] {
167- return ! ! values ? values . map ( ( myStr ) => ( { name : myStr } as TreeNode ) ) : [ ] ;
168- }
169-
170131 protected logout ( ) : void {
171132 console . log ( 'logout' ) ;
172133 }
0 commit comments