Project

General

Profile

1
import {Component, OnInit} from '@angular/core';
2
import {loadingReposMessage, 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
import {ActivatedRoute, Params, Router} from '@angular/router';
7
import {URLParameter} from '../../domain/url-parameter';
8

    
9
@Component({
10
  selector: 'app-registration',
11
  templateUrl: 'adminPg-registrations.component.html',
12
  styleUrls: ['./adminPg-registrations.component.css']
13
})
14

    
15
export class RegistrationComponent implements OnInit {
16

    
17
  errorMessage: string;
18
  successMessage: string;
19
  loadingMessage: string;
20
  countries: Country[] = [];
21
  repositorySnippet: RepositorySnippet[] = [];
22
  urlParams: URLParameter[] = [];
23
  thisIsForBadUse: RepositorySnippet[] = []; // remove if page total is fixed!!!
24

    
25
  formPrepare = {
26
    country: '',
27
    typology: '',
28
    officialName: '',
29
    requestSortBy: 'registrationdate',
30
    order: 'DESCENDING',
31
    page: '0',
32
    size: '25'
33
  };
34

    
35
  dataForm: FormGroup;
36

    
37
  constructor(private repoService: RepositoryService,
38
              private fb: FormBuilder,
39
              private route: ActivatedRoute,
40
              private router: Router) { }
41

    
42
  ngOnInit() {
43
    this.dataForm = this.fb.group(this.formPrepare);
44
    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
    this.getCountries();
61

    
62

    
63
  }
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
        }
82
      );
83
  }
84

    
85
  getRegisteredRepositories(urlParams: URLParameter[]) {
86
    this.loadingMessage = loadingReposMessage;
87
    this.repoService.searchRegisteredRepositories(this.dataForm.get('page').value,
88
      this.dataForm.get('size').value, urlParams).subscribe(
89
        suc => this.repositorySnippet = suc,
90
        error => {
91
          console.log(error);
92
          this.loadingMessage = '';
93
        },
94
      () => this.loadingMessage = ''
95
      );
96
  }
97

    
98
  handleChange() {
99
    const tempUrlParams = new Array<URLParameter>();
100
    const map: { [name: string]: string; } = {};
101

    
102
    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
        map[i] = this.dataForm.get(i).value;
106
      }
107
    }
108

    
109
    this.router.navigate([`/admin/registrations`],
110
      {queryParams: map});
111
    this.getRegisteredRepositories(tempUrlParams);
112
  }
113

    
114
  handleChangeAndResetPage() {
115
    this.dataForm.get('page').setValue(0);
116
    this.handleChange();
117
  }
118

    
119
  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
    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
      this.repoService.searchRegisteredRepositories(+this.dataForm.get('page').value + 1,
143
      this.dataForm.get('size').value, tempUrlParams).subscribe(
144
      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
          this.handleChange();
150
        }
151
      }
152
    );
153
    /** **/
154

    
155
  }
156

    
157
}
(5-5/8)