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 56496 andreas.ma
  handleChangeAndResetPage() {
81
    this.dataForm.get('page').setValue(0);
82
    this.getRegisteredRepositories();
83
  }
84
85 56469 andreas.ma
  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 56496 andreas.ma
      this.repoService.searchRegisteredRepositories(this.dataForm.get('country').value, this.dataForm.get('typology').value, this.dataForm.get('englishname').value,
103 56469 andreas.ma
      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 56496 andreas.ma
        console.log(this.thisIsForBadUse.length);
109 56469 andreas.ma
        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 56436 andreas.ma
}