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
        }
48
        this.getCorrectTopic();
49
      }
50
    );
51
  }
52

    
53

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

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

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

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

    
84

    
85
  isHighlighted(item: any, itemList: any[]) {
86
    return itemList.some(x => x === item);
87
  }
88

    
89
  getCorrectTopic() {
90
    const temp = this.eventsPage.topic.split('/');
91
    this.topic = temp[0];
92
    for (let i = 1; i < temp.length; i++) {
93
      this.topic += ` | ${temp[i]}`;
94
    }
95
  }
96
}
(8-8/13)