Refreshing list of templates

This commit is contained in:
piotrpekala7 2020-06-22 18:15:50 +02:00
parent fc6a5671fb
commit 7b1c5deff5
3 changed files with 27 additions and 7 deletions

View File

@ -19,6 +19,8 @@ import { IosTemplate } from '../../../models/templates/ios-template';
import { IosService } from '../../../services/ios.service'; import { IosService } from '../../../services/ios.service';
import { IouService } from '../../../services/iou.service'; import { IouService } from '../../../services/iou.service';
import { IouTemplate } from '../../../models/templates/iou-template'; import { IouTemplate } from '../../../models/templates/iou-template';
import { TemplateService } from '../../../services/template.service';
import { Template } from '../../../models/template';
@Component({ @Component({
selector: 'app-new-template-dialog', selector: 'app-new-template-dialog',
@ -73,6 +75,7 @@ export class NewTemplateDialogComponent implements OnInit {
private dockerService: DockerService, private dockerService: DockerService,
private iosService: IosService, private iosService: IosService,
private iouService: IouService, private iouService: IouService,
private templateService: TemplateService,
public dialog: MatDialog public dialog: MatDialog
) {} ) {}
@ -274,7 +277,8 @@ export class NewTemplateDialogComponent implements OnInit {
iouTemplate.path = image.filename; iouTemplate.path = image.filename;
iouTemplate.template_type = 'iou'; iouTemplate.template_type = 'iou';
this.iouService.addTemplate(this.server, iouTemplate).subscribe(() => { this.iouService.addTemplate(this.server, iouTemplate).subscribe((template) => {
this.templateService.newTemplateCreated.next(template);
this.toasterService.success('Template added'); this.toasterService.success('Template added');
this.dialogRef.close(); this.dialogRef.close();
}); });
@ -305,7 +309,8 @@ export class NewTemplateDialogComponent implements OnInit {
iosTemplate.image = image.filename; iosTemplate.image = image.filename;
iosTemplate.template_type = 'dynamips'; iosTemplate.template_type = 'dynamips';
this.iosService.addTemplate(this.server, iosTemplate).subscribe(() => { this.iosService.addTemplate(this.server, iosTemplate).subscribe((template) => {
this.templateService.newTemplateCreated.next(template as any as Template);
this.toasterService.success('Template added'); this.toasterService.success('Template added');
this.dialogRef.close(); this.dialogRef.close();
}); });
@ -325,7 +330,8 @@ export class NewTemplateDialogComponent implements OnInit {
dockerTemplate.image = this.applianceToInstall.docker.image; dockerTemplate.image = this.applianceToInstall.docker.image;
dockerTemplate.template_type = 'docker'; dockerTemplate.template_type = 'docker';
this.dockerService.addTemplate(this.server, dockerTemplate).subscribe(() => { this.dockerService.addTemplate(this.server, dockerTemplate).subscribe((template) => {
this.templateService.newTemplateCreated.next(template as any as Template);
this.toasterService.success('Template added'); this.toasterService.success('Template added');
this.dialogRef.close(); this.dialogRef.close();
}); });
@ -355,7 +361,8 @@ export class NewTemplateDialogComponent implements OnInit {
qemuTemplate.hda_disk_image = image.filename; qemuTemplate.hda_disk_image = image.filename;
qemuTemplate.template_type = 'qemu'; qemuTemplate.template_type = 'qemu';
this.qemuService.addTemplate(this.server, qemuTemplate).subscribe(() => { this.qemuService.addTemplate(this.server, qemuTemplate).subscribe((template) => {
this.templateService.newTemplateCreated.next(template as any as Template);
this.toasterService.success('Template added'); this.toasterService.success('Template added');
this.dialogRef.close(); this.dialogRef.close();
}); });

View File

@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output, OnDestroy } from '@angular/core';
import { MatDialog } from '@angular/material'; import { MatDialog } from '@angular/material';
import { TemplateListDialogComponent, NodeAddedEvent } from './template-list-dialog/template-list-dialog.component'; import { TemplateListDialogComponent, NodeAddedEvent } from './template-list-dialog/template-list-dialog.component';
@ -8,13 +8,14 @@ import { Project } from '../../models/project';
import { TemplateService } from '../../services/template.service'; import { TemplateService } from '../../services/template.service';
import { MapScaleService } from '../../services/mapScale.service'; import { MapScaleService } from '../../services/mapScale.service';
import { SymbolService } from '../../services/symbol.service'; import { SymbolService } from '../../services/symbol.service';
import { Subscription } from 'rxjs';
@Component({ @Component({
selector: 'app-template', selector: 'app-template',
templateUrl: './template.component.html', templateUrl: './template.component.html',
styleUrls: ['./template.component.scss'] styleUrls: ['./template.component.scss']
}) })
export class TemplateComponent implements OnInit { export class TemplateComponent implements OnInit, OnDestroy {
@Input() server: Server; @Input() server: Server;
@Input() project: Project; @Input() project: Project;
@Output() onNodeCreation = new EventEmitter<any>(); @Output() onNodeCreation = new EventEmitter<any>();
@ -31,6 +32,8 @@ export class TemplateComponent implements OnInit {
startX: number; startX: number;
startY: number; startY: number;
private subscription: Subscription;
constructor( constructor(
private dialog: MatDialog, private dialog: MatDialog,
private templateService: TemplateService, private templateService: TemplateService,
@ -39,6 +42,10 @@ export class TemplateComponent implements OnInit {
) {} ) {}
ngOnInit() { ngOnInit() {
this.subscription = this.templateService.newTemplateCreated.subscribe((template: Template) => {
this.templates.push(template);
});
this.templateService.list(this.server).subscribe((listOfTemplates: Template[]) => { this.templateService.list(this.server).subscribe((listOfTemplates: Template[]) => {
this.filteredTemplates = listOfTemplates; this.filteredTemplates = listOfTemplates;
this.templates = listOfTemplates; this.templates = listOfTemplates;
@ -105,4 +112,8 @@ export class TemplateComponent implements OnInit {
getImageSourceForTemplate(template: Template) { getImageSourceForTemplate(template: Template) {
return `http://${this.server.host}:${this.server.port}/v2/symbols/${template.symbol}/raw`; return `http://${this.server.host}:${this.server.port}/v2/symbols/${template.symbol}/raw`;
} }
ngOnDestroy() {
this.subscription.unsubscribe();
}
} }

View File

@ -4,11 +4,13 @@ import 'rxjs/add/operator/map';
import { Server } from '../models/server'; import { Server } from '../models/server';
import { HttpServer } from './http-server.service'; import { HttpServer } from './http-server.service';
import { Template } from '../models/template'; import { Template } from '../models/template';
import { Observable } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { QemuTemplate } from '../models/templates/qemu-template'; import { QemuTemplate } from '../models/templates/qemu-template';
@Injectable() @Injectable()
export class TemplateService { export class TemplateService {
public newTemplateCreated: Subject<Template> = new Subject<Template>();
constructor(private httpServer: HttpServer) {} constructor(private httpServer: HttpServer) {}
list(server: Server): Observable<Template[]> { list(server: Server): Observable<Template[]> {