1 import { Component, Input, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService } from '@app/core'
4 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6 import { NotificationsService } from 'angular2-notifications'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
10 selector: 'my-subscribe-button',
11 templateUrl: './subscribe-button.component.html',
12 styleUrls: [ './subscribe-button.component.scss' ]
14 export class SubscribeButtonComponent implements OnInit {
15 @Input() videoChannel: VideoChannel
16 @Input() displayFollowers = false
17 @Input() size: 'small' | 'normal' = 'normal'
22 private authService: AuthService,
23 private router: Router,
24 private notificationsService: NotificationsService,
25 private userSubscriptionService: UserSubscriptionService,
30 return this.videoChannel.name + '@' + this.videoChannel.host
34 return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
38 if (this.isUserLoggedIn()) {
39 this.userSubscriptionService.isSubscriptionExists(this.uri)
41 res => this.subscribed = res[this.uri],
43 err => this.notificationsService.error(this.i18n('Error'), err.message)
49 if (this.isUserLoggedIn()) {
57 this.userSubscriptionService.addSubscription(this.uri)
60 this.subscribed = true
62 this.notificationsService.success(
63 this.i18n('Subscribed'),
64 this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
68 err => this.notificationsService.error(this.i18n('Error'), err.message)
73 if (this.isUserLoggedIn()) {
74 this.localUnsubscribe()
79 this.userSubscriptionService.deleteSubscription(this.uri)
82 this.subscribed = false
84 this.notificationsService.success(
85 this.i18n('Unsubscribed'),
86 this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
90 err => this.notificationsService.error(this.i18n('Error'), err.message)
95 return this.authService.isLoggedIn()
99 this.router.navigate([ '/login' ])