01c6d1e473842dffecb67a47fc02d80c8c388d2a
[src/xds/xds-agent.git] / webapp / src / app / pages / build / build-settings-modal / build-settings-modal.component.ts
1 import { Component, Input, OnInit } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { FormControl, FormGroup, Validators, ValidationErrors, FormBuilder, ValidatorFn, AbstractControl } from '@angular/forms';
5
6 import { AlertService } from '../../../@core-xds/services/alert.service';
7 import { ProjectService, IProject } from '../../../@core-xds/services/project.service';
8
9
10 @Component({
11   selector: 'xds-build-settings-modal',
12   templateUrl: 'build-settings-modal.component.html',
13 })
14
15 export class BuildSettingsModalComponent implements OnInit {
16   // @Input('server-id') serverID: string;
17   private serverID: string;
18
19   closeAction = false;
20   userEditedLabel = false;
21
22   settingsProjectForm: FormGroup;
23   subpathCtrl = new FormControl('', Validators.nullValidator);
24
25   private curPrj: IProject;
26
27   constructor(
28     private alert: AlertService,
29     private projectSvr: ProjectService,
30     private fb: FormBuilder,
31     private activeModal: NgbActiveModal,
32   ) {
33     this.settingsProjectForm = fb.group({
34       subpath: this.subpathCtrl,
35       cmdClean: ['', Validators.required],
36       cmdPrebuild: ['', Validators.nullValidator],
37       cmdBuild: ['', Validators.required],
38       cmdPopulate: ['', Validators.nullValidator],
39       cmdArgs: ['', Validators.nullValidator],
40       envVars: ['', Validators.nullValidator],
41     });
42   }
43
44   ngOnInit() {
45     this.curPrj = this.projectSvr.getCurrent();
46     this.settingsProjectForm.patchValue(this.curPrj.uiSettings);
47   }
48
49   closeModal() {
50     this.activeModal.close();
51   }
52
53   resetDefault() {
54     this.settingsProjectForm.patchValue(this.projectSvr.getDefaultSettings());
55   }
56
57   onSubmit() {
58     if (!this.closeAction) {
59       return;
60     }
61
62     this.curPrj.uiSettings = this.settingsProjectForm.value;
63     this.projectSvr.setSettings(this.curPrj)
64     .subscribe(prj => {
65       this.alert.info('Settings of project "' + prj.label + '" successfully updated.');
66       this.closeModal();
67
68       // Reset Value for the next creation
69       this.settingsProjectForm.reset();
70     },
71     err => {
72       this.alert.error(err, 60);
73       this.closeModal();
74     });
75   }
76
77 }