]> 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 16cfd8a3ab959f64ed247a2f78609d228c40e9d9..64165a9a5b312547f2771a639a67ffc3407f4662 100644 (file)
@@ -1,5 +1,5 @@
 import { Component, OnInit } from '@angular/core';
-import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
+import { FormControl, FormGroup } from '@angular/forms';
 import { Router } from '@angular/router';
 
 import { validateUrl } from '../../../shared';
@@ -7,24 +7,23 @@ import { FriendService } from '../shared';
 
 @Component({
   selector: 'my-friend-add',
-  template: require('./friend-add.component.html'),
-  styles: [ require('./friend-add.component.scss') ],
-  directives: [ REACTIVE_FORM_DIRECTIVES ]
+  templateUrl: './friend-add.component.html',
+  styleUrls: [ './friend-add.component.scss' ]
 })
 export class FriendAddComponent implements OnInit {
-  friendAddForm: FormGroup;
+  form: FormGroup;
   urls = [ ];
   error: string = null;
 
   constructor(private router: Router, private friendService: FriendService) {}
 
   ngOnInit() {
-    this.friendAddForm = new FormGroup({});
+    this.form = new FormGroup({});
     this.addField();
   }
 
   addField() {
-    this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
+    this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
     this.urls.push('');
   }
 
@@ -43,7 +42,7 @@ export class FriendAddComponent implements OnInit {
   isFormValid() {
     // Do not check the last input
     for (let i = 0; i < this.urls.length - 1; i++) {
-      if (!this.friendAddForm.controls[`url-${i}`].valid) return false;
+      if (!this.form.controls[`url-${i}`].valid) return false;
     }
 
     const lastIndex = this.urls.length - 1;
@@ -51,11 +50,13 @@ export class FriendAddComponent implements OnInit {
     if (this.urls[lastIndex] === '' && lastIndex !== 0) {
       return true;
     } else {
-      return this.friendAddForm.controls[`url-${lastIndex}`].valid;
+      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);
   }
 
@@ -78,21 +79,23 @@ export class FriendAddComponent implements OnInit {
 
     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);
     });