Project

General

Profile

1 56436 andreas.ma
import {Component, OnInit} from '@angular/core';
2 56782 andreas.ma
import {loadingReposMessage, noServiceMessage} from '../../domain/shared-messages';
3 56436 andreas.ma
import {Country, RepositorySnippet} from '../../domain/typeScriptClasses';
4
import {RepositoryService} from '../../services/repository.service';
5
import {FormBuilder, FormGroup} from '@angular/forms';
6 56580 andreas.ma
import {ActivatedRoute, Params, Router} from '@angular/router';
7
import {URLParameter} from '../../domain/url-parameter';
8 56436 andreas.ma
9 56443 andreas.ma
@Component({
10 56436 andreas.ma
  selector: 'app-registration',
11 56777 andreas.ma
  templateUrl: 'adminPg-registrations.component.html',
12
  styleUrls: ['./adminPg-registrations.component.css']
13 56436 andreas.ma
})
14
15
export class RegistrationComponent implements OnInit {
16
17
  errorMessage: string;
18
  successMessage: string;
19
  loadingMessage: string;
20
  countries: Country[] = [];
21
  repositorySnippet: RepositorySnippet[] = [];
22 56580 andreas.ma
  urlParams: URLParameter[] = [];
23 56469 andreas.ma
  thisIsForBadUse: RepositorySnippet[] = []; // remove if page total is fixed!!!
24 56436 andreas.ma
25
  formPrepare = {
26
    country: '',
27
    typology: '',
28 56777 andreas.ma
    officialName: '',
29 56436 andreas.ma
    requestSortBy: 'registrationdate',
30
    order: 'DESCENDING',
31 56443 andreas.ma
    page: '0',
32 56582 andreas.ma
    size: '25'
33 56436 andreas.ma
  };
34
35
  dataForm: FormGroup;
36
37
  constructor(private repoService: RepositoryService,
38 56580 andreas.ma
              private fb: FormBuilder,
39
              private route: ActivatedRoute,
40
              private router: Router) { }
41 56436 andreas.ma
42
  ngOnInit() {
43
    this.dataForm = this.fb.group(this.formPrepare);
44 56580 andreas.ma
    const tempUrlParams = new Array<URLParameter>();
45
    this.route.queryParams
46
      .subscribe(params => {
47
        for (const i in params) {
48
          this.dataForm.get(i).setValue(params[i]);
49
        }
50
        for (let i in this.dataForm.controls) {
51
          if (this.dataForm.get(i).value) {
52
            tempUrlParams.push({key: i, value: [this.dataForm.get(i).value]});
53
          }
54
        }
55
        this.handleChange();
56
      },
57
      error => this.errorMessage = <any>error
58
    );
59
60 56436 andreas.ma
    this.getCountries();
61
62 56580 andreas.ma
63 56436 andreas.ma
  }
64
65
  getCountries() {
66
    this.repoService.getCountries()
67
      .subscribe(
68
        countries => this.countries = countries.sort(function (a, b) {
69
          if (a.name < b.name) {
70
            return -1;
71
          } else if (a.name > b.name) {
72
            return 1;
73
          } else {
74
            return 0;
75
          }
76
        }),
77
        error => {
78
          this.loadingMessage = '';
79
          this.errorMessage = noServiceMessage;
80
          console.log(error);
81 56469 andreas.ma
        }
82 56436 andreas.ma
      );
83
  }
84
85 56580 andreas.ma
  getRegisteredRepositories(urlParams: URLParameter[]) {
86 56782 andreas.ma
    this.loadingMessage = loadingReposMessage;
87 56582 andreas.ma
    this.repoService.searchRegisteredRepositories(this.dataForm.get('page').value,
88
      this.dataForm.get('size').value, urlParams).subscribe(
89 56443 andreas.ma
        suc => this.repositorySnippet = suc,
90 56782 andreas.ma
        error => {
91
          console.log(error);
92
          this.loadingMessage = '';
93
        },
94
      () => this.loadingMessage = ''
95 56443 andreas.ma
      );
96
  }
97
98
  handleChange() {
99 56580 andreas.ma
    const tempUrlParams = new Array<URLParameter>();
100 56582 andreas.ma
    const map: { [name: string]: string; } = {};
101
102 56580 andreas.ma
    for (let i in this.dataForm.controls) {
103
      if (this.dataForm.get(i).value !== '') {
104
        tempUrlParams.push({key: i, value: [this.dataForm.get(i).value]});
105 56582 andreas.ma
        map[i] = this.dataForm.get(i).value;
106 56580 andreas.ma
      }
107
    }
108
109
    this.router.navigate([`/admin/registrations`],
110
      {queryParams: map});
111
    this.getRegisteredRepositories(tempUrlParams);
112 56443 andreas.ma
  }
113
114 56517 andreas.ma
  handleChangeAndResetPage() {
115
    this.dataForm.get('page').setValue(0);
116 56580 andreas.ma
    this.handleChange();
117 56517 andreas.ma
  }
118
119 56469 andreas.ma
  getCountryName(countryCode): string {
120
    for (const country of Object.values(this.countries)) {
121
      if (country.code === countryCode) {
122
        return country.name;
123
      }
124
    }
125
  }
126
127
  previousPage() {
128
    if (this.dataForm.get('page').value > 0) {
129
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value - 1);
130
      this.handleChange();
131
    }
132
  }
133
134
  nextPage() {
135
    /** remove when page total is fixed!!! **/
136 56580 andreas.ma
    const tempUrlParams = new Array<URLParameter>();
137
    for (let i in this.dataForm.controls) {
138
      if (this.dataForm.get(i).value !== '') {
139
        tempUrlParams.push({key: i, value: [this.dataForm.get(i).value]});
140
      }
141
    }
142 56582 andreas.ma
      this.repoService.searchRegisteredRepositories(+this.dataForm.get('page').value + 1,
143
      this.dataForm.get('size').value, tempUrlParams).subscribe(
144 56469 andreas.ma
      suc => this.thisIsForBadUse = suc,
145
      error => console.log(error),
146
      () => {
147
        if (!(this.thisIsForBadUse.length === 0)) {
148
          this.dataForm.get('page').setValue(+this.dataForm.get('page').value + 1);
149 56580 andreas.ma
          this.handleChange();
150 56469 andreas.ma
        }
151
      }
152
    );
153
    /** **/
154
155
  }
156
157 56436 andreas.ma
}