Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2
import { RepositoryService } from '../../../services/repository.service';
3
import {
4
  loadingReposMessage,
5
  noRepositoriesFound,
6
  noRepositoryChosenMsg,
7
  noServiceMessage } from '../../../domain/shared-messages';
8
import { Country, Repository, RepositorySnippet } from '../../../domain/typeScriptClasses';
9
import { finalize } from 'rxjs/operators';
10

    
11
@Component({
12
  selector: 'register-datasource-shareable',
13
  templateUrl: './register-datasource-shareable.component.html'
14
})
15

    
16
export class RegisterDatasourceShareableComponent implements OnInit {
17
  countries: Country[] = [];
18
  hasSelectedCountry: boolean;
19
  selectedCountry: Country;
20
  countryRepos: RepositorySnippet[] = [];
21
  hasSelectedRepo: boolean;
22

    
23
  noRepositories: string;
24
  alertMessage: string;
25
  loadingMessage: string = loadingReposMessage;
26

    
27
  repoId: string;
28

    
29
  sourceUrl: string;
30
  sourceTitle: string;
31
  latestUpdate: string;
32

    
33
  @Input() mode: string;
34

    
35
  @Output() emitRepoId: EventEmitter<string> = new EventEmitter();
36
  @Output() promptNext: EventEmitter<boolean> = new EventEmitter();
37

    
38
  searchBox: string = '';
39

    
40
  constructor(private repoService:RepositoryService) {}
41

    
42
  ngOnInit() {
43
    this.setUpSourceInfo();
44
    this.getCountries();
45
    this.hasSelectedCountry = false;
46
  }
47

    
48
  setUpSourceInfo() {
49
    if (this.mode == 'opendoar') {
50
      this.sourceUrl = 'https://www.opendoar.org/';
51
      this.sourceTitle = 'OpenDOAR';
52
    } else if (this.mode == 're3data') {
53
      this.sourceUrl = 'https://www.re3data.org/';
54
      this.sourceTitle = 'Re3data';
55
    }
56
    this.getLatestUpdate();
57
  }
58

    
59
  getCountries() {
60
    this.repoService.getCountries()
61
      .subscribe(
62
        countries => {
63
          // TODO: check again getCountries null return values
64
          /*/!* check for null values *!/
65
          let nullVals = countries.filter(el => el.name === null);
66
          /!* remove null values from array *!/
67
          for (let nullVal of nullVals) {
68
            let i = countries.findIndex(el => el === nullVal);
69
            /!* remove null value from array *!/
70
            if (i !== -1) { countries.splice(i, 1); }
71
          }*/
72

    
73
          /* sort countries array */
74
          this.countries = countries.sort( function(a,b) {
75
            if (a.name<b.name) {
76
              return -1;
77
            } else if(a.name>b.name) {
78
              return 1;
79
            } else {
80
              return 0;
81
            }
82
          } );
83
        },
84
        error => {
85
          this.alertMessage = noServiceMessage;
86
          console.log(error);
87
        });
88
  }
89

    
90
  getReposInCountry(i: number){
91
    setTimeout( () => {
92
      const country = this.countries[i];
93
      console.log(`I got ${country} and ${this.mode}`);
94
      this.countryRepos = [];
95
      this.selectedCountry = country;
96
      this.hasSelectedCountry = false;
97
      this.loadingMessage = loadingReposMessage;
98
      this.noRepositories = '';
99
      this.repoService.getRepositoriesOfCountry(country.code, this.mode).subscribe (
100
        repos => {
101
          this.countryRepos = repos;
102
        },
103
        error => {
104
          console.log(error.statusText);
105
          this.loadingMessage = '';
106
          this.alertMessage = noServiceMessage;
107
          this.countryRepos = [];
108
        },
109
        () => {
110
          if (!this.countryRepos || !this.countryRepos.length) {
111
            this.noRepositories = noRepositoriesFound;
112
          } else {
113
            this.noRepositories = '';
114
            if (this.selectedCountry.code == country.code) {
115
              /* to make sure that the correct set of repositories is displayed - in case of consequent country selections */
116
              this.hasSelectedCountry = true;
117
            } else {
118
              this.countryRepos = [];
119
            }
120
          }
121
          this.loadingMessage = '';
122
          this.alertMessage = '';
123
          console.log('this.selectedCountry became', JSON.stringify(this.selectedCountry));
124
        }
125
      );
126
    }, 500);
127
  }
128

    
129
  getLatestUpdate() {
130
    return this.repoService.getListLatestUpdate(this.mode).subscribe (
131
      responseDate => this.latestUpdate = responseDate['lastCollectionDate'],
132
      error => console.log(error)
133
    );
134
  }
135

    
136
  onChooseRepository(id: string){
137
    this.hasSelectedRepo = true;
138
    this.repoId = id;
139
  }
140

    
141
  pushedNext() {
142
    this.promptNext.emit(true);
143
  }
144

    
145
  public goToNextStep(): boolean {
146
    if (!this.hasSelectedRepo || this.noRepositories) {
147
      this.alertMessage = noRepositoryChosenMsg;
148
      return false;
149
    } else {
150
      this.emitRepoId.emit(this.repoId);
151
      return true;
152
    }
153
  }
154

    
155
  searchTerm(event: any) {
156
    this.searchBox = event.target.value;
157
  }
158

    
159
}
(2-2/10)