]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/admin/friends/friend-add/friend-add.component.ts
Server: use preview image for opengraph
[github/Chocobozzz/PeerTube.git] / client / src / app / admin / friends / friend-add / friend-add.component.ts
index ffc499b92bb65ccc2ecd9c90fa945474e886934f..64165a9a5b312547f2771a639a67ffc3407f4662 100644 (file)
@@ -1,20 +1,29 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
+import { FormControl, FormGroup } from '@angular/forms';
 import { Router } from '@angular/router';
 
+import { validateUrl } 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;
+  urls = [ ];
   error: string = null;
 
   constructor(private router: Router, private friendService: FriendService) {}
 
+  ngOnInit() {
+    this.form = new FormGroup({});
+    this.addField();
+  }
+
   addField() {
+    this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
     this.urls.push('');
   }
 
@@ -30,7 +39,24 @@ export class FriendAddComponent {
     return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1);
   }
 
+  isFormValid() {
+    // Do not check the last input
+    for (let i = 0; i < this.urls.length - 1; i++) {
+      if (!this.form.controls[`url-${i}`].valid) return false;
+    }
+
+    const lastIndex = this.urls.length - 1;
+    // If the last input (which is not the first) is empty, it's ok
+    if (this.urls[lastIndex] === '' && lastIndex !== 0) {
+      return true;
+    } else {
+      return this.form.controls[`url-${lastIndex}`].valid;
+    }
+  }
+
   removeField(index: number) {
+    // Remove the last control
+    this.form.removeControl(`url-${this.urls.length - 1}`);
     this.urls.splice(index, 1);
   }
 
@@ -43,11 +69,6 @@ export class FriendAddComponent {
       return;
     }
 
-    if (!this.isUrlsRegexValid(notEmptyUrls)) {
-      this.error = 'Some url(s) are not valid.';
-      return;
-    }
-
     if (!this.isUrlsUnique(notEmptyUrls)) {
       this.error = 'Urls need to be unique.';
       return;
@@ -58,42 +79,29 @@ export class FriendAddComponent {
 
     this.friendService.makeFriends(notEmptyUrls).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) => {
+    Object.keys(this.form.value).forEach((urlKey) => {
+      const url = this.form.value[urlKey];
       if (url !== '') notEmptyUrls.push(url);
     });
 
     return notEmptyUrls;
   }
 
-  // Temporary
-  // Use HTML validators
-  private isUrlsRegexValid(urls: string[]) {
-    let res = true;
-
-    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;
-      }
-    });
-
-    return res;
-  }
-
   private isUrlsUnique(urls: string[]) {
     return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
   }