1
0
Fork 0
peertube/client/src/app/shared/user-subscription/subscribe-button.component.ts

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-08-21 14:18:59 +00:00
import { Component, Input, OnInit } from '@angular/core'
import { Router } from '@angular/router'
import { AuthService } from '@app/core'
2018-08-21 14:18:59 +00:00
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
2018-08-23 15:58:39 +00:00
@Input() size: 'small' | 'normal' = 'normal'
2018-08-21 14:18:59 +00:00
subscribed: boolean
constructor (
private authService: AuthService,
private router: Router,
2018-08-21 14:18:59 +00:00
private notificationsService: NotificationsService,
private userSubscriptionService: UserSubscriptionService,
private i18n: I18n
) { }
get uri () {
return this.videoChannel.name + '@' + this.videoChannel.host
}
get uriAccount () {
return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
}
2018-08-21 14:18:59 +00:00
ngOnInit () {
if (this.isUserLoggedIn()) {
this.userSubscriptionService.isSubscriptionExists(this.uri)
.subscribe(
res => this.subscribed = res[this.uri],
2018-08-21 14:18:59 +00:00
err => this.notificationsService.error(this.i18n('Error'), err.message)
)
}
2018-08-21 14:18:59 +00:00
}
subscribe () {
if (this.isUserLoggedIn()) {
this.localSubscribe()
} else {
this.gotoLogin()
}
}
localSubscribe () {
2018-08-21 14:18:59 +00:00
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 () {
if (this.isUserLoggedIn()) {
this.localUnsubscribe()
}
}
localUnsubscribe () {
2018-08-21 14:18:59 +00:00
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)
)
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
}
gotoLogin () {
this.router.navigate([ '/login' ])
}
rssOpen () {
window.open('')
}
2018-08-21 14:18:59 +00:00
}