aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/user-subscription/subscribe-button.component.ts
blob: 46d6dbaf7f861253e72b611fce629007ccc8dc27 (plain) (blame)
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
import { Component, Input, OnInit } from '@angular/core'
import { AuthService } from '@app/core'
import { RestExtractor } from '@app/shared/rest'
import { RedirectService } from '@app/core/routing/redirect.service'
import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
import { NotificationsService } from 'angular2-notifications'
import { I18n } from '@ngx-translate/i18n-polyfill'

@Component({
  selector: 'my-subscribe-button',
  templateUrl: './subscribe-button.component.html',
  styleUrls: [ './subscribe-button.component.scss' ]
})
export class SubscribeButtonComponent implements OnInit {
  @Input() videoChannel: VideoChannel
  @Input() displayFollowers = false

  subscribed: boolean

  constructor (
    private authService: AuthService,
    private restExtractor: RestExtractor,
    private redirectService: RedirectService,
    private notificationsService: NotificationsService,
    private userSubscriptionService: UserSubscriptionService,
    private i18n: I18n
  ) { }

  get uri () {
    return this.videoChannel.name + '@' + this.videoChannel.host
  }

  ngOnInit () {
    this.userSubscriptionService.isSubscriptionExists(this.uri)
      .subscribe(
        exists => this.subscribed = exists,

        err => this.notificationsService.error(this.i18n('Error'), err.message)
      )
  }

  subscribe () {
    this.userSubscriptionService.addSubscription(this.uri)
      .subscribe(
        () => {
          this.subscribed = true

          this.notificationsService.success(
            this.i18n('Subscribed'),
            this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
          )
        },

          err => this.notificationsService.error(this.i18n('Error'), err.message)
      )
  }

  unsubscribe () {
    this.userSubscriptionService.deleteSubscription(this.uri)
        .subscribe(
          () => {
            this.subscribed = false

            this.notificationsService.success(
              this.i18n('Unsubscribed'),
              this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
            )
          },

          err => this.notificationsService.error(this.i18n('Error'), err.message)
        )
  }
}