Project

General

Profile

1
import { Component, OnInit, Type, ViewChild } from '@angular/core';
2
import { FormBuilder, FormGroup } from '@angular/forms';
3
import { DatasourceInterfaceFormComponent } from './sources-forms/datasource-interface-form.component';
4
import { Repository, RepositoryInterface } from '../../domain/typeScriptClasses';
5
import { RepositoryService } from '../../services/repository.service';
6
import {ActivatedRoute, Router} from '@angular/router';
7
import {
8
  Description,
9
  interfaceFormDesc,
10
} from '../../domain/oa-description';
11
import { formInfoLoading, loadingRepoError } from '../../domain/shared-messages';
12
import { DatasourceUpdateFormComponent } from './sources-forms/datasource-update-form.component';
13
import {ConfirmationDialogComponent} from "../../shared/reusablecomponents/confirmation-dialog.component";
14
import {AuthenticationService} from "../../services/authentication.service";
15

    
16

    
17

    
18
@Component ({
19
  selector: 'sources-update-repo',
20
  templateUrl: 'sources-update-repo.component.html'
21
})
22

    
23
export class SourcesUpdateRepoComponent implements OnInit {
24
  loadingMessage: string;
25
  errorMessage: string;
26

    
27
  repoId: string;
28
  logoURL: string;
29
  repo: Repository;
30
  repoInterfaces: RepositoryInterface[] = [];
31

    
32
  @ViewChild('datasourceUpdateForm') datasourceUpdateForm: DatasourceUpdateFormComponent;
33

    
34
  group: FormGroup;
35
  interfaceFormDesc: Description = interfaceFormDesc;
36
  updateDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
37

    
38
  isModalShown: boolean;
39
  @ViewChild('updateLogoUrlModal')
40
  public updateLogoUrlModal: ConfirmationDialogComponent;
41

    
42
  constructor (
43
    private fb: FormBuilder,
44
    private repoService: RepositoryService,
45
    private authService: AuthenticationService,
46
    private route: ActivatedRoute,
47
    private router: Router)
48
  {}
49

    
50

    
51
  ngOnInit() {
52
    this.readRepoId();
53
  }
54

    
55
  readRepoId() {
56
    this.repoId = this.route.snapshot.paramMap.get('id');
57
    console.log(`repoId is ${this.repoId}`);
58
    this.getRepo();
59
    this.getRepoInterfaces();
60
  }
61

    
62
  getRepo() {
63
    if (this.repoId) {
64
      this.loadingMessage = formInfoLoading;
65
      this.repoService.getRepositoryById(this.repoId).subscribe(
66
        repo => {
67
          this.repo = repo;
68
        },
69
        error => {
70
          console.log(error);
71
          this.loadingMessage = '';
72
          this.errorMessage = loadingRepoError;
73
        },
74
        () => {
75
          this.loadingMessage = '';
76
          this.logoURL = this.repo.logoUrl;
77
          if ( this.authService.getUserEmail() !== this.repo.registeredBy ) {
78
            this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
79
          }
80
        }
81
      );
82
    }
83
  }
84

    
85
  getRepoInterfaces() {
86
    this.group = this.fb.group({});
87
    this.repoService.getRepositoryInterface(this.repoId).subscribe(
88
      interfaces => {
89
        this.repoInterfaces = interfaces.sort( function(a,b) {
90
          if(a.id<b.id){
91
            return -1;
92
          } else if(a.id>b.id){
93
            return 1;
94
          } else {
95
            return 0;
96
          }
97
        });
98
        console.log(`the number of interfaces for ${this.repoId} is ${this.repoInterfaces.length}`);
99
      },
100
      error => {
101
        console.log(error);
102
        this.loadingMessage = '';
103
        this.errorMessage = loadingRepoError;
104
      }, () => this.loadingMessage = ''
105
    );
106
  }
107

    
108
  updateLogoUrl(logoUrl: string){
109
    this.updateLogoUrlModal.ids = [logoUrl];
110
    this.updateLogoUrlModal.showModal();
111
  }
112

    
113
  updatedLogoUrl(event: any) {
114
    this.repo.logoUrl = this.logoURL;
115
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
116
    this.datasourceUpdateForm.updateRepo();
117

    
118
  }
119

    
120
  getNewLogoUrl( event: any ) {
121
    this.logoURL = event.target.value;
122

    
123
  }
124

    
125
}
(4-4/10)