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
    body.classList.remove("landing");
31
    body.classList.add("dashboard");
32
  }
33

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

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

    
76
    );
77
  }
78

    
79
}
(4-4/7)