Project

General

Profile

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

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

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

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

    
24
  constructor(private authService: AuthenticationService,
25
              private brokerService: BrokerService) {}
26

    
27
  ngOnInit() {
28
    this.getSubscriptions();
29
  }
30

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

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

    
70
    );
71
  }
72

    
73
}
(10-10/13)