Skip to content

Commit 9e6813a

Browse files
authored
Load categories dynamically also when AI is enabled (#233)
* Load existing categories also for AI search * Remove hardcoded categories for AI search * Improve loading performace for categories in search page * Fix issues managing categories in the admin section
1 parent 674dff3 commit 9e6813a

5 files changed

Lines changed: 359 additions & 366 deletions

File tree

src/app/data/availableFilters.ts

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,6 @@ export type Filter = {
44
}
55

66
const availableFilters: Filter[] = [
7-
{
8-
name: 'technical_approach',
9-
children: [
10-
{
11-
name: 'Infrastructure as a Service (IaaS)',
12-
children: [
13-
{ name: 'Compute' },
14-
{ name: 'Network' },
15-
{ name: 'Storage' },
16-
{ name: 'Security' },
17-
],
18-
},
19-
{ name: 'Container as a Service (CaaS)' },
20-
{
21-
name: 'Platform as a Service (PaaS)',
22-
children: [
23-
{ name: 'Database' },
24-
{ name: 'Development and Testing' },
25-
{ name: 'Business Analytics' },
26-
{ name: 'Process management' },
27-
{ name: 'Knowledge management' },
28-
{ name: 'Data management' },
29-
],
30-
},
31-
{ name: 'Software as a Service (SaaS)' },
32-
{ name: 'Artificial Intelligence and Machine Learning' },
33-
{
34-
name: 'Data as a Service (DaaS)',
35-
children: [{ name: 'Data product distribution and exchange' }],
36-
},
37-
{ name: 'Cybersecurity and Data Privacy' },
38-
{ name: 'Internet of Things (IoT)' },
39-
],
40-
},
41-
{
42-
name: 'business_domain',
43-
children: [
44-
{ name: 'Automotive' },
45-
{ name: 'Agriculture, Forestry, Fishing' },
46-
{ name: 'Blockchain (DLT)' },
47-
{ name: 'Beauty and Perfume' },
48-
{ name: 'Cleaning and Facility Management Services' },
49-
{
50-
name: 'Community Groups, Social, Political and Religious',
51-
children: [{ name: 'Governmental Administration and Regulation' }],
52-
},
53-
{ name: 'Education' },
54-
{ name: 'Construction' },
55-
{ name: 'Employment, Recruitment, HR' },
56-
{
57-
name: 'Energy and Utility Suppliers',
58-
children: [
59-
{ name: 'Electricity' },
60-
{ name: 'Gas' },
61-
{ name: 'Waste Collection, Treatment and Disposal Activities' },
62-
{ name: 'Water Supply' },
63-
],
64-
},
65-
{ name: 'Financial Services and Insurance' },
66-
{ name: 'Healthcare' },
67-
{ name: 'IT' },
68-
{ name: 'Leisure and Entertainment' },
69-
{ name: 'Legal, Public Order, Security' },
70-
{
71-
name: 'Manufacturing',
72-
children: [{ name: 'Manufacturing of Metal Products' }, { name: 'Other (manufacturing)' }],
73-
},
74-
{ name: 'Mining and Drilling' },
75-
{ name: 'Project Management, Marketing and Admin' },
76-
{ name: 'Personal Services' },
77-
{ name: 'Restaurants, Bars, Cafes, Catering' },
78-
{ name: 'Real Estate' },
79-
{ name: 'Publishing, Printing and Photography' },
80-
{ name: 'Tourism and Accommodation' },
81-
{ name: 'Science and Engineering' },
82-
{ name: 'Trade' },
83-
{
84-
name: 'Transportation and Transportation infrastructure',
85-
children: [
86-
{ name: 'Transport of Freight' },
87-
{ name: 'Transport of Persons' },
88-
{ name: 'Other' },
89-
],
90-
},
91-
],
92-
},
93-
{
94-
name: 'professional_services',
95-
children: [
96-
{ name: 'Implementation Services' },
97-
{ name: 'Consulting' },
98-
{
99-
name: 'Service Management',
100-
children: [{ name: 'Operations' }, { name: 'Maintenance' }, { name: 'Governance' }],
101-
},
102-
],
103-
},
1047
{
1058
name: 'compliance_profile',
1069
children: [

src/app/pages/admin/categories/categories.component.ts

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class CategoriesComponent implements OnDestroy {
6161
initCatalogs(){
6262
this.loading=true;
6363
this.categories=[];
64+
this.unformattedCategories=[];
6465
let aux = this.localStorage.getObject('login_items') as LoginInfo;
6566
if(aux.logged_as==aux.id){
6667
this.partyId = aux.partyId;
@@ -69,7 +70,7 @@ export class CategoriesComponent implements OnDestroy {
6970
this.partyId = loggedOrg.partyId
7071
}
7172

72-
this.getCategories();
73+
void this.getCategories();
7374
initFlowbite();
7475
}
7576

@@ -81,7 +82,7 @@ export class CategoriesComponent implements OnDestroy {
8182
this.eventMessage.emitUpdateCategory(cat);
8283
}
8384

84-
getCategories(){
85+
async getCategories(){
8586
/*this.api.getCatalog(this.selectedCatalog.id).then(data => {
8687
if(data.category){
8788
for (let i=0; i<data.category.length; i++){
@@ -101,65 +102,31 @@ export class CategoriesComponent implements OnDestroy {
101102
}
102103
})*/
103104
console.log('Getting categories...')
104-
this.api.getDefaultCategories().then(data => {
105-
for(let i=0; i < data.length; i++){
106-
this.findChildren(data[i],data);
107-
this.unformattedCategories.push(data[i]);
108-
}
109-
this.loading=false;
110-
this.cdr.detectChanges();
111-
initFlowbite();
112-
})
113-
}
114-
115-
findChildren(parent:any,data:any[]){
116-
let childs = data.filter((p => p.parentId === parent.id));
117-
parent["children"] = childs;
118-
if(parent.isRoot == true){
119-
this.categories.push(parent)
120-
} else {
121-
this.saveChildren(this.categories,parent)
122-
}
123-
if(childs.length != 0){
124-
for(let i=0; i < childs.length; i++){
125-
this.findChildren(childs[i],data)
126-
}
127-
}
128-
}
105+
const rootCategories = await this.api.getDefaultCategories().catch(() => []);
106+
const roots = Array.isArray(rootCategories) ? rootCategories : [];
129107

130-
findChildrenByParent(parent:any){
131-
let childs: any[] = []
132-
this.api.getCategoriesByParentId(parent.id).then(c => {
133-
childs=c;
134-
parent["children"] = childs;
135-
if(parent.isRoot == true){
136-
this.categories.push(parent)
137-
} else {
138-
this.saveChildren(this.categories,parent)
139-
}
140-
if(childs.length != 0){
141-
for(let i=0; i < childs.length; i++){
142-
this.findChildrenByParent(childs[i])
143-
}
144-
}
145-
initFlowbite();
146-
})
108+
this.unformattedCategories = [...roots];
109+
const categoryTrees = await Promise.all(
110+
roots.map((root: any) => this.loadCategorySubtree(root))
111+
);
147112

113+
this.categories = categoryTrees.filter((cat): cat is Category => !!cat);
114+
this.loading=false;
115+
this.cdr.detectChanges();
116+
initFlowbite();
148117
}
149118

150-
saveChildren(superCategories:any[],parent:any){
151-
for(let i=0; i < superCategories.length; i++){
152-
let children = superCategories[i].children;
153-
if (children != undefined){
154-
let check = children.find((element: { id: any; }) => element.id == parent.id)
155-
if (check != undefined) {
156-
let idx = children.findIndex((element: { id: any; }) => element.id == parent.id)
157-
children[idx] = parent
158-
superCategories[i].children = children
159-
}
160-
this.saveChildren(children,parent)
161-
}
162-
}
119+
private async loadCategorySubtree(parent:any): Promise<Category> {
120+
const children = await this.api.getCategoriesByParentId(parent.id).catch(() => []);
121+
const childList = Array.isArray(children) ? children : [];
122+
const resolvedChildren = await Promise.all(
123+
childList.map((child: any) => this.loadCategorySubtree(child))
124+
);
125+
126+
return {
127+
...parent,
128+
children: resolvedChildren
129+
};
163130
}
164131

165132
/*addParent(parentId:any){
@@ -187,7 +154,8 @@ export class CategoriesComponent implements OnDestroy {
187154
}
188155
this.loading=true;
189156
this.categories=[];
190-
this.getCategories();
157+
this.unformattedCategories=[];
158+
void this.getCategories();
191159
console.log('filter')
192160
}
193161

src/app/pages/admin/categories/create-category/create-category.component.ts

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -101,74 +101,44 @@ export class CreateCategoryComponent implements OnInit, OnDestroy {
101101
this.partyId = loggedOrg.partyId
102102
}
103103
}
104-
this.getCategories();
104+
void this.getCategories();
105105
}
106106

107107
goBack() {
108108
this.eventMessage.emitAdminCategories(true);
109109
}
110110

111-
getCategories(){
111+
async getCategories(){
112112
console.log('Getting categories...')
113-
this.api.getLaunchedCategories().then(data => {
114-
for(let i=0; i < data.length; i++){
115-
this.findChildren(data[i],data);
116-
this.unformattedCategories.push(data[i]);
117-
}
118-
this.loading=false;
119-
this.cdr.detectChanges();
120-
initFlowbite();
121-
})
122-
}
113+
this.loading = true;
114+
this.categories = [];
115+
this.unformattedCategories = [];
123116

124-
findChildren(parent:any,data:any[]){
125-
let childs = data.filter((p => p.parentId === parent.id));
126-
parent["children"] = childs;
127-
if(parent.isRoot == true){
128-
this.categories.push(parent)
129-
} else {
130-
this.saveChildren(this.categories,parent)
131-
}
132-
if(childs.length != 0){
133-
for(let i=0; i < childs.length; i++){
134-
this.findChildren(childs[i],data)
135-
}
136-
}
137-
}
117+
const rootCategories = await this.api.getDefaultCategories().catch(() => []);
118+
const roots = Array.isArray(rootCategories) ? rootCategories : [];
138119

139-
findChildrenByParent(parent:any){
140-
let childs: any[] = []
141-
this.api.getCategoriesByParentId(parent.id).then(c => {
142-
childs=c;
143-
parent["children"] = childs;
144-
if(parent.isRoot == true){
145-
this.categories.push(parent)
146-
} else {
147-
this.saveChildren(this.categories,parent)
148-
}
149-
if(childs.length != 0){
150-
for(let i=0; i < childs.length; i++){
151-
this.findChildrenByParent(childs[i])
152-
}
153-
}
154-
initFlowbite();
155-
})
120+
this.unformattedCategories = [...roots];
121+
const categoryTrees = await Promise.all(
122+
roots.map((root: any) => this.loadCategorySubtree(root))
123+
);
156124

125+
this.categories = categoryTrees.filter(Boolean);
126+
this.loading=false;
127+
this.cdr.detectChanges();
128+
initFlowbite();
157129
}
158130

159-
saveChildren(superCategories:any[],parent:any){
160-
for(let i=0; i < superCategories.length; i++){
161-
let children = superCategories[i].children;
162-
if (children != undefined){
163-
let check = children.find((element: { id: any; }) => element.id == parent.id)
164-
if (check != undefined) {
165-
let idx = children.findIndex((element: { id: any; }) => element.id == parent.id)
166-
children[idx] = parent
167-
superCategories[i].children = children
168-
}
169-
this.saveChildren(children,parent)
170-
}
171-
}
131+
private async loadCategorySubtree(parent:any): Promise<any> {
132+
const children = await this.api.getCategoriesByParentId(parent.id).catch(() => []);
133+
const childList = Array.isArray(children) ? children : [];
134+
const resolvedChildren = await Promise.all(
135+
childList.map((child: any) => this.loadCategorySubtree(child))
136+
);
137+
138+
return {
139+
...parent,
140+
children: resolvedChildren
141+
};
172142
}
173143

174144
toggleGeneral(){

0 commit comments

Comments
 (0)