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