Project

General

Profile

1
import {Component, OnInit, ViewChild} from '@angular/core';
2
import { NavigationEnd, Router, RoutesRecognized } from '@angular/router';
3
import { AuthenticationService } from './services/authentication.service';
4
import { environment } from '../environments/environment';
5
import { MatomoTracker } from 'ngx-matomo';
6
import { ConfirmationDialogComponent } from './shared/reusablecomponents/confirmation-dialog.component';
7
import { RepositoryService } from './services/repository.service';
8
import { RepositorySnippet } from './domain/typeScriptClasses';
9
import {FormBuilder, FormGroup, FormControl, FormArray} from '@angular/forms';
10
import {element} from 'protractor';
11
import {timestamp} from 'rxjs/operators';
12

    
13
@Component({
14
  selector: 'oa-repo-manager',
15
  templateUrl: './app.component.html',
16
  styleUrls: ['./app.component.css'],
17
})
18
export class AppComponent implements OnInit {
19
  reposOfUser: RepositorySnippet[] = [];
20
  modalTitle = 'Terms of Use';
21
  isModalShown: boolean;
22
  modalButton = 'OK';
23

    
24
  agreementForm = this.fb.group({
25
    terms: this.fb.array([])
26
  });
27

    
28
  consentTermsOfUseDate: Date;
29

    
30
  @ViewChild('subscribeToTermsModal')
31
  public subscribeToTermsModal: ConfirmationDialogComponent;
32

    
33
  open: boolean = true;
34

    
35
  constructor(private router: Router,
36
              private authService: AuthenticationService,
37
              private matomoTracker: MatomoTracker,
38
              private repositoryService: RepositoryService,
39
              private fb: FormBuilder) {
40

    
41
    console.log('21-06-2019. Fixed matomo to log userIds?');
42

    
43
    /*disabling console.log in production*/
44
    if ( environment.production === true ) {
45
      console.log = function () {};
46
    }
47

    
48
    // URL of the SPA to redirect the user to after login
49
    // this.authService.redirectUrl = "/dashboard";
50

    
51
    if (window.location.pathname.includes('/compatibility/browseHistory/')) {
52
      this.authService.redirectUrl = window.location.pathname;
53
      console.log('redirectUrl', this.authService.redirectUrl);
54
    }
55

    
56
    this.authService.tryLogin();
57
  }
58

    
59
  getReposOfUser(): void {
60
    this.repositoryService.getRepositoriesSnippetsOfUser()
61
      .subscribe(
62
        repos => { this.reposOfUser = repos; },
63
        error => { console.log(error); },
64
        () => {
65
          console.log(this.reposOfUser);
66
          this.reposOfUser.forEach( repo => {
67
            // TODO: change !repo.consentTermsOfUse check when it gets a non-null value
68
            if (this.authService.isLoggedIn && !repo.consentTermsOfUse) {
69
              this.addTerm(repo.officialname, repo.id, repo.consentTermsOfUse);
70
              this.isModalShown = true;
71
            }
72
          });
73
        }
74
      );
75
  }
76

    
77
  updateTerms() {
78
    /*   update consentTermsOfUse, consentTermsOfUseDate(?)
79
         depending on  what value will consentTermsOfUse hold
80
         Also what type of consentTermsOfUse will be? boolean or string */
81
    for (let i = 0; i < this.terms.length; i++) {
82
      const  id = this.terms.controls[i].get('id').value;
83
      if (this.terms.controls[i].get('accept').value === true) {
84
        console.log(`Agreed to the Terms of Use for: `, id);
85
      }
86
    }
87
    this.consentTermsOfUseDate = new Date(Date.now());
88
    console.log(this.consentTermsOfUseDate);
89
    console.log('will POST when backend is ready');
90
  }
91

    
92
  ngOnInit() {
93
    this.router.events.subscribe((evt) => {
94
      if (!(evt instanceof NavigationEnd)) {
95
        return;
96
      }
97
      if (this.authService.isLoggedIn) {
98
        this.matomoTracker.setUserId(this.authService.getUserEmail());
99
      }
100
      window.scrollTo(0, 0);
101
    });
102

    
103
    // this.getReposOfUser();
104

    
105
  }
106

    
107
  addTerm(name: string, id: string, consent: string) {
108
    this.terms.push(this.newTerm(name, id, consent));
109
  }
110

    
111
  newTerm(name: string, id: string, consent: string): FormGroup {
112
    return this.fb.group({
113
      id: [id],
114
      name: [name],
115
      accept: [(consent ? consent : true)]
116
    });
117
  }
118

    
119
  get terms() {
120
    return this.agreementForm.get('terms') as FormArray;
121
  }
122

    
123
  isLandingRoute() {
124
    // console.log('Is home route? Route is: ' + this.router.url);
125
    return (this.router.url === '/') || (this.router.url === '/home') || (this.router.url === '/about');
126
  }
127

    
128
  public toggleOpen(event: MouseEvent) {
129
    event.preventDefault();
130
    this.open = !this.open;
131
  }
132

    
133
  // ngAfterContentInit() {
134
  //
135
  //   // this.loadScript('assets/js/common.js');
136
  //   // this.loadScript('assets/js/uikit_custom.js');
137
  //   // this.loadScript('assets/js/altair_admin_common.js');
138
  //   this.loadScript('assets/js/altair_admin_common.min.js');
139
  //
140
  //   // setTimeout( () => {
141
  //   //   // this.loadScript('assets/js/common.js');
142
  //   //   // this.loadScript('assets/js/uikit_custom.js');
143
  //   //   this.loadScript('assets/js/altair_admin_common.min.js');
144
  //   // }, 2000);
145
  //
146
  //   // $.getScript('assets/js/altair_admin_common.min.js');
147
  //
148
  //
149
  //
150
  //   // // Load the script
151
  //   // // var self = this;
152
  //   //
153
  //   // var script = <HTMLScriptElement>document.createElement("SCRIPT");
154
  //   // script.src = 'assets/js/altair_admin_common.min.js';
155
  //   // script.type = 'text/javascript';
156
  //   // // self.script = <HTMLScriptElement>document.createElement("SCRIPT");
157
  //   // // self.script.src = '../Content/js/settings.js';
158
  //   // // self.script.type = 'text/javascript';
159
  //   // document.getElementsByTagName("head")[0].appendChild(script);
160
  // }
161
  //
162
  // public loadScript(url) {
163
  //   console.log('preparing to load...')
164
  //   let node = document.createElement('script');
165
  //   node.src = url;
166
  //   node.type = 'text/javascript';
167
  //   document.getElementsByTagName('head')[0].appendChild(node);
168
  // }
169

    
170
}
(5-5/6)