Project

General

Profile

1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute } from '@angular/router';
3
import { AdvQueryObject, EventsPage, NotificationFrequency, NotificationMode } from '../../domain/typeScriptClasses';
4
import { BrokerService } from '../../services/broker.service';
5
import {
6
  loadingEvents, noEventsForTopic, noEventsWithParams, noServiceMessage, subscribingChooseFrequency,
7
  subscribingToEvents,
8
  subscribingToEventsError, subscribingToeventsSuccess
9
} from '../../domain/shared-messages';
10
import { AbstractControl, FormArray, FormBuilder, FormGroup, Validator, Validators } from '@angular/forms';
11
import { AuthenticationService } from '../../services/authentication.service';
12
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
13

    
14
@Component ({
15
  selector: 'app-content-events-of-repo-eventslist',
16
  templateUrl: 'content-events-of-repo-eventslist.component.html'
17
})
18

    
19
export class ContentEventsOfRepoEventslistComponent implements OnInit {
20
  errorMessage: string;
21
  loadingMessage: string;
22
  successMessage: string;
23
  noEvents: string;
24
  eventsPageInitialized = false;
25

    
26
  topic = '';
27
  correctTopic = '';
28
  repoName = '';
29

    
30
  advanceSearch: AdvQueryObject;
31
  eventsPage: EventsPage;
32

    
33
  group: FormGroup;
34
  readonly titleDefinition = { eventTitle: [''] };
35
  readonly authorDefinition = { eventAuthor: [''] };
36
  readonly subjectDefinition = { eventSubject: [''] };
37
  readonly dateRangeDefinition = { dateFrom: '', dateTo: '' };
38
  readonly groupDefinition = {
39
    trustMin: [+''],
40
    trustMax: [+''],
41
    eventTitles: this.fb.array([this.initControl(this.titleDefinition)]),
42
    eventAuthors: this.fb.array([this.initControl(this.authorDefinition)]),
43
    eventSubjects: this.fb.array([this.initControl(this.subjectDefinition)]),
44
    eventDateRanges: this.fb.array([this.initControl(this.dateRangeDefinition)])
45
  };
46

    
47
  frequencyChoice: string;
48
  userEmail: string;
49
  modalErrorMessage: string;
50

    
51
  @ViewChild('subscribeToEventsModal')
52
  public subscribeToEventsModal: ConfirmationDialogComponent;
53

    
54
  constructor (private route: ActivatedRoute,
55
               private fb: FormBuilder,
56
               private brokerService: BrokerService,
57
               private authService: AuthenticationService) {}
58

    
59
  ngOnInit () {
60
    this.userEmail = this.authService.getUserEmail();
61
    this.getParams();
62
    this.initQuery();
63
    this.initForm();
64
    this.getEventsPage(0);
65
  }
66

    
67
  getParams() {
68
    this.topic = this.route.snapshot.paramMap.get('topic');
69
    this.getCorrectTopic();
70
    this.repoName = this.route.snapshot.paramMap.get('name');
71
  }
72

    
73
  initQuery() {
74
    this.advanceSearch = {
75
      datasource: this.repoName,
76
      topic: this.correctTopic,
77
      titles: [],
78
      subjects: [],
79
      authors: [],
80
      dates: [],
81
      trust: {min:'0', max:'1'},
82
      page: 0
83
    };
84
  }
85

    
86
  initForm() {
87
    this.group = this.fb.group( this.groupDefinition, { validator: checkMinMax } );
88
    this.group.get('trustMin').setValue(0);
89
    this.group.get('trustMax').setValue(1);
90
  }
91

    
92
  initControl(definition: any) {
93
    return this.fb.group(definition);
94
  }
95

    
96
  removeControl(controlName: string, i: number) {
97
    let controlArray = <FormArray>this.group.controls[controlName];
98
    controlArray.removeAt(i);
99
  }
100

    
101
  addControl(controlName: string, definition: any) {
102
    let controlArray = <FormArray>this.group.controls[controlName];
103
    controlArray.push(this.initControl(definition));
104
  }
105

    
106
  clearForm() {
107
    let controlArray: FormArray;
108
    controlArray = <FormArray>this.group.controls['eventTitles'];
109
    controlArray.controls = [];
110
    controlArray.push(this.initControl(this.titleDefinition));
111

    
112
    controlArray = <FormArray>this.group.controls['eventAuthors'];
113
    controlArray.controls = [];
114
    controlArray.push(this.initControl(this.authorDefinition));
115

    
116
    controlArray = <FormArray>this.group.controls['eventSubjects'];
117
    controlArray.controls = [];
118
    controlArray.push(this.initControl(this.subjectDefinition));
119

    
120
    controlArray = <FormArray>this.group.controls['eventDateRanges'];
121
    controlArray.controls = [];
122
    controlArray.push(this.initControl(this.dateRangeDefinition));
123

    
124
    this.group.get('trustMin').setValue(0);
125
    this.group.get('trustMax').setValue(1);
126

    
127
    this.initQuery();
128
    this.getEventsPage(0);
129
  }
130

    
131
  updateQuery() {
132
    let i: number;
133
    let controlArray: FormArray;
134

    
135
    if ( this.group.valid ) {
136
      this.initQuery();
137
      this.advanceSearch.trust.min = this.group.get('trustMin').value;
138
      this.advanceSearch.trust.max = this.group.get('trustMax').value;
139

    
140
      controlArray = <FormArray>this.group.controls['eventTitles'];
141
      for (i = 0; i < controlArray.length; i++) {
142
        if (controlArray.at(i).get('eventTitle').value) {
143
          this.advanceSearch.titles.push(controlArray.at(i).get('eventTitle').value);
144
        }
145
      }
146
      controlArray = <FormArray>this.group.controls['eventAuthors'];
147
      for (i = 0; i < controlArray.length; i++) {
148
        if (controlArray.at(i).get('eventAuthor').value) {
149
          this.advanceSearch.authors.push(controlArray.at(i).get('eventAuthor').value);
150
        }
151
      }
152
      controlArray = <FormArray>this.group.controls['eventSubjects'];
153
      for (i = 0; i < controlArray.length; i++) {
154
        if (controlArray.at(i).get('eventSubject').value) {
155
          this.advanceSearch.subjects.push(controlArray.at(i).get('eventSubject').value);
156
        }
157
      }
158
      controlArray = <FormArray>this.group.controls['eventDateRanges'];
159
      for (i = 0; i < controlArray.length; i++) {
160
        if (controlArray.at(i).get('dateFrom').value && controlArray.at(i).get('dateTo').value) {
161
          this.advanceSearch.dates.push({
162
            min: controlArray.at(i).get('dateFrom').value,
163
            max: controlArray.at(i).get('dateTo').value
164
          });
165
        }
166
      }
167
      console.log(this.advanceSearch);
168
      this.getEventsPage(0);
169
    }
170
  }
171

    
172
  getEventsPage(page: number) {
173
    this.noEvents = '';
174
    this.errorMessage = '';
175
    this.successMessage = '';
176
    this. loadingMessage = loadingEvents;
177
    this.brokerService.advancedShowEvents(page,10,this.advanceSearch).subscribe(
178
      events => this.eventsPage = events,
179
      error => {
180
        this.loadingMessage = '';
181
        this.errorMessage = noServiceMessage;
182
        console.log(error);
183
      },
184
      () => {
185
        this.loadingMessage = '';
186
        console.log(this.eventsPage);
187
        if (!this.eventsPage.total) {
188
          if (!this.eventsPageInitialized)
189
            this.noEvents = noEventsForTopic;
190
          else
191
            this.noEvents = noEventsWithParams;
192
        }
193
        this.eventsPageInitialized = true;
194
      }
195
    );
196
  }
197

    
198
  getCorrectTopic() {
199
    let temp = this.topic.split('|');
200
    this.correctTopic = temp[0];
201
    this.topic = temp[0];
202
    for (let i=1; i<temp.length; i++){
203
      this.correctTopic += `/${temp[i]}`;
204
      this.topic += ` | ${temp[i]}`;
205
    }
206
  }
207

    
208
  goToNextPage(){
209
    if(this.eventsPage.currPage < this.eventsPage.totalPages) {
210
      console.log(`Get me page ${this.eventsPage.currPage+1}!`);
211
      this.getEventsPage(this.eventsPage.currPage+1);
212
    }
213
  }
214

    
215
  goToPreviousPage(){
216
    if(this.eventsPage.currPage > 0) {
217
      console.log(`Get me page ${this.eventsPage.currPage-1}!`);
218
      this.getEventsPage(this.eventsPage.currPage-1);
219
    }
220
  }
221

    
222
  showSubscriptionModal() {
223
    if (this.advanceSearch && this.eventsPage) {
224
      this.subscribeToEventsModal.confirmed = false;
225
      this.subscribeToEventsModal.showModal();
226
    }
227
  }
228

    
229
  choseFrequency(freq: string) {
230
    this.frequencyChoice = freq;
231
  }
232

    
233
  subscribeToEvents() {
234
    this.modalErrorMessage = '';
235
    if (this.frequencyChoice) {
236
      this.subscribeToEventsModal.confirmed = true;
237
      let freq = <NotificationFrequency>this.frequencyChoice;
238
      let mod: NotificationMode = "EMAIL";
239
      let sub = {
240
        subscriber: this.userEmail,
241
        frequency: freq,
242
        mode: mod,
243
        query: this.advanceSearch
244
      };
245
      this.errorMessage = '';
246
      this.successMessage = '';
247
      console.log(JSON.stringify(sub));
248
      this.loadingMessage = subscribingToEvents;
249
      this.brokerService.subscribeToEvent(sub).subscribe(
250
        response => console.log(`subscribeToEvents responded ${response}`),
251
        error => {
252
          this.errorMessage = subscribingToEventsError;
253
          this.loadingMessage = '';
254
        },
255
        () => {
256
          this.loadingMessage = '';
257
          this.successMessage = subscribingToeventsSuccess;
258
        }
259
      );
260
    } else {
261
      this.modalErrorMessage = subscribingChooseFrequency;
262
    }
263
  }
264

    
265
}
266

    
267
export function checkMinMax(c: AbstractControl) {
268
  if( c.get('trustMin').value > c.get('trustMax').value ){
269
    return 'invalid';
270
  }
271
  return null;
272
}
273

    
274
export function checkDates(c: AbstractControl) {
275
  if( c.get('dateFrom').value > c.get('dateTo').value ) {
276
    return 'invalid';
277
  }
278
  return null;
279
}
(2-2/13)