Project

General

Profile

1
import {Component, OnInit} from '@angular/core';
2
import {noServiceMessage} from '../../domain/shared-messages';
3
import {Country, RepositorySnippet} from '../../domain/typeScriptClasses';
4
import {RepositoryService} from '../../services/repository.service';
5
import {FormBuilder, FormGroup} from '@angular/forms';
6

    
7
@Component({
8
  selector: 'app-registration',
9
  templateUrl: 'registration.component.html'
10
})
11

    
12
export class RegistrationComponent implements OnInit {
13

    
14
  errorMessage: string;
15
  successMessage: string;
16
  loadingMessage: string;
17
  countries: Country[] = [];
18
  repositorySnippet: RepositorySnippet[] = [];
19
  thisIsForBadUse: RepositorySnippet[] = []; // remove if page total is fixed!!!
20

    
21
  formPrepare = {
22
    country: '',
23
    typology: '',
24
    englishname: '',
25
    officialname: '',
26
    requestSortBy: 'registrationdate',
27
    order: 'DESCENDING',
28
    page: '0',
29
    pageSize: '25'
30
  };
31

    
32
  dataForm: FormGroup;
33

    
34
  constructor(private repoService: RepositoryService,
35
              private fb: FormBuilder) {
36
  }
37

    
38
  ngOnInit() {
39
    this.dataForm = this.fb.group(this.formPrepare);
40

    
41
    this.getCountries();
42
    this.getRegisteredRepositories();
43

    
44
  }
45

    
46
  getCountries() {
47
    this.repoService.getCountries()
48
      .subscribe(
49
        countries => this.countries = countries.sort(function (a, b) {
50
          if (a.name < b.name) {
51
            return -1;
52
          } else if (a.name > b.name) {
53
            return 1;
54
          } else {
55
            return 0;
56
          }
57
        }),
58
        error => {
59
          this.loadingMessage = '';
60
          this.errorMessage = noServiceMessage;
61
          console.log(error);
62
        }
63
      );
64
  }
65

    
66
  getRegisteredRepositories() {
67
    this.repoService.searchRegisteredRepositories(this.dataForm.get('country').value, this.dataForm.get('typology').value, this.dataForm.get('englishname').value,
68
      this.dataForm.get('officialname').value, this.dataForm.get('requestSortBy').value, this.dataForm.get('order').value, this.dataForm.get('page').value,
69
      this.dataForm.get('pageSize').value).subscribe(
70
        suc => this.repositorySnippet = suc,
71
        error => console.log(error),
72
        // () => console.log(this.repositorySnippet)
73
      );
74
  }
75

    
76
  handleChange() {
77
    this.getRegisteredRepositories();
78
  }
79

    
80
  getCountryName(countryCode): string {
81
    for (const country of Object.values(this.countries)) {
82
      if (country.code === countryCode) {
83
        return country.name;
84
      }
85
    }
86
  }
87

    
88
  previousPage() {
89
    if (this.dataForm.get('page').value > 0) {
90
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value - 1);
91
      this.handleChange();
92
    }
93
  }
94

    
95
  nextPage() {
96
    /** remove when page total is fixed!!! **/
97
      this.repoService.searchRegisteredRepositories(this.dataForm.get('country').value, this.dataForm.get('typology').value, this.dataForm.get('englishname').value,
98
      this.dataForm.get('officialname').value, this.dataForm.get('requestSortBy').value, this.dataForm.get('order').value, +this.dataForm.get('page').value + 1,
99
      this.dataForm.get('pageSize').value).subscribe(
100
      suc => this.thisIsForBadUse = suc,
101
      error => console.log(error),
102
      () => {
103
        console.log(this.thisIsForBadUse.length);
104
        if (!(this.thisIsForBadUse.length === 0)) {
105
          console.log('got here');
106
          this.dataForm.get('page').setValue(+this.dataForm.get('page').value + 1);
107
          this.repositorySnippet = this.thisIsForBadUse;
108
          // this.handleChange();
109
        }
110
      }
111
    );
112
    /** **/
113

    
114
  }
115

    
116
}
(7-7/7)