Project

General

Profile

1 56436 andreas.ma
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 56443 andreas.ma
@Component({
8 56436 andreas.ma
  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 56469 andreas.ma
  thisIsForBadUse: RepositorySnippet[] = []; // remove if page total is fixed!!!
20 56436 andreas.ma
21
  formPrepare = {
22
    country: '',
23
    typology: '',
24 56443 andreas.ma
    englishname: '',
25
    officialname: '',
26 56436 andreas.ma
    requestSortBy: 'registrationdate',
27
    order: 'DESCENDING',
28 56443 andreas.ma
    page: '0',
29 56436 andreas.ma
    pageSize: '25'
30
  };
31
32
  dataForm: FormGroup;
33
34
  constructor(private repoService: RepositoryService,
35 56443 andreas.ma
              private fb: FormBuilder) {
36
  }
37 56436 andreas.ma
38
  ngOnInit() {
39
    this.dataForm = this.fb.group(this.formPrepare);
40
41
    this.getCountries();
42 56443 andreas.ma
    this.getRegisteredRepositories();
43 56436 andreas.ma
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 56469 andreas.ma
        }
63 56436 andreas.ma
      );
64
  }
65
66 56443 andreas.ma
  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 56469 andreas.ma
        // () => console.log(this.repositorySnippet)
73 56443 andreas.ma
      );
74
  }
75
76
  handleChange() {
77
    this.getRegisteredRepositories();
78
  }
79
80 56469 andreas.ma
  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 56496 andreas.ma
      this.repoService.searchRegisteredRepositories(this.dataForm.get('country').value, this.dataForm.get('typology').value, this.dataForm.get('englishname').value,
98 56469 andreas.ma
      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 56496 andreas.ma
        console.log(this.thisIsForBadUse.length);
104 56469 andreas.ma
        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 56436 andreas.ma
}