Skip to content
Snippets Groups Projects
Commit 2c559bb6 authored by kbeyro's avatar kbeyro
Browse files

Add json validation syntax

parent 1017ab09
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,10 @@ ...@@ -25,6 +25,10 @@
{{error}} {{error}}
</div> </div>
<div *ngIf="JsonError" style="color: indianred">
{{'APPS_MANAGEMENT.JSON_ERROR' | translate}}
</div>
</div> </div>
<div class="nmaas-modal-footer"> <div class="nmaas-modal-footer">
......
import {Component, OnInit, ViewChild} from '@angular/core'; import {Component, OnInit, ViewChild} from '@angular/core';
import {ModalComponent} from '../../../shared'; import {ModalComponent} from '../../../shared';
import {AppsService} from '../../../service'; import {AppsService} from '../../../service';
import {HttpHeaders} from '@angular/common/http';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
@Component({ @Component({
selector: 'app-app-add-json-app', selector: 'app-app-add-json-app',
templateUrl: './app-add-json-app.component.html', templateUrl: './app-add-json-app.component.html',
styleUrls: ['./app-add-json-app.component.css'] styleUrls: ['./app-add-json-app.component.css']
}) })
export class AppAddJsonAppComponent implements OnInit { export class AppAddJsonAppComponent implements OnInit {
@ViewChild(ModalComponent, { static: true }) @ViewChild(ModalComponent, {static: true})
public readonly modal: ModalComponent; public readonly modal: ModalComponent;
public jsonText = ''; public jsonText = '';
public error = ''; public error = '';
public JsonError = false;
constructor(private readonly appsService: AppsService, constructor(private readonly appsService: AppsService,
private readonly router: Router) { } private readonly router: Router) {
}
ngOnInit(): void {
}
onUpload(event: any) { ngOnInit(): void {
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
})
}
} }
fileReader.onerror = (error) => {
console.log(error);
}
}
public sendJsonText() { onUpload(event: any) {
if (this.jsonText.length > 0) { console.log(event);
this.appsService.createApplicationDTO(JSON.parse(this.jsonText)).subscribe(result => { const file = event.files[0];
console.log('uploaded', result); console.log(file);
this.modal.hide(); const fileReader = new FileReader();
this.router.navigate(['apps', result.id]) fileReader.readAsText(file);
}, fileReader.onload = () => {
error => { if (typeof fileReader.result === 'string') {
console.log(error) try {
this.error = error.message 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() { public show() {
this.modal.show(); this.modal.show();
} }
} }
...@@ -16,6 +16,9 @@ export class AppAddJsonVersionAppComponent implements OnInit { ...@@ -16,6 +16,9 @@ export class AppAddJsonVersionAppComponent implements OnInit {
public jsonText = ''; public jsonText = '';
public error = ''; public error = '';
public JsonError = false;
@Output() @Output()
public refresh: EventEmitter<boolean> = new EventEmitter<boolean>(); public refresh: EventEmitter<boolean> = new EventEmitter<boolean>();
...@@ -36,15 +39,21 @@ export class AppAddJsonVersionAppComponent implements OnInit { ...@@ -36,15 +39,21 @@ export class AppAddJsonVersionAppComponent implements OnInit {
fileReader.onload = () => { fileReader.onload = () => {
if (typeof fileReader.result === 'string') { if (typeof fileReader.result === 'string') {
console.log(JSON.parse(fileReader.result)); console.log(JSON.parse(fileReader.result));
this.appsService.createApplication(JSON.parse(fileReader.result)).subscribe(result => { try {
console.log('uploaded', result); JSON.parse(fileReader.result);
this.modal.hide(); this.appsService.createApplication(JSON.parse(fileReader.result)).subscribe(result => {
console.log('uploaded', result);
this.modal.hide();
this.refresh.emit(true); this.refresh.emit(true);
}, },
error => { error => {
console.log(error) console.log(error)
this.error = error.message this.error = error.message
}) })
} catch (e) {
console.warn('invalid json')
this.JsonError = true;
}
} }
} }
fileReader.onerror = (error) => { fileReader.onerror = (error) => {
...@@ -54,16 +63,21 @@ export class AppAddJsonVersionAppComponent implements OnInit { ...@@ -54,16 +63,21 @@ export class AppAddJsonVersionAppComponent implements OnInit {
public sendJsonText() { public sendJsonText() {
if (this.jsonText.length > 0) { if (this.jsonText.length > 0) {
this.appsService.createApplication(JSON.parse(this.jsonText)).subscribe(result => { try {
console.log('uploaded', result); JSON.parse(this.jsonText);
this.modal.hide(); this.appsService.createApplication(JSON.parse(this.jsonText)).subscribe(result => {
this.refresh.emit(true); console.log('uploaded', result);
}, this.modal.hide();
error => { this.refresh.emit(true);
console.log(error) },
this.error = error.message error => {
}) console.log(error)
this.error = error.message
})
} catch (e) {
console.warn('invalid json')
this.JsonError = true;
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment