Project

General

Profile

1
import {Component, OnInit} from '@angular/core';
2
import { SimpleSubscriptionDesc } from '../../domain/typeScriptClasses';
3
import { AuthenticationService } from '../../services/authentication.service';
4
import { BrokerService } from '../../services/broker.service';
5
import { deletingSubscription, deletingSubscriptionError, deletingSubscriptionSuccess,
6
         loadingSubscriptions, noServiceMessage, noSubscriptionsFound } from '../../domain/shared-messages';
7

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

    
13
export class ContentNotificationsComponent implements OnInit {
14
  errorMessage: string;
15
  successMessage: string;
16
  loadingMessage: string;
17
  noSubscriptions: string;
18

    
19
  subscrOfUser: Map<string, SimpleSubscriptionDesc[]> = new Map<string, SimpleSubscriptionDesc[]>();
20
  subKeys: string[];
21

    
22
  constructor(private authService: AuthenticationService,
23
              private brokerService: BrokerService) {}
24

    
25
  ngOnInit() {
26
    this.getSubscriptions();
27
    let body = document.getElementsByTagName('body')[0];
28
    body.classList.remove("top_bar_active");   //remove the class
29
    body.classList.remove("page_heading_active");
30
  }
31

    
32
  getSubscriptions() {
33
    this.errorMessage = '';
34
    this.loadingMessage = loadingSubscriptions;
35
    this.subKeys = [];
36
    this.brokerService.getSimpleSubscriptionsOfUser().subscribe(
37
      subscrs => this.subscrOfUser = subscrs,
38
      error => {
39
        console.log(`getSimpleSubscriptions returned error`);
40
        console.log(error);
41
        this.loadingMessage = '';
42
        this.errorMessage = noServiceMessage;
43
      },
44
      () => {
45
        this.loadingMessage = '';
46
        for (const key in this.subscrOfUser) {
47
          this.subKeys.push(key);
48
          console.log(key);
49
        }
50
        if (!this.subKeys.length) {
51
          this.noSubscriptions = noSubscriptionsFound;
52
        }
53
      }
54
    );
55
  }
56

    
57
  deleteSubscription(key: string) {
58
    this.loadingMessage = deletingSubscription;
59
    this.successMessage = '';
60
    this.errorMessage = '';
61
    this.brokerService.unsubscribe(key).subscribe(
62
      response => console.log(`unsubscribe responded with ${response}`),
63
      error => {
64
        console.log(error);
65
        this.loadingMessage = '';
66
        this.errorMessage = deletingSubscriptionError;
67
      },
68
      () => {
69
        this.loadingMessage = '';
70
        this.successMessage = deletingSubscriptionSuccess;
71
        this.getSubscriptions();
72
      }
73

    
74
    );
75
  }
76

    
77
}
(10-10/13)