Project

General

Profile

1
import {Component, OnInit, QueryList, ViewChild, ViewChildren} from '@angular/core';
2
import {FormBuilder, FormGroup} from '@angular/forms';
3
import {Repository, RepositoryInterface} from '../../../domain/typeScriptClasses';
4
import {RepositoryService} from '../../../services/repository.service';
5
import {ActivatedRoute, Router} from '@angular/router';
6
import {loadingRepoError} from '../../../domain/shared-messages';
7
import {DatasourceUpdateFormComponent} from '../../../shared/reusablecomponents/sources-forms/datasource-update-form.component';
8
import {ConfirmationDialogComponent} from '../../../shared/reusablecomponents/confirmation-dialog.component';
9
import {AuthenticationService} from '../../../services/authentication.service';
10
import {DatasourceNewInterfaceFormComponent} from '../../../shared/reusablecomponents/sources-forms/datasource-new-interface-form.component';
11
import {SharedService} from '../../../services/shared.service';
12

    
13
@Component({
14
  selector: 'sources-update-repo',
15
  templateUrl: 'sources-update-repo.component.html',
16
})
17

    
18
export class SourcesUpdateRepoComponent implements OnInit {
19
  loadingMessage: string;
20
  errorMessage: string;
21

    
22
  // repoId: string;
23
  logoURL: string;
24
  repo: Repository;
25
  mode: string = null;
26
  repoInterfaces: RepositoryInterface[] = [];
27

    
28
  @ViewChild('datasourceUpdateForm') datasourceUpdateForm: DatasourceUpdateFormComponent;
29

    
30
  group: FormGroup;
31

    
32
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
33
  dataForInterfaceComp: any[] = [];
34

    
35
  isModalShown: boolean;
36
  @ViewChild('updateLogoUrlModal', {static: true})
37
  public updateLogoUrlModal: ConfirmationDialogComponent;
38

    
39
  constructor(private fb: FormBuilder,
40
              private repoService: RepositoryService,
41
              private authService: AuthenticationService,
42
              private route: ActivatedRoute,
43
              private sharedService: SharedService,
44
              private router: Router) {
45
  }
46

    
47
  @ViewChild('updateTermsForm') updateTermsForm: DatasourceUpdateFormComponent;
48

    
49
  ngOnInit() {
50

    
51
    if (this.sharedService.getRepository()) {
52
      this.repo = this.sharedService.getRepository();
53
      if (this.repo.id.includes('eurocrisdris')) {
54
        this.mode = 'cris';
55
      } else if (this.repo.id.includes('opendoar')) {
56
        this.mode = 'opendoar';
57
      } else if (this.repo.id.includes('re3data')) {
58
        this.mode = 're3data';
59
      } else if (this.repo.id.includes('journal')) {
60
        this.mode = 'journal';
61
      } else if (this.repo.id.includes('aggregator')) {
62
        this.mode = 'aggregator';
63
      }
64
      this.logoURL = this.repo.logourl;
65
      this.getRepoInterfaces();
66
    }
67

    
68
    this.sharedService.repository$.subscribe(
69
      r => {
70
        this.repo = r;
71
        if (this.repo) {
72
          this.logoURL = this.repo.logourl;
73
          this.getRepoInterfaces();
74
        }
75
      }
76
    );
77

    
78
    // this.readRepoId();
79
    const body = document.getElementsByTagName('body')[0];
80
    body.classList.remove('top_bar_active');   // remove the class
81
    body.classList.remove('page_heading_active');
82
    body.classList.remove('landing');
83
    body.classList.add('dashboard');
84
  }
85

    
86
  // readRepoId() {
87
  //   this.repoId = this.route.snapshot.paramMap.get('id');
88
  //   console.log(`repoId is ${this.repoId}`);
89
  //   this.getRepo();
90
  // }
91
  //
92
  // getRepo() {
93
  //   if (this.repoId) {
94
  //     this.loadingMessage = formInfoLoading;
95
  //     this.repoService.getRepositoryById(this.repoId).subscribe(
96
  //       repo => {
97
  //         this.repo = repo;
98
  //       },
99
  //       error => {
100
  //         console.log(error);
101
  //         this.loadingMessage = '';
102
  //         this.errorMessage = loadingRepoError;
103
  //       },
104
  //       () => {
105
  //           this.logoURL = this.repo.logoUrl;
106
  //           this.getRepoInterfaces();
107
  //       }
108
  //     );
109
  //   }
110
  // }
111

    
112
  getRepoInterfaces() {
113
    this.group = this.fb.group({});
114
    this.repoService.getRepositoryInterface(this.repo.id).subscribe(
115
      interfaces => {
116
        this.repoInterfaces = interfaces.sort(function (a, b) {
117
          if (a.id < b.id) {
118
            return -1;
119
          } else if (a.id > b.id) {
120
            return 1;
121
          } else {
122
            return 0;
123
          }
124
        });
125
        console.log(`the number of interfaces for ${this.repo.id} is ${this.repoInterfaces.length}`);
126
      },
127
      error => {
128
        console.log(error);
129
        this.loadingMessage = '';
130
        this.errorMessage = loadingRepoError;
131
      },
132
      () => {
133
        this.loadingMessage = '';
134
        this.fillInterfacesForms();
135
      }
136
    );
137
  }
138

    
139

    
140
  fillInterfacesForms() {
141
    this.dataForInterfaceComp = [];
142
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
143
      for (let i = 0; i < this.repoInterfaces.length; i++) {
144
        this.dataForInterfaceComp.push([
145
          false, i,
146
          {
147
            id: this.repo.id,
148
            datasourceType: this.repo.eoscDatasourceType,
149
            datasourceClass: this.repo.eoscDatasourceType,
150
            registeredBy: this.repo.registeredby
151
          },
152
          this.repoInterfaces[i]
153
        ]);
154
      }
155
    } else {
156
      this.dataForInterfaceComp.push([
157
        false, 0,
158
        {
159
          id: this.repo.id,
160
          datasourceType: this.repo.eoscDatasourceType,
161
          datasourceClass: this.repo.eoscDatasourceType,
162
          registeredBy: this.repo.registeredby
163
        }
164
      ]);
165
    }
166
  }
