1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { HostService } from '../../../../shared/api/host.service';
6 import { ActionLabelsI18n, URLVerbs } from '../../../../shared/constants/app.constants';
7 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
8 import { CdValidators } from '../../../../shared/forms/cd-validators';
9 import { FinishedTask } from '../../../../shared/models/finished-task';
10 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
13 selector: 'cd-host-form',
14 templateUrl: './host-form.component.html',
15 styleUrls: ['./host-form.component.scss']
17 export class HostFormComponent implements OnInit {
18 hostForm: CdFormGroup;
25 private router: Router,
27 private actionLabels: ActionLabelsI18n,
28 private hostService: HostService,
29 private taskWrapper: TaskWrapperService
31 this.resource = this.i18n('host');
32 this.action = this.actionLabels.CREATE;
37 this.hostService.list().subscribe((resp: any[]) => {
38 this.hostnames = resp.map((host) => {
39 return host['hostname'];
45 private createForm() {
46 this.hostForm = new CdFormGroup({
47 hostname: new FormControl('', {
50 CdValidators.custom('uniqueName', (hostname: string) => {
51 return this.hostnames && this.hostnames.indexOf(hostname) !== -1;
59 const hostname = this.hostForm.get('hostname').value;
62 task: new FinishedTask('host/' + URLVerbs.CREATE, {
65 call: this.hostService.create(hostname)
70 this.hostForm.setErrors({ cdSubmitButton: true });
73 this.router.navigate(['/hosts']);