]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/friends/friend-add/friend-add.component.ts
Remove ng2 file upload module
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / friends / friend-add / friend-add.component.ts
index a271970ae70c7e60a20963f4023911ab924637bd..6580e1b881492379ce55ce59abe1d1f493b68ff5 100644 (file)
@@ -1,11 +1,12 @@
-import { Component, OnInit } from '@angular/core';
-import { FormControl, FormGroup } from '@angular/forms';
-import { Router } from '@angular/router';
+import { Component, OnInit } from '@angular/core'
+import { FormControl, FormGroup } from '@angular/forms'
+import { Router } from '@angular/router'
 
-import { NotificationsService } from 'angular2-notifications';
+import { NotificationsService } from 'angular2-notifications'
 
-import { validateHost } from '../../../shared';
-import { FriendService } from '../shared';
+import { ConfirmService } from '../../../core'
+import { validateHost } from '../../../shared'
+import { FriendService } from '../shared'
 
 @Component({
   selector: 'my-friend-add',
@@ -13,102 +14,108 @@ import { FriendService } from '../shared';
   styleUrls: [ './friend-add.component.scss' ]
 })
 export class FriendAddComponent implements OnInit {
-  form: FormGroup;
-  hosts = [ ];
-  error: string = null;
+  form: FormGroup
+  hosts: string[] = [ ]
+  error: string = null
 
-  constructor(
+  constructor (
     private router: Router,
     private notificationsService: NotificationsService,
+    private confirmService: ConfirmService,
     private friendService: FriendService
   ) {}
 
-  ngOnInit() {
-    this.form = new FormGroup({});
-    this.addField();
+  ngOnInit () {
+    this.form = new FormGroup({})
+    this.addField()
   }
 
-  addField() {
-    this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ]));
-    this.hosts.push('');
+  addField () {
+    this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ]))
+    this.hosts.push('')
   }
 
-  canMakeFriends() {
-    return window.location.protocol === 'https:';
+  canMakeFriends () {
+    return window.location.protocol === 'https:'
   }
 
-  customTrackBy(index: number, obj: any): any {
-    return index;
+  customTrackBy (index: number, obj: any): any {
+    return index
   }
 
-  displayAddField(index: number) {
-    return index === (this.hosts.length - 1);
+  displayAddField (index: number) {
+    return index === (this.hosts.length - 1)
   }
 
-  displayRemoveField(index: number) {
-    return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1);
+  displayRemoveField (index: number) {
+    return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1)
   }
 
-  isFormValid() {
+  isFormValid () {
     // Do not check the last input
     for (let i = 0; i < this.hosts.length - 1; i++) {
-      if (!this.form.controls[`host-${i}`].valid) return false;
+      if (!this.form.controls[`host-${i}`].valid) return false
     }
 
-    const lastIndex = this.hosts.length - 1;
+    const lastIndex = this.hosts.length - 1
     // If the last input (which is not the first) is empty, it's ok
     if (this.hosts[lastIndex] === '' && lastIndex !== 0) {
-      return true;
+      return true
     } else {
-      return this.form.controls[`host-${lastIndex}`].valid;
+      return this.form.controls[`host-${lastIndex}`].valid
     }
   }
 
-  removeField(index: number) {
+  removeField (index: number) {
     // Remove the last control
-    this.form.removeControl(`host-${this.hosts.length - 1}`);
-    this.hosts.splice(index, 1);
+    this.form.removeControl(`host-${this.hosts.length - 1}`)
+    this.hosts.splice(index, 1)
   }
 
-  makeFriends() {
-    this.error = '';
+  makeFriends () {
+    this.error = ''
 
-    const notEmptyHosts = this.getNotEmptyHosts();
+    const notEmptyHosts = this.getNotEmptyHosts()
     if (notEmptyHosts.length === 0) {
-      this.error = 'You need to specify at least 1 host.';
-      return;
+      this.error = 'You need to specify at least 1 host.'
+      return
     }
 
     if (!this.isHostsUnique(notEmptyHosts)) {
-      this.error = 'Hosts need to be unique.';
-      return;
+      this.error = 'Hosts need to be unique.'
+      return
     }
 
-    const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyHosts.join('\n - ');
-    if (!confirm(confirmMessage)) return;
-
-    this.friendService.makeFriends(notEmptyHosts).subscribe(
-      status => {
-        this.notificationsService.success('Sucess', 'Make friends request sent!');
-        this.router.navigate([ '/admin/friends/list' ]);
-      },
-
-      err => this.notificationsService.error('Error', err.text)
-    );
+    const confirmMessage = 'Are you sure to make friends with:<br /> - ' + notEmptyHosts.join('<br /> - ')
+    this.confirmService.confirm(confirmMessage, 'Make friends').subscribe(
+      res => {
+        if (res === false) return
+
+        this.friendService.makeFriends(notEmptyHosts).subscribe(
+          status => {
+            this.notificationsService.success('Success', 'Make friends request sent!')
+            // Wait requests between pods
+            setTimeout(() => this.router.navigate([ '/admin/friends/list' ]), 1000)
+          },
+
+          err => this.notificationsService.error('Error', err.message)
+        )
+      }
+    )
   }
 
-  private getNotEmptyHosts() {
-    const notEmptyHosts = [];
+  private getNotEmptyHosts () {
+    const notEmptyHosts = []
 
     Object.keys(this.form.value).forEach((hostKey) => {
-      const host = this.form.value[hostKey];
-      if (host !== '') notEmptyHosts.push(host);
-    });
+      const host = this.form.value[hostKey]
+      if (host !== '') notEmptyHosts.push(host)
+    })
 
-    return notEmptyHosts;
+    return notEmptyHosts
   }
 
-  private isHostsUnique(hosts: string[]) {
-    return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host));
+  private isHostsUnique (hosts: string[]) {
+    return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
   }
 }