File size: 3,806 Bytes
11452ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Component, OnInit, EventEmitter } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SubscribeDialogComponent } from 'app/dialogs/subscribe-dialog/subscribe-dialog.component';
import { PostsService } from 'app/posts.services';
import { Router } from '@angular/router';
import { SubscriptionInfoDialogComponent } from 'app/dialogs/subscription-info-dialog/subscription-info-dialog.component';
import { EditSubscriptionDialogComponent } from 'app/dialogs/edit-subscription-dialog/edit-subscription-dialog.component';

@Component({
  selector: 'app-subscriptions',
  templateUrl: './subscriptions.component.html',
  styleUrls: ['./subscriptions.component.scss']
})
export class SubscriptionsComponent implements OnInit {

  playlist_subscriptions = [];
  channel_subscriptions = [];
  subscriptions = null;

  subscriptions_loading = false;

  constructor(private dialog: MatDialog, public postsService: PostsService, private router: Router, private snackBar: MatSnackBar) { }

  ngOnInit() {
    if (this.postsService.initialized) {
      this.getSubscriptions();
    }
    this.postsService.service_initialized.subscribe(init => {
      if (init) {
        this.getSubscriptions();
      }
    });
  }

  getSubscriptions(show_loading = true) {
    if (show_loading) this.subscriptions_loading = true;
    this.subscriptions = null;
    this.postsService.getAllSubscriptions().subscribe(res => {
      this.channel_subscriptions = [];
      this.playlist_subscriptions = [];
      this.subscriptions_loading = false;
      this.subscriptions = res['subscriptions'];
      if (!this.subscriptions) {
        // set it to an empty array so it can notify the user there are no subscriptions
        this.subscriptions = [];
        return;
      }

      for (let i = 0; i < this.subscriptions.length; i++) {
        const sub = this.subscriptions[i];

        // parse subscriptions into channels and playlists
        if (sub.isPlaylist) {
          this.playlist_subscriptions.push(sub);
        } else {
          this.channel_subscriptions.push(sub);
        }
      }
    }, err => {
      this.subscriptions_loading = false;
      console.error('Failed to get subscriptions');
      this.openSnackBar('ERROR: Failed to get subscriptions!', 'OK.');
    });
  }

  goToSubscription(sub) {
    this.router.navigate(['/subscription', {id: sub.id}]);
  }

  openSubscribeDialog() {
    const dialogRef = this.dialog.open(SubscribeDialogComponent, {
      maxWidth: 500,
      width: '80vw'
    });

    dialogRef.afterClosed().subscribe(result => {
      if (result) {
        if (result.isPlaylist) {
          this.playlist_subscriptions.push(result);
        } else {
          this.channel_subscriptions.push(result);
        }
        this.postsService.reloadSubscriptions();
      }
    });
  }

  showSubInfo(sub) {
    const unsubbedEmitter = new EventEmitter<any>();
    const dialogRef = this.dialog.open(SubscriptionInfoDialogComponent, {
      data: {
        sub: sub,
        unsubbedEmitter: unsubbedEmitter
      }
    });
    unsubbedEmitter.subscribe(success => {
      if (success) {
        this.openSnackBar(`${sub.name} successfully deleted!`)
        this.getSubscriptions();
        this.postsService.reloadSubscriptions();
      }
    })
  }

  editSubscription(sub) {
    const dialogRef = this.dialog.open(EditSubscriptionDialogComponent, {
      data: {
        sub: this.postsService.getSubscriptionByID(sub.id)
      }
    });
    dialogRef.afterClosed().subscribe(() => {
      this.getSubscriptions(false);
    });
  }

  // snackbar helper
  public openSnackBar(message: string, action = '') {
    this.snackBar.open(message, action, {
      duration: 2000,
    });
  }

}