Project

General

Profile

1
import { Component, OnInit } from '@angular/core';
2
import { ActivatedRoute } from '@angular/router';
3
import { BrokerService } from '../../services/broker.service';
4
import { loadingEvents, noEventsForTopic, noServiceMessage } from '../../domain/shared-messages';
5
import { EventsPage } from '../../domain/typeScriptClasses';
6

    
7
@Component ({
8
  selector: 'app-content-notifications-of-subscription',
9
  templateUrl: 'content-notifications-of-subscription.component.html'
10
})
11

    
12
export class ContentNotificationsOfSubscriptionComponent implements OnInit {
13
  noEvents: string;
14
  errorMessage: string;
15
  loadingMessage: string;
16

    
17
  subId: string;
18
  topic: string;
19
  eventsPage: EventsPage;
20
  currentPage: number;  /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
21

    
22
  constructor(private route: ActivatedRoute,
23
              private brokerService: BrokerService) {}
24

    
25
  ngOnInit () {
26
    this.subId = this.route.snapshot.paramMap.get('id');
27
    this.currentPage = 0; /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
28
    this.getEventsPage(0);
29
  }
30

    
31
  getEventsPage(page: number) {
32
    this.noEvents = '';
33
    this.errorMessage = '';
34
    this. loadingMessage = loadingEvents;
35
    this.brokerService.getNotificationsBySubscriptionId(this.subId, page,10).subscribe(
36
      events => this.eventsPage = events,
37
      error => {
38
        this.loadingMessage = '';
39
        this.errorMessage = noServiceMessage;
40
        console.log(error);
41
      },
42
      () => {
43
        this.loadingMessage = '';
44
        console.log(this.eventsPage);
45
        if (!this.eventsPage.total)
46
          this.noEvents = noEventsForTopic;
47
        this.getCorrectTopic();
48
      }
49
    );
50
  }
51

    
52

    
53
  goToNextPage(){
54
    /* RESTORE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
55
    /*if(this.eventsPage.currPage < this.eventsPage.totalPages) {
56
      console.log(`Get me page ${this.eventsPage.currPage+1}!`);
57
      this.getEventsPage(this.eventsPage.currPage+1);
58
    }*/
59

    
60
    /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
61
    if( (this.currentPage+1) < this.eventsPage.totalPages) {
62
      this.currentPage = this.currentPage+1;
63
      console.log(`Get me page ${this.currentPage}!`);
64
      this.getEventsPage(this.currentPage);
65
    }
66
  }
67

    
68
  goToPreviousPage(){
69
    /* RESTORE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
70
    /*if(this.eventsPage.currPage > 0) {
71
      console.log(`Get me page ${this.eventsPage.currPage-1}!`);
72
      this.getEventsPage(this.eventsPage.currPage-1);
73
    }*/
74

    
75
    /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
76
    if(this.currentPage > 0) {
77
      this.currentPage = this.currentPage-1;
78
      console.log(`Get me page ${this.currentPage}!`);
79
      this.getEventsPage(this.currentPage);
80
    }
81
  }
82

    
83

    
84
  getCorrectTopic() {
85
    let temp = this.eventsPage.topic.split('/');
86
    this.topic = temp[0];
87
    for (let i=1; i<temp.length; i++){
88
      this.topic += ` | ${temp[i]}`;
89
    }
90
  }
91
}
(8-8/13)