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
  repoInterfaces: RepositoryInterface[] = [];
26

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

    
29
  group: FormGroup;
30

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

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

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

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

    
48
  ngOnInit() {
49

    
50
    if (this.sharedService.getRepository()) {
51
      this.repo = this.sharedService.getRepository();
52
      this.logoURL = this.repo.logoUrl;
53
      this.getRepoInterfaces();
54
    }
55

    
56
    this.sharedService.repository$.subscribe(
57
      r => {
58
        this.repo = r;
59
        if (this.repo) {
60
          this.logoURL = this.repo.logoUrl;
61
          this.getRepoInterfaces();
62
        }
63
      }
64
    );
65

    
66
    // this.readRepoId();
67
    const body = document.getElementsByTagName('body')[0];
68
    body.classList.remove('top_bar_active');   // remove the class
69
    body.classList.remove('page_heading_active');
70
    body.classList.remove('landing');
71
    body.classList.add('dashboard');
72
  }
73

    
74
  // readRepoId() {
75
  //   this.repoId = this.route.snapshot.paramMap.get('id');
76
  //   console.log(`repoId is ${this.repoId}`);
77
  //   this.getRepo();
78
  // }
79
  //
80
  // getRepo() {
81
  //   if (this.repoId) {
82
  //     this.loadingMessage = formInfoLoading;
83
  //     this.repoService.getRepositoryById(this.repoId).subscribe(
84
  //       repo => {
85
  //         this.repo = repo;
86
  //       },
87
  //       error => {
88
  //         console.log(error);
89
  //         this.loadingMessage = '';
90
  //         this.errorMessage = loadingRepoError;
91
  //       },
92
  //       () => {
93
  //           this.logoURL = this.repo.logoUrl;
94
  //           this.getRepoInterfaces();
95
  //       }
96
  //     );
97
  //   }
98
  // }
99

    
100
  getRepoInterfaces() {
101
    this.group = this.fb.group({});
102
    this.repoService.getRepositoryInterface(this.repo.id).subscribe(
103
      interfaces => {
104
        this.repoInterfaces = interfaces.sort(function (a, b) {
105
          if (a.id < b.id) {
106
            return -1;
107
          } else if (a.id > b.id) {
108
            return 1;
109
          } else {
110
            return 0;
111
          }
112
        });
113
        console.log(`the number of interfaces for ${this.repo.id} is ${this.repoInterfaces.length}`);
114
      },
115
      error => {
116
        console.log(error);
117
        this.loadingMessage = '';
118
        this.errorMessage = loadingRepoError;
119
      },
120
      () => {
121
        this.loadingMessage = '';
122
        this.fillInterfacesForms();
123
      }
124
    );
125
  }
126

    
127

    
128
  fillInterfacesForms() {
129
    this.dataForInterfaceComp = [];
130
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
131
      for (let i = 0; i < this.repoInterfaces.length; i++) {
132
        this.dataForInterfaceComp.push([
133
          false, i,
134
          {
135
            id: this.repo.id,
136
            datasourceType: this.repo.datasourceType,
137
            datasourceClass: this.repo.datasourceClass,
138
            registeredBy: this.repo.registeredBy
139
          },
140
          this.repoInterfaces[i]
141
        ]);
142
      }
143
    } else {
144
      this.dataForInterfaceComp.push([
145
        false, 0,
146
        {
147
          id: this.repo.id,
148
          datasourceType: this.repo.datasourceType,
149
          datasourceClass: this.repo.datasourceClass,
150
          registeredBy: this.repo.registeredBy
151
        }
152
      ]);
153
    }
154
  }
155

    
156
  addInterfaceToList(intrf?: RepositoryInterface) {
157
    const curIndex = this.dataForInterfaceComp.length;
158
    const curRepoInfo = {
159
      id: this.repo.id, datasourceType: this.repo.datasourceType,
160
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy
161
    };
162
    if (intrf) {
163
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo, intrf]);
164
    } else {
165
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo]);
166
    }
167
  }
168

    
169
  removeInterfaceFromList(i: number) {
170
    const tempArray = this.dataForInterfaceComp;
171
    this.dataForInterfaceComp = [];
172
    tempArray.splice(i, 1);
173
    this.dataForInterfaceComp = tempArray;
174
    console.log(JSON.stringify(this.dataForInterfaceComp));
175
  }
176

    
177
  getInterfaces() {
178
    this.repoInterfaces = [];
179
    for (const el of this.interfacesArray.toArray()) {
180
      const intrf = el.getInterface();
181
      if (intrf) {
182
        this.repoInterfaces.push(intrf);
183
        console.log(JSON.stringify(intrf));
184
      }
185
    }
186
    console.log('new interfaces is ', this.repoInterfaces);
187
  }
188

    
189
  updateLogoUrl(logoUrl: string) {
190
    this.updateLogoUrlModal.ids = [logoUrl];
191
    this.updateLogoUrlModal.showModal();
192
  }
193

    
194
  updatedLogoUrl(event: any) {
195
    this.repo.logoUrl = this.logoURL;
196
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
197
    this.datasourceUpdateForm.updateRepo();
198

    
199
  }
200

    
201
  getNewLogoUrl(event: any) {
202
    this.logoURL = event.target.value;
203

    
204
  }
205

    
206
}
(2-2/4)