From 2c559bb60aba85add1ab4b8fb70ea28b4fc14284 Mon Sep 17 00:00:00 2001 From: kbeyro <121854496+kbeyro@users.noreply.github.com> Date: Thu, 22 Jun 2023 10:26:31 +0200 Subject: [PATCH] Add json validation syntax --- .../app-add-json-app.component.html | 4 + .../app-add-json-app.component.ts | 114 ++++++++++-------- .../app-add-json-version-app.component.ts | 50 +++++--- 3 files changed, 98 insertions(+), 70 deletions(-) diff --git a/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.html b/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.html index 308e3ad2..a89537de 100644 --- a/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.html +++ b/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.html @@ -25,6 +25,10 @@ {{error}} </div> + <div *ngIf="JsonError" style="color: indianred"> + {{'APPS_MANAGEMENT.JSON_ERROR' | translate}} + </div> + </div> <div class="nmaas-modal-footer"> diff --git a/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.ts b/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.ts index 595cfe1c..62638c29 100644 --- a/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.ts +++ b/src/app/appmarket/appmanagement/app-add-json-app/app-add-json-app.component.ts @@ -1,75 +1,85 @@ import {Component, OnInit, ViewChild} from '@angular/core'; import {ModalComponent} from '../../../shared'; import {AppsService} from '../../../service'; -import {HttpHeaders} from '@angular/common/http'; import {Router} from '@angular/router'; @Component({ - selector: 'app-app-add-json-app', - templateUrl: './app-add-json-app.component.html', - styleUrls: ['./app-add-json-app.component.css'] + selector: 'app-app-add-json-app', + templateUrl: './app-add-json-app.component.html', + styleUrls: ['./app-add-json-app.component.css'] }) export class AppAddJsonAppComponent implements OnInit { - @ViewChild(ModalComponent, { static: true }) - public readonly modal: ModalComponent; + @ViewChild(ModalComponent, {static: true}) + public readonly modal: ModalComponent; - public jsonText = ''; - public error = ''; + public jsonText = ''; + public error = ''; + public JsonError = false; - constructor(private readonly appsService: AppsService, - private readonly router: Router) { } - - + constructor(private readonly appsService: AppsService, + private readonly router: Router) { + } - ngOnInit(): void { - } - onUpload(event: any) { - console.log(event); - const file = event.files[0]; - console.log(file); - const fileReader = new FileReader(); - fileReader.readAsText(file); - fileReader.onload = () => { - if (typeof fileReader.result === 'string') { - console.log(JSON.parse(fileReader.result)); - this.appsService.createApplicationDTO(JSON.parse(fileReader.result)).subscribe(result => { - console.log('uploaded', result); - this.modal.hide(); - this.router.navigate(['apps', result.id]) - }, - error => { - console.log(error) - this.error = error.message - }) - } + ngOnInit(): void { } - fileReader.onerror = (error) => { - console.log(error); - } - } - public sendJsonText() { - if (this.jsonText.length > 0) { - this.appsService.createApplicationDTO(JSON.parse(this.jsonText)).subscribe(result => { - console.log('uploaded', result); - this.modal.hide(); - this.router.navigate(['apps', result.id]) - }, - error => { - console.log(error) - this.error = error.message - }) + onUpload(event: any) { + console.log(event); + const file = event.files[0]; + console.log(file); + const fileReader = new FileReader(); + fileReader.readAsText(file); + fileReader.onload = () => { + if (typeof fileReader.result === 'string') { + try { + JSON.parse(fileReader.result); + this.appsService.createApplicationDTO(JSON.parse(fileReader.result)).subscribe(result => { + console.log('uploaded', result); + this.modal.hide(); + this.router.navigate(['apps', result.id]) + }, + error => { + console.log(error) + this.error = error.message + }) + } catch (e) { + console.warn('invalid json') + this.JsonError = true; + } + } + } + fileReader.onerror = (error) => { + console.log(error); + } + } + public sendJsonText() { + if (this.jsonText.length > 0) { + try { + JSON.parse(this.jsonText); + this.appsService.createApplicationDTO(JSON.parse(this.jsonText)).subscribe(result => { + console.log('uploaded', result); + this.modal.hide(); + this.router.navigate(['apps', result.id]) + }, + error => { + console.log(error) + this.error = error.message + }) + } catch (e) { + console.warn('invalid json') + this.JsonError = true; + } + } } - } - public show() { - this.modal.show(); - } + public show() { + this.modal.show(); + } } diff --git a/src/app/appmarket/appmanagement/app-add-json-version-app/app-add-json-version-app.component.ts b/src/app/appmarket/appmanagement/app-add-json-version-app/app-add-json-version-app.component.ts index 49986732..2fce47d0 100644 --- a/src/app/appmarket/appmanagement/app-add-json-version-app/app-add-json-version-app.component.ts +++ b/src/app/appmarket/appmanagement/app-add-json-version-app/app-add-json-version-app.component.ts @@ -16,6 +16,9 @@ export class AppAddJsonVersionAppComponent implements OnInit { public jsonText = ''; public error = ''; + public JsonError = false; + + @Output() public refresh: EventEmitter<boolean> = new EventEmitter<boolean>(); @@ -36,15 +39,21 @@ export class AppAddJsonVersionAppComponent implements OnInit { fileReader.onload = () => { if (typeof fileReader.result === 'string') { console.log(JSON.parse(fileReader.result)); - this.appsService.createApplication(JSON.parse(fileReader.result)).subscribe(result => { - console.log('uploaded', result); - this.modal.hide(); + try { + JSON.parse(fileReader.result); + this.appsService.createApplication(JSON.parse(fileReader.result)).subscribe(result => { + console.log('uploaded', result); + this.modal.hide(); this.refresh.emit(true); - }, - error => { - console.log(error) - this.error = error.message - }) + }, + error => { + console.log(error) + this.error = error.message + }) + } catch (e) { + console.warn('invalid json') + this.JsonError = true; + } } } fileReader.onerror = (error) => { @@ -54,16 +63,21 @@ export class AppAddJsonVersionAppComponent implements OnInit { public sendJsonText() { if (this.jsonText.length > 0) { - this.appsService.createApplication(JSON.parse(this.jsonText)).subscribe(result => { - console.log('uploaded', result); - this.modal.hide(); - this.refresh.emit(true); - }, - error => { - console.log(error) - this.error = error.message - }) - + try { + JSON.parse(this.jsonText); + this.appsService.createApplication(JSON.parse(this.jsonText)).subscribe(result => { + console.log('uploaded', result); + this.modal.hide(); + this.refresh.emit(true); + }, + error => { + console.log(error) + this.error = error.message + }) + } catch (e) { + console.warn('invalid json') + this.JsonError = true; + } } } -- GitLab