]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/admin/friends/friend-add/friend-add.component.ts
Server: set manually the post host of a remote video throught the
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / friends / friend-add / friend-add.component.ts
index ffc499b92bb65ccc2ecd9c90fa945474e886934f..22b0241b4274c35aba18b0c5e6cff0b08012238e 100644 (file)
@@ -1,21 +1,34 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
+import { FormControl, FormGroup } from '@angular/forms';
 import { Router } from '@angular/router';
 
+import { validateHost } from '../../../shared';
 import { FriendService } from '../shared';
 
 @Component({
   selector: 'my-friend-add',
-  template: require('./friend-add.component.html'),
-  styles: [ require('./friend-add.component.scss') ]
+  templateUrl: './friend-add.component.html',
+  styleUrls: [ './friend-add.component.scss' ]
 })
-export class FriendAddComponent {
-  urls = [ '' ];
+export class FriendAddComponent implements OnInit {
+  form: FormGroup;
+  hosts = [ ];
   error: string = null;
 
   constructor(private router: Router, private friendService: FriendService) {}
 
+  ngOnInit() {
+    this.form = new FormGroup({});
+    this.addField();
+  }
+
   addField() {
-    this.urls.push('');
+    this.form.addControl(`host-${this.hosts.length}`, new FormControl('', [ validateHost ]));
+    this.hosts.push('');
+  }
+
+  canMakeFriends() {
+    return window.location.protocol === 'https:';
   }
 
   customTrackBy(index: number, obj: any): any {
@@ -23,78 +36,77 @@ export class FriendAddComponent {
   }
 
   displayAddField(index: number) {
-    return index === (this.urls.length - 1);
+    return index === (this.hosts.length - 1);
   }
 
   displayRemoveField(index: number) {
-    return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1);
+    return (index !== 0 || this.hosts.length > 1) && index !== (this.hosts.length - 1);
+  }
+
+  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;
+    }
+
+    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;
+    } else {
+      return this.form.controls[`host-${lastIndex}`].valid;
+    }
   }
 
   removeField(index: number) {
-    this.urls.splice(index, 1);
+    // Remove the last control
+    this.form.removeControl(`host-${this.hosts.length - 1}`);
+    this.hosts.splice(index, 1);
   }
 
   makeFriends() {
     this.error = '';
 
-    const notEmptyUrls = this.getNotEmptyUrls();
-    if (notEmptyUrls.length === 0) {
-      this.error = 'You need to specify at less 1 url.';
+    const notEmptyHosts = this.getNotEmptyHosts();
+    if (notEmptyHosts.length === 0) {
+      this.error = 'You need to specify at least 1 host.';
       return;
     }
 
-    if (!this.isUrlsRegexValid(notEmptyUrls)) {
-      this.error = 'Some url(s) are not valid.';
+    if (!this.isHostsUnique(notEmptyHosts)) {
+      this.error = 'Hosts need to be unique.';
       return;
     }
 
-    if (!this.isUrlsUnique(notEmptyUrls)) {
-      this.error = 'Urls need to be unique.';
-      return;
-    }
-
-    const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - ');
+    const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyHosts.join('\n - ');
     if (!confirm(confirmMessage)) return;
 
-    this.friendService.makeFriends(notEmptyUrls).subscribe(
+    this.friendService.makeFriends(notEmptyHosts).subscribe(
       status => {
-        if (status === 409) {
-          alert('Already made friends!');
-        } else {
+        // TODO: extractdatastatus
+        // if (status === 409) {
+        //   alert('Already made friends!');
+        // } else {
           alert('Make friends request sent!');
           this.router.navigate([ '/admin/friends/list' ]);
-        }
+        // }
       },
-      error => alert(error)
+      error => alert(error.text)
     );
   }
 
-  private getNotEmptyUrls() {
-    const notEmptyUrls = [];
-
-    this.urls.forEach((url) => {
-      if (url !== '') notEmptyUrls.push(url);
-    });
-
-    return notEmptyUrls;
-  }
-
-  // Temporary
-  // Use HTML validators
-  private isUrlsRegexValid(urls: string[]) {
-    let res = true;
+  private getNotEmptyHosts() {
+    const notEmptyHosts = [];
 
-    const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$');
-    urls.forEach((url) => {
-      if (urlRegex.test(url) === false) {
-        res = false;
-      }
+    Object.keys(this.form.value).forEach((hostKey) => {
+      const host = this.form.value[hostKey];
+      if (host !== '') notEmptyHosts.push(host);
     });
 
-    return res;
+    return notEmptyHosts;
   }
 
-  private isUrlsUnique(urls: string[]) {
-    return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
+  private isHostsUnique(hosts: string[]) {
+    return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host));
   }
 }