Project

General

Profile

1
/*
2
*  updated by myrto on 19/12/2018
3
*/
4

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

    
10
@Component({
11
  selector: 'app-select-existing-datasource',
12
  templateUrl: './register-datasource-select-existing.component.html'
13
})
14

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

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

    
26
  repoId: string;
27

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

    
32
  @Input() mode: string;
33

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

    
37
  searchBox = '';
38

    
39
  constructor(private repoService: RepositoryService) {}
40

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

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

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

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

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

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

    
133
  onChooseRepository(id: string) {
134
    this.hasSelectedRepo = true;
135
    this.repoId = id;
136
  }
137

    
138
  pushedNext() {
139
    this.promptNext.emit(true);
140
  }
141

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

    
152
  searchTerm(event: any) {
153
    this.searchBox = event.target.value;
154
  }
155

    
156
}
(2-2/10)