refactor subscribe button and comment-add for visitor-interact UX (#1100)
[oweals/peertube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
index ba7acf69adf7ef9c0086cc4c8790444dd1709823..e3c758942703c91bd5c316e570949d065edfc3de 100644 (file)
@@ -1,7 +1,6 @@
 import { Component, Input, OnInit } from '@angular/core'
+import { Router } from '@angular/router'
 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'
@@ -21,8 +20,7 @@ export class SubscribeButtonComponent implements OnInit {
 
   constructor (
     private authService: AuthService,
-    private restExtractor: RestExtractor,
-    private redirectService: RedirectService,
+    private router: Router,
     private notificationsService: NotificationsService,
     private userSubscriptionService: UserSubscriptionService,
     private i18n: I18n
@@ -32,16 +30,30 @@ export class SubscribeButtonComponent implements OnInit {
     return this.videoChannel.name + '@' + this.videoChannel.host
   }
 
+  get uriAccount () {
+    return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
+  }
+
   ngOnInit () {
-    this.userSubscriptionService.isSubscriptionExists(this.uri)
-      .subscribe(
-        res => this.subscribed = res[this.uri],
+    if (this.isUserLoggedIn()) {
+      this.userSubscriptionService.isSubscriptionExists(this.uri)
+        .subscribe(
+          res => this.subscribed = res[this.uri],
 
-        err => this.notificationsService.error(this.i18n('Error'), err.message)
-      )
+          err => this.notificationsService.error(this.i18n('Error'), err.message)
+        )
+    }
   }
 
   subscribe () {
+    if (this.isUserLoggedIn()) {
+      this.localSubscribe()
+    } else {
+      this.gotoLogin()
+    }
+  }
+
+  localSubscribe () {
     this.userSubscriptionService.addSubscription(this.uri)
       .subscribe(
         () => {
@@ -58,6 +70,12 @@ export class SubscribeButtonComponent implements OnInit {
   }
 
   unsubscribe () {
+    if (this.isUserLoggedIn()) {
+      this.localUnsubscribe()
+    }
+  }
+
+  localUnsubscribe () {
     this.userSubscriptionService.deleteSubscription(this.uri)
         .subscribe(
           () => {
@@ -72,4 +90,16 @@ export class SubscribeButtonComponent implements OnInit {
           err => this.notificationsService.error(this.i18n('Error'), err.message)
         )
   }
+
+  isUserLoggedIn () {
+    return this.authService.isLoggedIn()
+  }
+
+  gotoLogin () {
+    this.router.navigate([ '/login' ])
+  }
+
+  rssOpen () {
+    window.open('')
+  }
 }