Project

General

Profile

1
import { Component, Input, OnInit } from '@angular/core';
2
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3
import { didntChooseBaseUrl, invalidCustomBaseUrl } from '../../../domain/shared-messages';
4
import { ValidatorService } from '../../../services/validator.service';
5

    
6
@Component({
7
  selector: 'compatibility-validate-step1',
8
  templateUrl: 'compatibility-validate-step1.component.html'
9
})
10

    
11
export class CompatibilityValidateStep1Component implements OnInit {
12
  group: FormGroup;
13
  errorMessage: string;
14

    
15
  chosenUrl: string;
16

    
17
  @Input() type: string;
18
  @Input() baseUrlList: string[];
19

    
20
  constructor(private fb: FormBuilder,
21
              private valService: ValidatorService) {}
22

    
23
  ngOnInit() {
24
    this.group = this.fb.group({
25
      selectBaseUrl : '',
26
      customBaseUrl : ''
27
    });
28
    this.group.get('customBaseUrl').disable();
29
  }
30

    
31
  chooseUrl(choice: boolean){
32
    if (choice) {
33
      this.group.get('selectBaseUrl').enable();
34
      this.group.get('customBaseUrl').disable();
35
    } else {
36
      this.group.get('selectBaseUrl').disable();
37
      this.group.get('customBaseUrl').enable();
38
    }
39
  }
40

    
41
  submitForm() {
42
    let response: boolean;
43
    if (this.group.get('selectBaseUrl').enabled){
44
      if ( this.group.get('selectBaseUrl').value) {
45
        this.chosenUrl = this.group.get('selectBaseUrl').value;
46
        console.log('selected baseUrl!');
47
      } else {
48
        this.errorMessage = didntChooseBaseUrl;
49
      }
50
    } else if (this.group.get('customBaseUrl').enabled) {
51
      if ( this.group.get('customBaseUrl').value ) {
52
        console.log(`looking for ${this.group.get('customBaseUrl').value}`);
53
        this.valService.identifyRepository(this.group.get('customBaseUrl').value).subscribe(
54
          res => response = res,
55
          error =>  console.log(error),
56
          () => {
57
            if ( response ) {
58
              this.chosenUrl = this.group.get('customBaseUrl').value;
59
              console.log('added new baseUrl!');
60
            } else {
61
              this.errorMessage = invalidCustomBaseUrl;
62
            }
63
          }
64
        );
65
      } else {
66
        this.errorMessage = didntChooseBaseUrl;
67
      }
68
    }
69
  }
70
}
(2-2/2)