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 { formInfoLoading, loadingRepoError } from '../../domain/shared-messages';
7
import { DatasourceUpdateFormComponent } from './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 './sources-forms/datasource-new-interface-form.component';
11

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

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

    
21
  repoId: string;
22
  logoURL: string;
23
  repo: Repository;
24
  repoInterfaces: RepositoryInterface[] = [];
25

    
26
  @ViewChild('datasourceUpdateForm') datasourceUpdateForm: DatasourceUpdateFormComponent;
27

    
28
  group: FormGroup;
29

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

    
33
  isModalShown: boolean;
34
  @ViewChild('updateLogoUrlModal')
35
  public updateLogoUrlModal: ConfirmationDialogComponent;
36

    
37
  constructor ( private fb: FormBuilder,
38
                private repoService: RepositoryService,
39
                private authService: AuthenticationService,
40
                private route: ActivatedRoute,
41
                private router: Router) { }
42

    
43

    
44
  ngOnInit() {
45
    this.readRepoId();
46
  }
47

    
48
  readRepoId() {
49
    this.repoId = this.route.snapshot.paramMap.get('id');
50
    console.log(`repoId is ${this.repoId}`);
51
    this.getRepo();
52
  }
53

    
54
  getRepo() {
55
    if (this.repoId) {
56
      this.loadingMessage = formInfoLoading;
57
      this.repoService.getRepositoryById(this.repoId).subscribe(
58
        repo => {
59
          this.repo = repo;
60
        },
61
        error => {
62
          console.log(error);
63
          this.loadingMessage = '';
64
          this.errorMessage = loadingRepoError;
65
        },
66
        () => {
67
          if ( this.authService.activateFrontAuthorization && (this.authService.getUserEmail() !== this.repo.registeredBy.trim()) ) {
68
            this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
69
          } else {
70
            this.logoURL = this.repo.logoUrl;
71
            this.getRepoInterfaces();
72
          }
73
        }
74
      );
75
    }
76
  }
77

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

    
105

    
106
  fillInterfacesForms() {
107
    this.dataForInterfaceComp = [];
108
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
109
      for (let i = 0; i < this.repoInterfaces.length; i++) {
110
        this.dataForInterfaceComp.push([
111
          false, i,
112
          { id: this.repo.id,
113
            datasourceType: this.repo.datasourceType,
114
            datasourceClass: this.repo.datasourceClass,
115
            registeredBy: this.repo.registeredBy
116
          },
117
          this.repoInterfaces[i]
118
        ]);
119
      }
120
    } else {
121
      this.dataForInterfaceComp.push([
122
        false, 0,
123
        { id: this.repo.id,
124
          datasourceType: this.repo.datasourceType,
125
          datasourceClass: this.repo.datasourceClass,
126
          registeredBy: this.repo.registeredBy
127
        }
128
      ]);
129
    }
130
  }
131

    
132
  addInterfaceToList(intrf?: RepositoryInterface) {
133
    const curIndex = this.dataForInterfaceComp.length;
134
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
135
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy };
136
    if (intrf) {
137
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo, intrf]);
138
    } else {
139
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo]);
140
    }
141
  }
142

    
143
  removeInterfaceFromList(i: number) {
144
    const tempArray = this.dataForInterfaceComp;
145
    this.dataForInterfaceComp = [];
146
    tempArray.splice(i, 1);
147
    this.dataForInterfaceComp = tempArray;
148
    console.log(JSON.stringify(this.dataForInterfaceComp));
149
  }
150

    
151
  getInterfaces() {
152
    this.repoInterfaces = [];
153
    for (const el of this.interfacesArray.toArray()) {
154
      const intrf = el.getInterface();
155
      if (intrf) {
156
        this.repoInterfaces.push(intrf);
157
        console.log(JSON.stringify(intrf));
158
      }
159
    }
160
    console.log('new interfaces is ', this.repoInterfaces);
161
  }
162

    
163
  updateLogoUrl(logoUrl: string) {
164
    this.updateLogoUrlModal.ids = [logoUrl];
165
    this.updateLogoUrlModal.showModal();
166
  }
167

    
168
  updatedLogoUrl(event: any) {
169
    this.repo.logoUrl = this.logoURL;
170
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
171
    this.datasourceUpdateForm.updateRepo();
172

    
173
  }
174

    
175
  getNewLogoUrl( event: any ) {
176
    this.logoURL = event.target.value;
177

    
178
  }
179

    
180
}
(4-4/10)