Project

General

Profile

1 50041 myrto.kouk
import {Component, OnInit} from "@angular/core";
2 50883 myrto.kouk
import { SimpleSubscriptionDesc, Subscription } from '../../domain/typeScriptClasses';
3 50746 myrto.kouk
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 50041 myrto.kouk
10
@Component ({
11
  selector: 'app-content-notifications',
12
  templateUrl: 'content-notifications.component.html'
13
})
14
15
export class ContentNotificationsComponent implements OnInit {
16 50746 myrto.kouk
  errorMessage: string;
17
  successMessage: string;
18
  loadingMessage: string;
19
  noSubscriptions: string;
20 50041 myrto.kouk
21 51751 myrto.kouk
  subscrOfUser: Map<string,SimpleSubscriptionDesc[]> = new Map<string,SimpleSubscriptionDesc[]>();
22 52772 myrto.kouk
  subKeys: string[];
23 50041 myrto.kouk
24 50746 myrto.kouk
  constructor(private authService: AuthenticationService,
25
              private brokerService: BrokerService) {}
26
27
  ngOnInit() {
28
    this.getSubscriptions();
29
  }
30
31
  getSubscriptions() {
32 52772 myrto.kouk
    this.errorMessage = '';
33 50746 myrto.kouk
    this.loadingMessage = loadingSubscriptions;
34 52772 myrto.kouk
    this.subKeys = [];
35 51275 myrto.kouk
    this.brokerService.getSimpleSubscriptionsOfUser(this.authService.getUserEmail()).subscribe(
36 50746 myrto.kouk
      subscrs => this.subscrOfUser = subscrs,
37
      error => {
38 51751 myrto.kouk
        console.log(`getSimpleSubscriptions returned error`);
39 50746 myrto.kouk
        console.log(error);
40
        this.loadingMessage = '';
41
        this.errorMessage = noServiceMessage;
42
      },
43
      () => {
44
        this.loadingMessage = '';
45 51240 myrto.kouk
        for (let key in this.subscrOfUser) {
46 50765 myrto.kouk
          this.subKeys.push(key);
47
          console.log(key);
48
        }
49
        if (!this.subKeys.length) {
50 50746 myrto.kouk
          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 52772 myrto.kouk
        this.getSubscriptions();
71 50746 myrto.kouk
      }
72
73
    );
74
  }
75
76 50041 myrto.kouk
}