Project

General

Profile

1
import {Component, Input, OnInit, ViewChild} from '@angular/core';
2
import { PiwikService } from '../../services/piwik.service';
3
import { PiwikInfo } from '../../domain/typeScriptClasses';
4
import {
5
  enabledMetricsError,
6
  enablingMetrics,
7
  loadingReposMessage,
8
  reposRetrievalError,
9
  validatePiwikSiteSuccess
10
} from '../../domain/shared-messages';
11
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
12
import {URLParameter} from '../../domain/url-parameter';
13
import {FormBuilder, FormGroup} from '@angular/forms';
14
import {RepositoryService} from '../../services/repository.service';
15
import {ActivatedRoute, Router} from '@angular/router';
16
import {PiwikInfoPage} from '../../domain/page-content';
17
import {environment} from '../../../environments/environment';
18

    
19
@Component ({
20
  selector: 'app-admin-metrics',
21
  templateUrl: 'adminPg-metrics.component.html'
22
})
23

    
24
export class AdminPgMetricsComponent implements OnInit {
25
  piwiks: PiwikInfoPage;
26
  urlParams: URLParameter[] = [];
27
  errorMessage: string;
28
  successMessage: string;
29
  loadingMessage: string;
30

    
31
  modalTitle = 'Approval Confirmation';
32
  modalButton = 'Yes, validate';
33
  isModalShown: boolean;
34

    
35
  formPrepare = {
36
    searchField: '',
37
    orderField: 'REPOSITORY_NAME',
38
    order: 'ASC',
39
    page: '0',
40
    quantity: '25',
41
    from: '0'
42
  };
43

    
44
  dataForm: FormGroup;
45

    
46
  @ViewChild('confirmApprovalModal')
47
  public confirmApprovalModal: ConfirmationDialogComponent;
48
  private pageTotal: number;
49
  private piwiksTotal: number;
50
  public pages = [];
51
  private offset = 2;
52

    
53
  constructor(private piwikService: PiwikService,
54
              private fb: FormBuilder,
55
              private route: ActivatedRoute,
56
              private router: Router) {}
57

    
58
  ngOnInit() {
59
    this.dataForm = this.fb.group(this.formPrepare);
60
    this.urlParams = [];
61
    this.route.queryParams
62
      .subscribe(params => {
63
          for (const i in params) {
64
            this.dataForm.get(i).setValue(params[i]);
65
          }
66
          for (let i in this.dataForm.controls) {
67
            if (this.dataForm.get(i).value) {
68
              this.urlParams.push({key: i, value: [this.dataForm.get(i).value]});
69
            }
70
          }
71
          this.handleChange();
72
        },
73
        error => this.errorMessage = <any>error
74
      );
75

    
76
    this.isModalShown = false;
77
  }
78

    
79
  downloadCSV() {
80
    const url = environment.API_ENDPOINT;
81
    let csvUrlParams = '/piwik/getPiwikSitesForRepos/csv?';
82
    for (let i in this.dataForm.controls) {
83
      if (this.dataForm.get(i).value !== '') {
84
        csvUrlParams = csvUrlParams.concat(i, '=', this.dataForm.get(i).value, '&');
85
      }
86
    }
87
    csvUrlParams = csvUrlParams.split('&page=')[0];
88
    window.open(url + csvUrlParams, '_blank');
89
  }
90

    
91
  getPiwiks() {
92
    this.loadingMessage = loadingReposMessage;
93
    this.piwikService.getPiwikSitesForRepos(this.urlParams)
94
      .subscribe(
95
        piwiks => {
96
          this.piwiks = piwiks;
97
          this.getPages();
98
        },
99
        error => {
100
          console.log(error);
101
          this.loadingMessage = '';
102
          this.errorMessage = reposRetrievalError;
103
        },
104
        () => {
105
          this.loadingMessage = '';
106
          window.scroll(1, 1);
107
        }
108
      );
109
  }
110

    
111
  confirmApproval(repoId: string) {
112
    this.confirmApprovalModal.ids = [repoId];
113
    this.confirmApprovalModal.showModal();
114
  }
115

    
116
  confirmedApproval(ids: string[]) {
117
    const id = ids[0];
118
    console.log(`approving validation of piwik for repo with id: ${id}`);
119
    this.approvePiwik(id);
120
  }
121

    
122
  approvePiwik(id: string) {
123
    this.loadingMessage = enablingMetrics;
124
    this.errorMessage = '';
125
    this.successMessage = '';
126

    
127
    /*this.piwikService.approvePiwikSite(id).subscribe(*/
128
    this.piwikService.markPiwikSiteAsValidated(id).subscribe(
129
      response => console.log(`approvePiwikSite responded: ${JSON.stringify(response)}`),
130
      error => {
131
        console.log(error);
132
        this.loadingMessage = '';
133
        this.errorMessage = enabledMetricsError;
134
      },
135
      () => {
136
        this.loadingMessage = '';
137
        this.errorMessage = '';
138
        this.successMessage = validatePiwikSiteSuccess;
139
        this.getPiwiks();
140
      }
141
    );
142
  }
143

    
144
  handleChange() {
145
    this.urlParams = [];
146
    const map: { [name: string]: string; } = {};
147
    for (let i in this.dataForm.controls) {
148
      if (this.dataForm.get(i).value !== '') {
149
        this.urlParams.push({key: i, value: [this.dataForm.get(i).value]});
150
        map[i] = this.dataForm.get(i).value;
151
      }
152
    }
153

    
154
    this.router.navigate([`/admin/metrics`], {queryParams: map});
155
    this.getPiwiks();
156
  }
157

    
158
  handleChangeAndResetPage() {
159
    this.dataForm.get('page').setValue(0);
160
    this.dataForm.get('from').setValue(0);
161
    this.handleChange();
162
  }
163

    
164
  getPages() {
165
    let addToEndCounter = 0;
166
    let addToStartCounter = 0;
167
    this.pages = [];
168
    this.pageTotal = Math.ceil(this.piwiks.total / (this.dataForm.get('quantity').value));
169
    for ( let i = (+this.dataForm.get('page').value - this.offset); i < (+this.dataForm.get('page').value + 1 + this.offset); ++i ) {
170
      if ( i < 0 ) { addToEndCounter++; }
171
      if ( i >= this.pageTotal ) { addToStartCounter++; }
172
      if ((i >= 0) && (i < this.pageTotal)) {
173
        this.pages.push(i);
174
      }
175
    }
176
    for ( let i = 0; i < addToEndCounter; ++i ) {
177
      if (this.pages.length < this.pageTotal) {
178
        this.pages.push(this.pages.length);
179
      }
180
    }
181
    for ( let i = 0; i < addToStartCounter; ++i ) {
182
      if (this.pages[0] > 0) {
183
        this.pages.unshift(this.pages[0] - 1 );
184
      }
185
    }
186
  }
187

    
188
  selectPage(page) {
189
    this.dataForm.get('page').setValue(page);
190
    this.dataForm.get('from').setValue(((+this.dataForm.get('page').value) * (+this.dataForm.get('quantity').value)));
191
    this.handleChange();
192
  }
193

    
194
  previousPage() {
195
    if (this.dataForm.get('page').value > 0) {
196
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value - 1);
197
      this.dataForm.get('from').setValue(+this.dataForm.get('from').value - +this.dataForm.get('quantity').value);
198
      this.handleChange();
199
    }
200
  }
201

    
202
  nextPage() {
203
    // if ((this.dataForm.get('searchField').value) !== '') { this.piwiksTotal = this.piwiks.to; } else { this.piwiksTotal = this.piwiks.total; }
204
    this.pageTotal = Math.ceil(this.piwiks.total / (this.dataForm.get('quantity').value)) - 1;
205
    if (this.dataForm.get('page').value < this.pageTotal) {
206
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value + 1);
207
      this.dataForm.get('from').setValue(+this.dataForm.get('from').value + +this.dataForm.get('quantity').value);
208
      this.handleChange();
209
    }
210
  }
211

    
212
}
(2-2/8)