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
  styleUrls: ['./registration.component.css']
11
})
12

    
13
export class RegistrationComponent implements OnInit {
14

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

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

    
33
  dataForm: FormGroup;
34

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

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

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

    
45
  }
46

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

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

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

    
81
  handleChangeAndResetPage() {
82
    this.dataForm.get('page').setValue(0);
83
    this.getRegisteredRepositories();
84
  }
85

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

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

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

    
120
  }
121

    
122
}
(8-8/8)