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
  handleChangeAndResetPage() {
81
    this.dataForm.get('page').setValue(0);
82
    this.getRegisteredRepositories();
83
  }
84

    
85
  getCountryName(countryCode): string {
86
    for (const country of Object.values(this.countries)) {
87
      if (country.code === countryCode) {
88
        return country.name;
89
      }
90
    }
91
  }
92

    
93
  previousPage() {
94
    if (this.dataForm.get('page').value > 0) {
95
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value - 1);
96
      this.handleChange();
97
    }
98
  }
99

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

    
119
  }
120

    
121
}
(7-7/7)