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 } from '../../../domain/typeScriptClasses';
9

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

    
15
export class RegisterDatasourceShareableComponent implements OnInit {
16
  countries: Country[] = [];
17
  hasSelectedCountry: boolean;
18
  selectedCountry: string;
19
  countryRepos: Repository[] = [];
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
  constructor(private repoService:RepositoryService) {}
38

    
39
  ngOnInit() {
40
    this.setUpSourceInfo();
41
    this.getCountries();
42
    this.hasSelectedCountry = false;
43
    this.selectedCountry = '';
44
  }
45

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

    
57
  getCountries(){
58
    this.repoService.getCountries()
59
      .subscribe(
60
        countries => this.countries = countries.sort( function(a,b){
61
          if(a.name<b.name){
62
            return -1;
63
          } else if(a.name>b.name){
64
            return 1;
65
          } else {
66
            return 0;
67
          }
68
        } ),
69
        error => {
70
          this.alertMessage = noServiceMessage;
71
          console.log(error);
72
        });
73
  }
74

    
75
  getReposInCountry(country: string){
76
    setTimeout( () => {
77
      console.log(`I got ${country} and ${this.mode}`);
78
      this.countryRepos = [];
79
      this.selectedCountry = country;
80
      this.hasSelectedCountry = false;
81
      this.loadingMessage = loadingReposMessage;
82
      this.noRepositories = '';
83
      this.repoService.getRepositoriesOfCountry(country, this.mode).subscribe(
84
        repos => {
85
          this.countryRepos = repos;
86
        },
87
        error => {
88
          console.log(error.statusText);
89
          this.loadingMessage = '';
90
          this.alertMessage = noServiceMessage;
91
          this.countryRepos = [];
92
        },
93
        () => {
94
          if (!this.countryRepos.length) {
95
            this.noRepositories = noRepositoriesFound;
96
          } else {
97
            this.noRepositories = '';
98
            if (this.selectedCountry == country) {
99
              /* to make sure that the correct set of repositories is displayed - in case of consequent country selections */
100
              this.hasSelectedCountry = true;
101
            } else {
102
              this.countryRepos = [];
103
            }
104
          }
105
          this.loadingMessage = '';
106
          this.alertMessage = '';
107
        }
108
      );
109
    }, 500);
110
  }
111

    
112
  getLatestUpdate() {
113
    return this.repoService.getListLatestUpdate(this.mode).subscribe(
114
      responseDate => this.latestUpdate = responseDate,
115
      error => console.log(error)
116
    );
117
  }
118

    
119
  onChooseRepository(id: string){
120
    this.hasSelectedRepo = true;
121
    this.repoId = id;
122
  }
123

    
124
  pushedNext() {
125
    this.promptNext.emit(true);
126
  }
127

    
128
  public goToNextStep(): boolean {
129
    if(!this.hasSelectedRepo || this.noRepositories){
130
      this.alertMessage = noRepositoryChosenMsg;
131
      return false;
132
    } else {
133
      this.emitRepoId.emit(this.repoId);
134
      return true;
135
    }
136
  }
137

    
138
}
(2-2/10)