|
| 1 | +import { Component, Input, OnInit } from '@angular/core'; |
| 2 | +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 3 | +import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 4 | +import { Observable, of } from 'rxjs'; |
| 5 | +import { catchError, debounceTime, distinctUntilChanged, map, switchMap, take, tap } from 'rxjs/operators'; |
| 6 | +import { CollectorService } from 'src/app/shared/collector.service'; |
| 7 | +import { DashboardService } from 'src/app/shared/dashboard.service'; |
| 8 | +import { DockerService } from '../docker.service'; |
| 9 | + |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'app-docker-config-form', |
| 13 | + templateUrl: './docker-config-form.component.html', |
| 14 | + styleUrls: ['./docker-config-form.component.sass'] |
| 15 | +}) |
| 16 | +export class DockerConfigFormComponent implements OnInit { |
| 17 | + |
| 18 | + private widgetConfigId: string; |
| 19 | + private componentId: string; |
| 20 | + private collectorId : string; |
| 21 | + private collectorItemId: string; |
| 22 | + dashboard: any; |
| 23 | + dockerConfigForm: FormGroup; |
| 24 | + |
| 25 | + @Input() |
| 26 | + set widgetConfig(widgetConfig: any) { |
| 27 | + this.widgetConfigId = widgetConfig.options.id; |
| 28 | + this.dockerConfigForm.get('instanceURL').setValue(widgetConfig.options.instanceURL); |
| 29 | + this.dockerConfigForm.get('instancePort').setValue(widgetConfig.options.instancePort); |
| 30 | + this.dockerConfigForm.get('apiToken').setValue(widgetConfig.options.apiToken); |
| 31 | + this.dockerConfigForm.get('description').setValue(widgetConfig.options.description); |
| 32 | + } |
| 33 | + |
| 34 | + constructor( |
| 35 | + public activeModal: NgbActiveModal, |
| 36 | + private formBuilder: FormBuilder, |
| 37 | + private collectorService: CollectorService, |
| 38 | + private dashboardService: DashboardService, |
| 39 | + private dockerService: DockerService) { |
| 40 | + this.createForm(); |
| 41 | + } |
| 42 | + |
| 43 | + ngOnInit() { |
| 44 | + this.loadSavedDockerJob(); |
| 45 | + } |
| 46 | + |
| 47 | + ngAfterViewInit() { |
| 48 | + this.getDashboardComponent(); |
| 49 | + } |
| 50 | + |
| 51 | + private createForm() { |
| 52 | + this.dockerConfigForm = this.formBuilder.group({ |
| 53 | + instanceURL: ['', Validators.required], |
| 54 | + instancePort: ['', Validators.required], |
| 55 | + apiToken: [''], |
| 56 | + description: [''], |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + private submitForm() { |
| 61 | + |
| 62 | + const newConfig = { |
| 63 | + name: 'Docker', |
| 64 | + options: { |
| 65 | + id: this.widgetConfigId, |
| 66 | + instanceURL: this.dockerConfigForm.value.instanceURL, |
| 67 | + instancePort: this.dockerConfigForm.value.instancePort, |
| 68 | + apiToken: this.dockerConfigForm.value.apiToken, |
| 69 | + }, |
| 70 | + componentId: this.componentId, |
| 71 | + collectorItemId: this.collectorItemId |
| 72 | + }; |
| 73 | + |
| 74 | + this.activeModal.close(newConfig); |
| 75 | + } |
| 76 | + |
| 77 | + private loadSavedDockerJob() { |
| 78 | + this.dashboardService.dashboardConfig$.pipe(take(1), |
| 79 | + map(dashboard => { |
| 80 | + const dockerCollector = dashboard.application.components[0].collectorItems.Docker; |
| 81 | + if (dockerCollector) { |
| 82 | + const dockerId = dockerCollector[0].id; |
| 83 | + return dockerId; |
| 84 | + } |
| 85 | + return null; |
| 86 | + }), |
| 87 | + switchMap(dockerId => { |
| 88 | + if (dockerId) { |
| 89 | + return this.collectorService.getItemsById(dockerId); |
| 90 | + } |
| 91 | + return of(null); |
| 92 | + })).subscribe(collectorData => { |
| 93 | + this.collectorItemId = collectorData.id; |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + private getDashboardComponent() { |
| 98 | + this.dashboardService.dashboardConfig$.pipe(take(1), |
| 99 | + map(dashboard => { |
| 100 | + this.dashboard = dashboard; |
| 101 | + return dashboard.application.components[0].id; |
| 102 | + })).subscribe(componentId => this.componentId = componentId); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments