Add ability to search video channels
[oweals/peertube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
1 import { Component, Input, OnInit } from '@angular/core'
2 import { AuthService } from '@app/core'
3 import { RestExtractor } from '@app/shared/rest'
4 import { RedirectService } from '@app/core/routing/redirect.service'
5 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
6 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
7 import { NotificationsService } from 'angular2-notifications'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9
10 @Component({
11   selector: 'my-subscribe-button',
12   templateUrl: './subscribe-button.component.html',
13   styleUrls: [ './subscribe-button.component.scss' ]
14 })
15 export class SubscribeButtonComponent implements OnInit {
16   @Input() videoChannel: VideoChannel
17   @Input() displayFollowers = false
18   @Input() size: 'small' | 'normal' = 'normal'
19
20   subscribed: boolean
21
22   constructor (
23     private authService: AuthService,
24     private restExtractor: RestExtractor,
25     private redirectService: RedirectService,
26     private notificationsService: NotificationsService,
27     private userSubscriptionService: UserSubscriptionService,
28     private i18n: I18n
29   ) { }
30
31   get uri () {
32     return this.videoChannel.name + '@' + this.videoChannel.host
33   }
34
35   ngOnInit () {
36     this.userSubscriptionService.isSubscriptionExists(this.uri)
37       .subscribe(
38         res => this.subscribed = res[this.uri],
39
40         err => this.notificationsService.error(this.i18n('Error'), err.message)
41       )
42   }
43
44   subscribe () {
45     this.userSubscriptionService.addSubscription(this.uri)
46       .subscribe(
47         () => {
48           this.subscribed = true
49
50           this.notificationsService.success(
51             this.i18n('Subscribed'),
52             this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
53           )
54         },
55
56           err => this.notificationsService.error(this.i18n('Error'), err.message)
57       )
58   }
59
60   unsubscribe () {
61     this.userSubscriptionService.deleteSubscription(this.uri)
62         .subscribe(
63           () => {
64             this.subscribed = false
65
66             this.notificationsService.success(
67               this.i18n('Unsubscribed'),
68               this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
69             )
70           },
71
72           err => this.notificationsService.error(this.i18n('Error'), err.message)
73         )
74   }
75 }