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
  identifiedUrl: boolean;
17

    
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

    
42
  identifyUrl() {
43
    if (this.group.get('customBaseUrl').value) {
44
      console.log(`looking for ${this.group.get('customBaseUrl').value}`);
45
      this.valService.identifyRepository(this.group.get('customBaseUrl').value).subscribe(
46
        res => this.identifiedUrl = res,
47
        error =>  console.log(error)
48
      );
49
    }
50
  }
51

    
52
  submitForm() {
53
    let response: boolean;
54
    if (this.group.get('selectBaseUrl').enabled){
55
      if ( this.group.get('selectBaseUrl').value) {
56
        this.chosenUrl = this.group.get('selectBaseUrl').value;
57
        console.log('selected baseUrl!');
58
        return true;
59
      } else {
60
        this.errorMessage = didntChooseBaseUrl;
61
        return false;
62
      }
63
    } else if (this.group.get('customBaseUrl').enabled) {
64
      if ( this.group.get('customBaseUrl').value ) {
65
        if (this.identifiedUrl) {
66
          this.chosenUrl = this.group.get('customBaseUrl').value;
67
          console.log('added new baseUrl!');
68
          return true;
69
        } else {
70
          this.errorMessage = invalidCustomBaseUrl;
71
          return false;
72
        }
73
      } else {
74
        this.errorMessage = didntChooseBaseUrl;
75
        return false;
76
      }
77
    }
78
  }
79
}
(2-2/4)