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 { 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) {

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 { 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<ResourceType[]> {
return this.client.get<ResourceType[]>("/ajax/resourceTypes");
}
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'
loadResourceTypes(onSuccess: Function): void {
this.client.get<ResourceType[]>("/ajax/resourceTypes").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
saveSimpleResourceMedatata(res:SimpleResource):Observable<void> {
return this.client.post<void>('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res);
loadResourceType(id:string, onSuccess: Function): void {
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')
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')
let body = new HttpParams()
.set('name', name)
.set('type', type)
.set('description', description)
.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> {
return this.client.delete<void>('/ajax/resources/' + encodeURIComponent(res.id));
deleteSimpleResource(res:SimpleResource, onSuccess: Function): void {
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();
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); }
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[] = [];
constructor(public service:ISService) {
this.service.loadResourceTypes().subscribe((data:ResourceType[]) => this.resTypes = data);
this.service.loadResourceTypes((data:ResourceType[]) => this.resTypes = data);
}
}

View File

@ -45,7 +45,7 @@ export class MainMenuTreeComponent {
this.isExpandable,
this.getChildren);
this.service.loadResourceTypes().subscribe((data:ResourceType[]) => {
this.service.loadResourceTypes((data:ResourceType[]) => {
let simpleResources: MenuNode[] = []
let advancedResources: MenuNode[] = []
@ -72,7 +72,7 @@ export class MainMenuTreeComponent {
}
})
this.dataSource.data = menuData;
})
});
this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable);
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);

View File

@ -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[]) => {
constructor(public service: ISService) {
this.service.loadProtocols((data: Protocol[]) =>
data.forEach(p => {
this.protDatasources.push({
protocol : p.id,
datasource : new MatTableDataSource(p.params)
protocol: p.id,
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 { 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,9 +60,7 @@ export class ResourcesComponent implements OnInit {
}
openContentDialog(r:SimpleResource): void {
this.service.loadSimpleResourceContent(r.id).subscribe({
next: (data: string) => {
this.service.loadSimpleResourceContent(r.id, (data: string) => {
const dialogRef = this.contentDialog.open(ResContentDialog, {
data: {
id: r.id,
@ -83,19 +73,13 @@ export class ResourcesComponent implements OnInit {
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
},
error: error => this.utils.snackError(error)
});
}
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<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);
}
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<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);
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<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 {
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();

View File

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