167

    
168
  addInterfaceToList(intrf?: RepositoryInterface) {
169
    const curIndex = this.dataForInterfaceComp.length;
170
    const curRepoInfo = {
171
      id: this.repo.id, datasourceType: this.repo.eoscDatasourceType,
172
      datasourceClass: this.repo.eoscDatasourceType, registeredBy: this.repo.registeredby
173
    };
174
    if (intrf) {
175
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo, intrf]);
176
    } else {
177
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo]);
178
    }
179
  }
180

    
181
  removeInterfaceFromList(i: number) {
182
    const tempArray = this.dataForInterfaceComp;
183
    this.dataForInterfaceComp = [];
184
    tempArray.splice(i, 1);
185
    this.dataForInterfaceComp = tempArray;
186
    console.log(JSON.stringify(this.dataForInterfaceComp));
187
  }
188

    
189
  getInterfaces() {
190
    this.repoInterfaces = [];
191
    for (const el of this.interfacesArray.toArray()) {
192
      const intrf = el.getInterface();
193
      if (intrf) {
194
        this.repoInterfaces.push(intrf);
195
        console.log(JSON.stringify(intrf));
196
      }
197
    }
198
    console.log('new interfaces is ', this.repoInterfaces);
199
  }
200

    
201
  updateLogoUrl(logoUrl: string) {
202
    this.updateLogoUrlModal.ids = [logoUrl];
203
    this.updateLogoUrlModal.showModal();
204
  }
205

    
206
  updatedLogoUrl(event: any) {
207
    this.repo.logourl = this.logoURL;
208
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
209
    this.datasourceUpdateForm.updateRepo();
210

    
211
  }
212

    
213
  getNewLogoUrl(event: any) {
214
    this.logoURL = event.target.value;
215

    
216
  }
217

    
218
}
(2-2/4)