]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/friends/friend-list/friend-list.component.ts
Remove one pod (#76)
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / friends / friend-list / friend-list.component.ts
index 700ea7a69fa5175d8e6c425b32630d1363192a68..822a112ccc05ebf9c405a46b7c097d36a6440424 100644 (file)
-import { Component, OnInit } from '@angular/core';
+import { Component } from '@angular/core'
 
-import { NotificationsService } from 'angular2-notifications';
+import { NotificationsService } from 'angular2-notifications'
+import { ServerDataSource } from 'ng2-smart-table'
 
-import { Friend, FriendService } from '../shared';
+import { ConfirmService } from '../../../core'
+import { Utils } from '../../../shared'
+import { FriendService } from '../shared'
+import { Pod } from '../../../../../../shared'
 
 @Component({
   selector: 'my-friend-list',
   templateUrl: './friend-list.component.html',
   styleUrls: [ './friend-list.component.scss' ]
 })
-export class FriendListComponent implements OnInit {
-  friends: Friend[];
+export class FriendListComponent {
+  friendsSource = null
+  tableSettings = {
+    mode: 'external',
+    attr: {
+      class: 'table-hover'
+    },
+    hideSubHeader: true,
+    actions: {
+      position: 'right',
+      add: false,
+      edit: false,
+      delete: true
+    },
+    delete: {
+      deleteButtonContent: Utils.getRowDeleteButton()
+    },
+    columns: {
+      id: {
+        title: 'ID',
+        sort: false,
+        sortDirection: 'asc'
+      },
+      host: {
+        title: 'Host',
+        sort: false
+      },
+      email: {
+        title: 'Email',
+        sort: false
+      },
+      score: {
+        title: 'Score',
+        sort: false
+      },
+      createdAt: {
+        title: 'Created Date',
+        sort: false,
+        valuePrepareFunction: Utils.dateToHuman
+      }
+    }
+  }
 
-  constructor(
+  constructor (
     private notificationsService: NotificationsService,
+    private confirmService: ConfirmService,
     private friendService: FriendService
-  ) {  }
-
-  ngOnInit() {
-    this.getFriends();
+  ) {
+    this.friendsSource = this.friendService.getDataSource()
   }
 
-  quitFriends() {
-    if (!confirm('Are you sure?')) return;
+  hasFriends () {
+    return this.friendsSource.count() !== 0
+  }
 
-    this.friendService.quitFriends().subscribe(
-      status => {
-        this.notificationsService.success('Sucess', 'Friends left!');
+  quitFriends () {
+    const confirmMessage = 'Do you really want to quit your friends? All their videos will be deleted.'
+    this.confirmService.confirm(confirmMessage, 'Quit friends').subscribe(
+      res => {
+        if (res === false) return
 
-        this.getFriends();
-      },
+        this.friendService.quitFriends().subscribe(
+          status => {
+            this.notificationsService.success('Success', 'Friends left!')
+            this.friendsSource.refresh()
+          },
 
-      err => this.notificationsService.error('Error', err.text)
-    );
+          err => this.notificationsService.error('Error', err.text)
+        )
+      }
+    )
   }
 
-  private getFriends() {
-    this.friendService.getFriends().subscribe(
-      res => this.friends = res.friends,
+  removeFriend ({ data }) {
+    const confirmMessage = 'Do you really want to remove this friend ? All its videos will be deleted.'
+    const friend: Pod = data
+
+    this.confirmService.confirm(confirmMessage, 'Remove').subscribe(
+      res => {
+        if (res === false) return
+
+        this.friendService.removeFriend(friend).subscribe(
+         status => {
+           this.notificationsService.success('Success', 'Friend removed')
+           this.friendsSource.refresh()
+         },
 
-      err => this.notificationsService.error('Error', err.text)
-    );
+         err => this.notificationsService.error('Error', err.text)
+       )
+      }
+    )
   }
 }