reimplemented ajax callback

This commit is contained in:
Michele Artini 2023-01-27 08:58:33 +01:00
parent e62329b5d9
commit 448bcb4e86
9 changed files with 162 additions and 201 deletions

View File

@ -1,6 +1,5 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { ISService } from '../is.service'; import { ISService } from '../is.service';
import { ISUtilsService } from '../is-utils.service';
import { MatTableDataSource } from '@angular/material/table'; import { MatTableDataSource } from '@angular/material/table';
import { KeyValue, Module } from '../model/controller.model'; import { KeyValue, Module } from '../model/controller.model';
@ -22,9 +21,8 @@ export class InfoComponent {
displayedKVColumns: string[] = ['k', 'v']; displayedKVColumns: string[] = ['k', 'v'];
displayedModuleColumns: string[] = ['group', 'name', 'versions', 'files']; displayedModuleColumns: string[] = ['group', 'name', 'versions', 'files'];
constructor(public service:ISService, public utils:ISUtilsService) { constructor(public service:ISService) {
this.service.loadInfo().subscribe({ this.service.loadInfo((data:any[]) => {
next:(data:any[]) => {
data.forEach(section => { data.forEach(section => {
if (section['name'] == 'Modules') { if (section['name'] == 'Modules') {
this.moduleDatasource.data = section['data']; this.moduleDatasource.data = section['data'];
@ -35,9 +33,7 @@ export class InfoComponent {
}); });
} }
}) })
}, });
error:error => this.utils.snackError(error)
})
} }
applyFilter(event: Event) { applyFilter(event: Event) {

View File

@ -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();
});
});

View File

@ -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';
}
}
}

View File

