From 448bcb4e86103d48b6ad8b17337aa76a9cf1b42a Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Fri, 27 Jan 2023 08:58:33 +0100 Subject: [PATCH] reimplemented ajax callback --- .../src/app/info/info.component.ts | 10 +- .../src/app/is-utils.service.spec.ts | 16 --- .../src/app/is-utils.service.ts | 35 ----- .../dnet-is-application/src/app/is.service.ts | 134 +++++++++++++----- .../main-menu-panels.component.ts | 2 +- .../main-menu-tree.component.ts | 54 +++---- .../src/app/protocols/protocols.component.ts | 26 ++-- .../src/app/resources/resources.component.ts | 78 +++------- .../app/wf-history/wf-history.component.ts | 8 +- 9 files changed, 162 insertions(+), 201 deletions(-) delete mode 100644 frontends/dnet-is-application/src/app/is-utils.service.spec.ts delete mode 100644 frontends/dnet-is-application/src/app/is-utils.service.ts diff --git a/frontends/dnet-is-application/src/app/info/info.component.ts b/frontends/dnet-is-application/src/app/info/info.component.ts index 37b1014f..bfdf6278 100644 --- a/frontends/dnet-is-application/src/app/info/info.component.ts +++ b/frontends/dnet-is-application/src/app/info/info.component.ts @@ -1,6 +1,5 @@ import { Component } from '@angular/core'; import { ISService } from '../is.service'; -import { ISUtilsService } from '../is-utils.service'; import { MatTableDataSource } from '@angular/material/table'; import { KeyValue, Module } from '../model/controller.model'; @@ -22,9 +21,8 @@ export class InfoComponent { displayedKVColumns: string[] = ['k', 'v']; displayedModuleColumns: string[] = ['group', 'name', 'versions', 'files']; - constructor(public service:ISService, public utils:ISUtilsService) { - this.service.loadInfo().subscribe({ - next:(data:any[]) => { + constructor(public service:ISService) { + this.service.loadInfo((data:any[]) => { data.forEach(section => { if (section['name'] == 'Modules') { this.moduleDatasource.data = section['data']; @@ -35,9 +33,7 @@ export class InfoComponent { }); } }) - }, - error:error => this.utils.snackError(error) - }) + }); } applyFilter(event: Event) { diff --git a/frontends/dnet-is-application/src/app/is-utils.service.spec.ts b/frontends/dnet-is-application/src/app/is-utils.service.spec.ts deleted file mode 100644 index d2561a49..00000000 --- a/frontends/dnet-is-application/src/app/is-utils.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { IsUtilsService } from './is-utils.service'; - -describe('IsUtilsService', () => { - let service: IsUtilsService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(IsUtilsService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/frontends/dnet-is-application/src/app/is-utils.service.ts b/frontends/dnet-is-application/src/app/is-utils.service.ts deleted file mode 100644 index 434cfe58..00000000 --- a/frontends/dnet-is-application/src/app/is-utils.service.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Inject, Injectable } from '@angular/core'; -import { FormGroup } from '@angular/forms'; -import {MatSnackBar} from '@angular/material/snack-bar'; - -@Injectable({ - providedIn: 'root' -}) -export class ISUtilsService { - - constructor(public snackBar: MatSnackBar) { } - - prepareFormError(error:any, form:FormGroup): void { - form.setErrors({ serverError: this.errorMessage(error) }) - } - - snackError(error:any) { - this.snackBar.open(this.errorMessage(error), 'ERROR', { - duration: 5000, - }); - } - - alertError(error:any) { - alert(error); - } - - private errorMessage(error:any) { - if (error.error && error.error.message) { - return error.error.message; - } else if (error.message) { - return error.message; - } else { - return 'Generic server side error'; - } - } -} diff --git a/frontends/dnet-is-application/src/app/is.service.ts b/frontends/dnet-is-application/src/app/is.service.ts index f792625e..c7e45647 100644 --- a/frontends/dnet-is-application/src/app/is.service.ts +++ b/frontends/dnet-is-application/src/app/is.service.ts @@ -2,74 +2,130 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { ResourceType,Protocol,WfHistoryEntry,SimpleResource } from './model/controller.model'; import { Observable, Observer } from 'rxjs'; +import { FormGroup } from '@angular/forms'; +import { MatSnackBar } from '@angular/material/snack-bar'; @Injectable({ providedIn: 'root' }) export class ISService { - constructor(public client:HttpClient) { } + constructor(public client:HttpClient, public snackBar: MatSnackBar) { } - - loadResourceTypes():Observable { - return this.client.get("/ajax/resourceTypes"); - } - - loadResourceType(id:string):Observable { - return this.client.get("/ajax/resourceTypes/" + encodeURIComponent(id)); - } - - loadInfo():Observable { - return this.client.get("/ajax/info/"); - } - - loadProtocols():Observable { - return this.client.get("/ajax/protocols/"); - } - - loadSimpleResources(type:string):Observable { - return this.client.get("/ajax/resources/" + encodeURIComponent(type)); - } - - loadSimpleResourceContent(id:any):Observable { - const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8'); - return this.client.get("/ajax/resources/" + encodeURIComponent(id) + '/content', { - headers, responseType: 'text' as 'json' + loadResourceTypes(onSuccess: Function): void { + this.client.get("/ajax/resourceTypes").subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) }); } - saveSimpleResourceMedatata(res:SimpleResource):Observable { - return this.client.post('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res); + loadResourceType(id:string, onSuccess: Function): void { + this.client.get("/ajax/resourceTypes/" + encodeURIComponent(id)).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); } - saveSimpleResourceContent(id:string, content:string):Observable { + loadInfo(onSuccess: Function): void { + this.client.get("/ajax/info/").subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); + } + + loadProtocols(onSuccess: Function): void { + this.client.get("/ajax/protocols/").subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); + } + + loadSimpleResources(type:string, onSuccess: Function): void { + this.client.get("/ajax/resources/" + encodeURIComponent(type)).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); + } + + loadSimpleResourceContent(id:any, onSuccess: Function): void { + const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8'); + this.client.get("/ajax/resources/" + encodeURIComponent(id) + '/content', { + headers, responseType: 'text' as 'json' + }).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); + } + + saveSimpleResourceMedatata(res:SimpleResource, onSuccess: Function, relatedForm?:FormGroup): void { + this.client.post('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error, relatedForm) + }); + } + + saveSimpleResourceContent(id:string, content:string, onSuccess: Function, relatedForm?:FormGroup): void { const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') let body = new HttpParams().set('content', content); - return this.client.post('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }); + this.client.post('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error, relatedForm) + }); } - addSimpleResource(name:string, type:string, description:string, content:string):Observable { + addSimpleResource(name:string, type:string, description:string, content:string, onSuccess: Function, relatedForm?:FormGroup): void { const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') let body = new HttpParams() .set('name', name) .set('type', type) .set('description', description) .set('content', content); - return this.client.post('/ajax/resources/', body, { headers: headers }); + this.client.post('/ajax/resources/', body, { headers: headers }).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error, relatedForm) + }); } - deleteSimpleResource(res:SimpleResource):Observable { - return this.client.delete('/ajax/resources/' + encodeURIComponent(res.id)); + deleteSimpleResource(res:SimpleResource, onSuccess: Function): void { + this.client.delete('/ajax/resources/' + encodeURIComponent(res.id)).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); } - loadWfHistory(total?:number, from?:number, to?:number):Observable { + loadWfHistory(total:number, from:number, to:number, onSuccess: Function): void { let params = new HttpParams(); - if (total && total > 0) { params = params.append('total', total); } - if (from && from > 0) { params = params.append('from', from); } - if (to && to > 0) { params = params.append('to', to); } + if (from && from > 0) { params = params.append('from', from); } + if (to && to > 0) { params = params.append('to', to); } - return this.client.get('/ajax/wfs/', {params: params}); + this.client.get('/ajax/wfs/', {params: params}).subscribe({ + next: data => onSuccess(data), + error: error => this.showError(error) + }); } + private showError(error:any, form?:FormGroup) { + const msg = this.errorMessage(error); + if (form) { + form.setErrors({ serverError: msg }) + } else if (this.snackBar) { + this.snackBar.open(msg, 'ERROR', { + duration: 5000, + }); + } else { + alert(msg); + } + } + + private errorMessage(error:any) { + if (error.error && error.error.message) { + return error.error.message; + } else if (error.message) { + return error.message; + } else { + return 'Generic server side error'; + } + } + } diff --git a/frontends/dnet-is-application/src/app/main-menu-panels/main-menu-panels.component.ts b/frontends/dnet-is-application/src/app/main-menu-panels/main-menu-panels.component.ts index cb8a0bc2..140245f9 100644 --- a/frontends/dnet-is-application/src/app/main-menu-panels/main-menu-panels.component.ts +++ b/frontends/dnet-is-application/src/app/main-menu-panels/main-menu-panels.component.ts @@ -17,6 +17,6 @@ export class MainMenuPanelsComponent { resTypes:ResourceType[] = []; constructor(public service:ISService) { - this.service.loadResourceTypes().subscribe((data:ResourceType[]) => this.resTypes = data); + this.service.loadResourceTypes((data:ResourceType[]) => this.resTypes = data); } } diff --git a/frontends/dnet-is-application/src/app/main-menu-tree/main-menu-tree.component.ts b/frontends/dnet-is-application/src/app/main-menu-tree/main-menu-tree.component.ts index 107db5f0..08c2d7cc 100644 --- a/frontends/dnet-is-application/src/app/main-menu-tree/main-menu-tree.component.ts +++ b/frontends/dnet-is-application/src/app/main-menu-tree/main-menu-tree.component.ts @@ -45,34 +45,34 @@ export class MainMenuTreeComponent { this.isExpandable, this.getChildren); - this.service.loadResourceTypes().subscribe((data:ResourceType[]) => { - let simpleResources: MenuNode[] = [] - let advancedResources: MenuNode[] = [] + this.service.loadResourceTypes((data:ResourceType[]) => { + let simpleResources: MenuNode[] = [] + let advancedResources: MenuNode[] = [] + + data.forEach(resType => { + let item:MenuNode = { + name: resType.name, + type: 'menuitem', + badge: resType.count.toString() + } + if (resType.simple) { + item.route = '/resources/' + resType.id; + simpleResources.push(item) + } else { + item.route = '/adv_resources/' + resType.id; + advancedResources.push(item) + } + }) - data.forEach(resType => { - let item:MenuNode = { - name: resType.name, - type: 'menuitem', - badge: resType.count.toString() - } - if (resType.simple) { - item.route = '/resources/' + resType.id; - simpleResources.push(item) - } else { - item.route = '/adv_resources/' + resType.id; - advancedResources.push(item) - } - }) - - menuData.forEach(m => { - if (m.name=='Simple Resources') { - m.children = simpleResources - } else if (m.name=='Advanced Resources') { - m.children = advancedResources - } - }) - this.dataSource.data = menuData; - }) + menuData.forEach(m => { + if (m.name=='Simple Resources') { + m.children = simpleResources + } else if (m.name=='Advanced Resources') { + m.children = advancedResources + } + }) + this.dataSource.data = menuData; + }); this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable); this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); diff --git a/frontends/dnet-is-application/src/app/protocols/protocols.component.ts b/frontends/dnet-is-application/src/app/protocols/protocols.component.ts index 4589a9d7..d8388273 100644 --- a/frontends/dnet-is-application/src/app/protocols/protocols.component.ts +++ b/frontends/dnet-is-application/src/app/protocols/protocols.component.ts @@ -1,6 +1,5 @@ import { Component } from '@angular/core'; import { ISService } from '../is.service'; -import { ISUtilsService } from '../is-utils.service'; import { MatTableDataSource } from '@angular/material/table'; import { Protocol, ProtocolParams } from '../model/controller.model'; @@ -15,21 +14,18 @@ export interface ProtocolDatasource { styleUrls: ['./protocols.component.css'] }) export class ProtocolsComponent { - protDatasources:ProtocolDatasource[] = []; - colums : string[] = ['name', 'label', 'type', 'optional', 'hasSelFunction']; + protDatasources: ProtocolDatasource[] = []; + colums: string[] = ['name', 'label', 'type', 'optional', 'hasSelFunction']; - constructor(public service:ISService, public utils:ISUtilsService) { - this.service.loadProtocols().subscribe({ - next:(data:Protocol[]) => { - data.forEach(p => { - this.protDatasources.push({ - protocol : p.id, - datasource : new MatTableDataSource(p.params) - }); - }) - }, - error:error => this.utils.snackError(error) - }) + constructor(public service: ISService) { + this.service.loadProtocols((data: Protocol[]) => + data.forEach(p => { + this.protDatasources.push({ + protocol: p.id, + datasource: new MatTableDataSource(p.params) + }); + }) + ); } } diff --git a/frontends/dnet-is-application/src/app/resources/resources.component.ts b/frontends/dnet-is-application/src/app/resources/resources.component.ts index d3f90cbf..dbc84463 100644 --- a/frontends/dnet-is-application/src/app/resources/resources.component.ts +++ b/frontends/dnet-is-application/src/app/resources/resources.component.ts @@ -1,10 +1,8 @@ import { Component, Inject,AfterViewInit, ViewChild, OnInit } from '@angular/core'; import { ISService } from '../is.service'; -import { ISUtilsService } from '../is-utils.service'; import { MatTableDataSource } from '@angular/material/table'; import { MatSort, Sort } from '@angular/material/sort'; import { ActivatedRoute } from '@angular/router'; -import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { ResourceType, SimpleResource } from '../model/controller.model'; @@ -22,26 +20,20 @@ export class ResourcesComponent implements OnInit { resources:SimpleResource[] = []; searchText:string = ''; - constructor(public service: ISService, public utils:ISUtilsService, public route: ActivatedRoute, public newDialog: MatDialog, public contentDialog: MatDialog, public metadataDialog: MatDialog) { + constructor(public service: ISService, public route: ActivatedRoute, public newDialog: MatDialog, public contentDialog: MatDialog, public metadataDialog: MatDialog) { } ngOnInit() { this.route.params.subscribe(params => { this.typeId = params['type']; - this.service.loadResourceType(this.typeId).subscribe({ - next: (data: ResourceType) => this.type = data, - error: error => this.utils.snackError(error) - }); + this.service.loadResourceType(this.typeId, (data: ResourceType) => this.type = data); this.reload() }); } reload() { if (this.typeId) { - this.service.loadSimpleResources(this.typeId).subscribe({ - next: (data: SimpleResource[]) => this.resources = data, - error: error => this.utils.snackError(error) - }); + this.service.loadSimpleResources(this.typeId, (data: SimpleResource[]) => this.resources = data); } } @@ -68,34 +60,26 @@ export class ResourcesComponent implements OnInit { } openContentDialog(r:SimpleResource): void { - this.service.loadSimpleResourceContent(r.id).subscribe({ - next: (data: string) => { - - const dialogRef = this.contentDialog.open(ResContentDialog, { - data: { - id: r.id, - contentType: this.type.contentType, - content: data - }, - width: '80%' - }); - - dialogRef.afterClosed().subscribe(result => { - if (result) this.reload(); - }); - }, - error: error => this.utils.snackError(error) + this.service.loadSimpleResourceContent(r.id, (data: string) => { + const dialogRef = this.contentDialog.open(ResContentDialog, { + data: { + id: r.id, + contentType: this.type.contentType, + content: data + }, + width: '80%' + }); + + dialogRef.afterClosed().subscribe(result => { + if (result) this.reload(); + }); }); } deleteResource(r:SimpleResource) { if (confirm('Are you sure?')) { - this.service.deleteSimpleResource(r).subscribe({ - next: (data: void) => this.reload(), - error: error => this.utils.snackError(error) - }); + this.service.deleteSimpleResource(r, (data: void) => this.reload()); } - } } @@ -112,19 +96,14 @@ export class ResContentDialog { content : this.contentFormControl }); - constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) { + constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) { this.contentFormControl.setValue(data.content); } onSubmit():void { let content = this.contentFormControl.value; if (content) { - this.service.saveSimpleResourceContent(this.data.id, content).subscribe({ - next: (data: void) => { - this.dialogRef.close(1) - }, - error: error => this.utils.prepareFormError(error, this.contentForm) - }); + this.service.saveSimpleResourceContent(this.data.id, content, (data: void) => this.dialogRef.close(1), this.contentForm) } } @@ -144,7 +123,7 @@ export class ResMetadataDialog { description : new FormControl('') }); - constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) { + constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) { this.metadataForm.get('name')?.setValue(data.name); if (data.description) { this.metadataForm.get('description')?.setValue(data.description); @@ -153,13 +132,7 @@ export class ResMetadataDialog { onSubmit():void { const res = Object.assign({}, this.data, this.metadataForm.value); - - this.service.saveSimpleResourceMedatata(res).subscribe({ - next: (data: void) => { - this.dialogRef.close(1) - }, - error: error => this.utils.prepareFormError(error, this.metadataForm) - }); + this.service.saveSimpleResourceMedatata(res, (data: void) => this.dialogRef.close(1), this.metadataForm); } onNoClick(): void { @@ -179,7 +152,7 @@ export class ResCreateNewDialog { content : new FormControl('') }); - constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) {} + constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {} onSubmit():void { let name:string = this.newResourceForm.get('name')?.value!; @@ -187,12 +160,7 @@ export class ResCreateNewDialog { let description:string = this.newResourceForm.get('description')?.value!; let content:string = this.newResourceForm.get('content')?.value!; - this.service.addSimpleResource(name, type, description, content).subscribe({ - next: (data: void) => { - this.dialogRef.close(1) - }, - error: error => this.utils.prepareFormError(error, this.newResourceForm) - }); + this.service.addSimpleResource(name, type, description, content, (data: void) => this.dialogRef.close(1), this.newResourceForm); } onNoClick(): void { this.dialogRef.close(); diff --git a/frontends/dnet-is-application/src/app/wf-history/wf-history.component.ts b/frontends/dnet-is-application/src/app/wf-history/wf-history.component.ts index 7b6d2f6f..06e5307e 100644 --- a/frontends/dnet-is-application/src/app/wf-history/wf-history.component.ts +++ b/frontends/dnet-is-application/src/app/wf-history/wf-history.component.ts @@ -1,6 +1,5 @@ import { Component, Inject,AfterViewInit, OnInit, ViewChild } from '@angular/core'; import { ISService } from '../is.service'; -import { ISUtilsService } from '../is-utils.service'; import { MatTableDataSource } from '@angular/material/table'; import { MatSort, Sort } from '@angular/material/sort'; import { WfHistoryEntry } from '../model/controller.model'; @@ -25,7 +24,7 @@ export class WfHistoryComponent implements AfterViewInit , OnInit{ from: number = -1 to: number = -1 - constructor(public service: ISService, public utils:ISUtilsService, public route: ActivatedRoute, public dialog: MatDialog) { + constructor(public service: ISService, public route: ActivatedRoute, public dialog: MatDialog) { } ngOnInit() { @@ -41,10 +40,7 @@ export class WfHistoryComponent implements AfterViewInit , OnInit{ if (fromP) { this.from = parseInt(fromP); } if (toP) { this.to = parseInt(toP); } - this.service.loadWfHistory(this.total, this.from, this.to).subscribe({ - next: (data: WfHistoryEntry[]) => this.historyDatasource.data = data, - error: error => this.utils.snackError(error) - }) + this.service.loadWfHistory(this.total, this.from, this.to, (data: WfHistoryEntry[]) => this.historyDatasource.data = data); }); }