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

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

    
73
    );
74
  }
75

    
76
}
(10-10/13)