@ -2,74 +2,130 @@ import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { ResourceType,Protocol,WfHistoryEntry,SimpleResource } from './model/controller.model'; import { ResourceType,Protocol,WfHistoryEntry,SimpleResource } from './model/controller.model';
import { Observable, Observer } from 'rxjs'; import { Observable, Observer } from 'rxjs';
import { FormGroup } from '@angular/forms';
import { MatSnackBar } from '@angular/material/snack-bar';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ISService { export class ISService {
constructor(public client:HttpClient) { } constructor(public client:HttpClient, public snackBar: MatSnackBar) { }
loadResourceTypes(onSuccess: Function): void {
loadResourceTypes():Observable<ResourceType[]> { this.client.get<ResourceType[]>("/ajax/resourceTypes").subscribe({
return this.client.get<ResourceType[]>("/ajax/resourceTypes"); next: data => onSuccess(data),
} error: error => this.showError(error)
loadResourceType(id:string):Observable<ResourceType> {
return this.client.get<ResourceType>("/ajax/resourceTypes/" + encodeURIComponent(id));
}
loadInfo():Observable<any[]> {
return this.client.get<any[]>("/ajax/info/");
}
loadProtocols():Observable<Protocol[]> {
return this.client.get<Protocol[]>("/ajax/protocols/");
}
loadSimpleResources(type:string):Observable<SimpleResource[]> {
return this.client.get<SimpleResource[]>("/ajax/resources/" + encodeURIComponent(type));
}
loadSimpleResourceContent(id:any):Observable<string> {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
return this.client.get<string>("/ajax/resources/" + encodeURIComponent(id) + '/content', {
headers, responseType: 'text' as 'json'
}); });
} }
saveSimpleResourceMedatata(res:SimpleResource):Observable<void> { loadResourceType(id:string, onSuccess: Function): void {
return this.client.post<void>('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res); this.client.get<ResourceType>("/ajax/resourceTypes/" + encodeURIComponent(id)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
} }
saveSimpleResourceContent(id:string, content:string):Observable<void> { loadInfo(onSuccess: Function): void {
this.client.get<any[]>("/ajax/info/").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadProtocols(onSuccess: Function): void {
this.client.get<Protocol[]>("/ajax/protocols/").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadSimpleResources(type:string, onSuccess: Function): void {
this.client.get<SimpleResource[]>("/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<string>("/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<void>('/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') const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams().set('content', content); let body = new HttpParams().set('content', content);
return this.client.post<void>('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }); this.client.post<void>('/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<void> { 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') const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams() let body = new HttpParams()
.set('name', name) .set('name', name)
.set('type', type) .set('type', type)
.set('description', description) .set('description', description)
.set('content', content); .set('content', content);
return this.client.post<void>('/ajax/resources/', body, { headers: headers }); this.client.post<void>('/ajax/resources/', body, { headers: headers }).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
} }
deleteSimpleResource(res:SimpleResource):Observable<void> { deleteSimpleResource(res:SimpleResource, onSuccess: Function): void {
return this.client.delete<void>('/ajax/resources/' + encodeURIComponent(res.id)); this.client.delete<void>('/ajax/resources/' + encodeURIComponent(res.id)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
} }
loadWfHistory(total?:number, from?:number, to?:number):Observable<WfHistoryEntry[]> { loadWfHistory(total:number, from:number, to:number, onSuccess: Function): void {
let params = new HttpParams(); let params = new HttpParams();
if (total && total > 0) { params = params.append('total', total); } if (total && total > 0) { params = params.append('total', total); }
if (from && from > 0) { params = params.append('from', from); } if (from && from > 0) { params = params.append('from', from); }
if (to && to > 0) { params = params.append('to', to); } if (to && to > 0) { params = params.append('to', to); }
return this.client.get<WfHistoryEntry[]>('/ajax/wfs/', {params: params}); this.client.get<WfHistoryEntry[]>('/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';
}
}
} }

View File

@ -17,6 +17,6 @@ export class MainMenuPanelsComponent {
resTypes:ResourceType[] = []; resTypes:ResourceType[] = [];
constructor(public service:ISService) { constructor(public service:ISService) {
this.service.loadResourceTypes().subscribe((data:ResourceType[]) => this.resTypes = data); this.service.loadResourceTypes((data:ResourceType[]) => this.resTypes = data);
} }
} }

View File

@ -45,34 +45,34 @@ export class MainMenuTreeComponent {
this.isExpandable, this.isExpandable,
this.getChildren); this.getChildren);
this.service.loadResourceTypes().subscribe((data:ResourceType[]) => { this.service.loadResourceTypes((data:ResourceType[]) => {
let simpleResources: MenuNode[] = [] let simpleResources: MenuNode[] = []
let advancedResources: 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 => { menuData.forEach(m => {
let item:MenuNode = { if (m.name=='Simple Resources') {
name: resType.name, m.children = simpleResources
type: 'menuitem', } else if (m.name=='Advanced Resources') {
badge: resType.count.toString() m.children = advancedResources
} }
if (resType.simple) { })
item.route = '/resources/' + resType.id; this.dataSource.data = menuData;
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;
})
this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable); this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable);
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);

View File

@ -1,6 +1,5 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { ISService } from '../is.service'; import { ISService } from '../is.service';
import { ISUtilsService } from '../is-utils.service';
import { MatTableDataSource } from '@angular/material/table'; import { MatTableDataSource } from '@angular/material/table';
import { Protocol, ProtocolParams } from '../model/controller.model'; import { Protocol, ProtocolParams } from '../model/controller.model';
@ -15,21 +14,18 @@ export interface ProtocolDatasource {
styleUrls: ['./protocols.component.css'] styleUrls: ['./protocols.component.css']
}) })
export class ProtocolsComponent { export class ProtocolsComponent {
protDatasources:ProtocolDatasource[] = []; protDatasources: ProtocolDatasource[] = [];
colums : string[] = ['name', 'label', 'type', 'optional', 'hasSelFunction']; colums: string[] = ['name', 'label', 'type', 'optional', 'hasSelFunction'];
constructor(public service:ISService, public utils:ISUtilsService) { constructor(public service: ISService) {
this.service.loadProtocols().subscribe({ this.service.loadProtocols((data: Protocol[]) =>
next:(data:Protocol[]) => { data.forEach(p => {
data.forEach(p => { this.protDatasources.push({
this.protDatasources.push({ protocol: p.id,
protocol : p.id, datasource: new MatTableDataSource(p.params)
datasource : new MatTableDataSource(p.params) });
}); })
}) );
},
error:error => this.utils.snackError(error)
})
} }
} }

View File

@ -1,10 +1,8 @@
import { Component, Inject,AfterViewInit, ViewChild, OnInit } from '@angular/core'; import { Component, Inject,AfterViewInit, ViewChild, OnInit } from '@angular/core';
import { ISService } from '../is.service'; import { ISService } from '../is.service';
import { ISUtilsService } from '../is-utils.service';
import { MatTableDataSource } from '@angular/material/table'; import { MatTableDataSource } from '@angular/material/table';
import { MatSort, Sort } from '@angular/material/sort'; import { MatSort, Sort } from '@angular/material/sort';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ResourceType, SimpleResource } from '../model/controller.model'; import { ResourceType, SimpleResource } from '../model/controller.model';
@ -22,26 +20,20 @@ export class ResourcesComponent implements OnInit {
resources:SimpleResource[] = []; resources:SimpleResource[] = [];
searchText:string = ''; 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() { ngOnInit() {
this.route.params.subscribe(params => { this.route.params.subscribe(params => {
this.typeId = params['type']; this.typeId = params['type'];
this.service.loadResourceType(this.typeId).subscribe({ this.service.loadResourceType(this.typeId, (data: ResourceType) => this.type = data);
next: (data: ResourceType) => this.type = data,
error: error => this.utils.snackError(error)
});
this.reload() this.reload()
}); });
} }
reload() { reload() {
if (this.typeId) { if (this.typeId) {
this.service.loadSimpleResources(this.typeId).subscribe({ this.service.loadSimpleResources(this.typeId, (data: SimpleResource[]) => this.resources = data);
next: (data: SimpleResource[]) => this.resources = data,
error: error => this.utils.snackError(error)
});
} }
} }
@ -68,34 +60,26 @@ export class ResourcesComponent implements OnInit {
} }
openContentDialog(r:SimpleResource): void { openContentDialog(r:SimpleResource): void {
this.service.loadSimpleResourceContent(r.id).subscribe({ this.service.loadSimpleResourceContent(r.id, (data: string) => {
next: (data: string) => { const dialogRef = this.contentDialog.open(ResContentDialog, {
data: {
const dialogRef = this.contentDialog.open(ResContentDialog, { id: r.id,
data: { contentType: this.type.contentType,
id: r.id, content: data
contentType: this.type.contentType, },
content: data width: '80%'
}, });
width: '80%'
}); dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
dialogRef.afterClosed().subscribe(result => { });
if (result) this.reload();
});
},
error: error => this.utils.snackError(error)
}); });
} }
deleteResource(r:SimpleResource) { deleteResource(r:SimpleResource) {
if (confirm('Are you sure?')) { if (confirm('Are you sure?')) {
this.service.deleteSimpleResource(r).subscribe({ this.service.deleteSimpleResource(r, (data: void) => this.reload());
next: (data: void) => this.reload(),
error: error => this.utils.snackError(error)
});
} }
} }
} }
@ -112,19 +96,14 @@ export class ResContentDialog {
content : this.contentFormControl content : this.contentFormControl
}); });
constructor(public dialogRef: MatDialogRef<ResContentDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) { constructor(public dialogRef: MatDialogRef<ResContentDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
this.contentFormControl.setValue(data.content); this.contentFormControl.setValue(data.content);
} }
onSubmit():void { onSubmit():void {
let content = this.contentFormControl.value; let content = this.contentFormControl.value;
if (content) { if (content) {
this.service.saveSimpleResourceContent(this.data.id, content).subscribe({ this.service.saveSimpleResourceContent(this.data.id, content, (data: void) => this.dialogRef.close(1), this.contentForm)
next: (data: void) => {
this.dialogRef.close(1)
},
error: error => this.utils.prepareFormError(error, this.contentForm)
});
} }
} }
@ -144,7 +123,7 @@ export class ResMetadataDialog {
description : new FormControl('') description : new FormControl('')
}); });
constructor(public dialogRef: MatDialogRef<ResMetadataDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) { constructor(public dialogRef: MatDialogRef<ResMetadataDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
this.metadataForm.get('name')?.setValue(data.name); this.metadataForm.get('name')?.setValue(data.name);
if (data.description) { if (data.description) {
this.metadataForm.get('description')?.setValue(data.description); this.metadataForm.get('description')?.setValue(data.description);
@ -153,13 +132,7 @@ export class ResMetadataDialog {
onSubmit():void { onSubmit():void {
const res = Object.assign({}, this.data, this.metadataForm.value); const res = Object.assign({}, this.data, this.metadataForm.value);
this.service.saveSimpleResourceMedatata(res, (data: void) => this.dialogRef.close(1), this.metadataForm);
this.service.saveSimpleResourceMedatata(res).subscribe({
next: (data: void) => {
this.dialogRef.close(1)
},
error: error => this.utils.prepareFormError(error, this.metadataForm)
});
} }
onNoClick(): void { onNoClick(): void {
@ -179,7 +152,7 @@ export class ResCreateNewDialog {
content : new FormControl('') content : new FormControl('')
}); });
constructor(public dialogRef: MatDialogRef<ResCreateNewDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) {} constructor(public dialogRef: MatDialogRef<ResCreateNewDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {}
onSubmit():void { onSubmit():void {
let name:string = this.newResourceForm.get('name')?.value!; let name:string = this.newResourceForm.get('name')?.value!;
@ -187,12 +160,7 @@ export class ResCreateNewDialog {
let description:string = this.newResourceForm.get('description')?.value!; let description:string = this.newResourceForm.get('description')?.value!;
let content:string = this.newResourceForm.get('content')?.value!; let content:string = this.newResourceForm.get('content')?.value!;
this.service.addSimpleResource(name, type, description, content).subscribe({ this.service.addSimpleResource(name, type, description, content, (data: void) => this.dialogRef.close(1), this.newResourceForm);
next: (data: void) => {
this.dialogRef.close(1)
},
error: error => this.utils.prepareFormError(error, this.newResourceForm)
});
} }
onNoClick(): void { onNoClick(): void {
this.dialogRef.close(); this.dialogRef.close();

View File

@ -1,6 +1,5 @@
import { Component, Inject,AfterViewInit, OnInit, ViewChild } from '@angular/core'; import { Component, Inject,AfterViewInit, OnInit, ViewChild } from '@angular/core';
import { ISService } from '../is.service'; import { ISService } from '../is.service';
import { ISUtilsService } from '../is-utils.service';
import { MatTableDataSource } from '@angular/material/table'; import { MatTableDataSource } from '@angular/material/table';
import { MatSort, Sort } from '@angular/material/sort'; import { MatSort, Sort } from '@angular/material/sort';
import { WfHistoryEntry } from '../model/controller.model'; import { WfHistoryEntry } from '../model/controller.model';
@ -25,7 +24,7 @@ export class WfHistoryComponent implements AfterViewInit , OnInit{
from: number = -1 from: number = -1
to: 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() { ngOnInit() {
@ -41,10 +40,7 @@ export class WfHistoryComponent implements AfterViewInit , OnInit{
if (fromP) { this.from = parseInt(fromP); } if (fromP) { this.from = parseInt(fromP); }
if (toP) { this.to = parseInt(toP); } if (toP) { this.to = parseInt(toP); }
this.service.loadWfHistory(this.total, this.from, this.to).subscribe({ this.service.loadWfHistory(this.total, this.from, this.to, (data: WfHistoryEntry[]) => this.historyDatasource.data = data);
next: (data: WfHistoryEntry[]) => this.historyDatasource.data = data,
error: error => this.utils.snackError(error)
})
}); });
} }