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
import {st} from '@angular/core/src/render3';
19

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

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

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

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

    
45
  dataForm: FormGroup;
46

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

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

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

    
77
    this.isModalShown = false;
78
  }
79

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

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

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

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

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

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

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

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

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

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

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

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

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

    
213
}
(2-2/8)