Skip to content

Commit 8827b5c

Browse files
committed
feat: show json result in tree
1 parent 6bdca1c commit 8827b5c

File tree

3 files changed

+32
-64
lines changed

3 files changed

+32
-64
lines changed

frontend/src/angular/src/app/function-search/function-search.component.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,35 @@
6161
</div>
6262
} @else {
6363
<p *ngIf="!!responseText" class="result-text">{{responseText}}</p>
64-
<p *ngIf="!!responseJson?.author" class="result-text">{{responseJson | json}}</p>
65-
<!--
64+
<p *ngIf="!!responseJson?.value1" class="result-text">
65+
<!-- {{responseJson | json}} -->
6666
<mat-tree
6767
[dataSource]="dataSource"
6868
[treeControl]="treeControl"
6969
class="example-tree"
7070
>
7171
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
72-
{{ node.name }}
73-
</mat-tree-node>
74-
<!-- This is the tree node template for expandable nodes -->
75-
<!--
72+
<div class="tree-node">
73+
<div>
74+
<span i18n="@@functionSearchTitle">Title</span>: {{ node.value1 }}
75+
</div>
76+
<div>
77+
<span i18n="@@functionSearchSummary">Summary</span>: {{ node.value2 }}
78+
</div>
79+
</div>
80+
</mat-tree-node>
7681
<mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
7782
<div class="mat-tree-node">
7883
<button
7984
mat-icon-button
80-
matTreeNodeToggle
81-
[attr.aria-label]="'Toggle ' + node.name"
85+
matTreeNodeToggle
8286
>
8387
<mat-icon class="mat-icon-rtl-mirror">
8488
{{ treeControl.isExpanded(node) ? "expand_more" : "chevron_right" }}
8589
</mat-icon>
8690
</button>
87-
{{ node.name }}
88-
</div>
89-
<!-- There is inline padding applied to this div using styles.
90-
This padding value depends on the mat-icon-button width. -->
91-
<!--
91+
<span i18n="@@fuctionSearchAuthor">Author</span>: {{ node.value1 }}
92+
</div>
9293
<div
9394
[class.example-tree-invisible]="!treeControl.isExpanded(node)"
9495
role="group"
@@ -97,5 +98,5 @@
9798
</div>
9899
</mat-nested-tree-node>
99100
</mat-tree>
100-
-->
101+
</p>
101102
}

frontend/src/angular/src/app/function-search/function-search.component.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
align-items: flex-start;
4545
}
4646

47+
.tree-node {
48+
display: block;
49+
width: 90vw;
50+
white-space: wrap;
51+
}
52+
4753
.spinner-container {
4854
display: flex;
4955
align-content: center;

frontend/src/angular/src/app/function-search/function-search.component.ts

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import { MatIconModule } from '@angular/material/icon';
3535
import {MatRadioModule} from '@angular/material/radio';
3636

3737
interface 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

Comments
 (0)