aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/friends/friend-add/friend-add.component.ts
blob: 12c46e5cd6fbcae63532c72108ed5c51592d4eb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';

import { NotificationsService } from 'angular2-notifications';

import { ConfirmService } from '../../../core';
import { validateHost } from '../../../shared';
import { FriendService } from '../shared';

@Component({
  selector: 'my-friend-add',
  templateUrl: './friend-add.component.html',
  styleUrls: [ './friend-add.component.scss' ]
})
export class FriendAddComponent implements OnInit {
  form: FormGroup;
  hosts = [ ];
  error: string = null;

  constructor(
    private router: Router,
    private notificationsService: NotificationsService,
    private confirmService: ConfirmService,
    private friendService: FriendService
  ) {}

  ngOnInit() {
    this.form = new FormGroup({});
    this.addField();
  }

  addField() {
    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 {
    return index;
  }

  displayAddField(index: number) {
    return index === (this.hosts.length - 1);
  }

  displayRemoveField(index: number) {
    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) {
    // Remove the last control
    this.form.removeControl(`host-${this.hosts.length - 1}`);
    this.hosts.splice(index, 1);
  }

  makeFriends() {
    this.error = '';

    const notEmptyHosts = this.getNotEmptyHosts();
    if (notEmptyHosts.length === 0) {
      this.error = 'You need to specify at least 1 host.';
      return;
    }

    if (!this.isHostsUnique(notEmptyHosts)) {
      this.error = 'Hosts need to be unique.';
      return;
    }

    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('Sucess', 'Make friends request sent!');
            this.router.navigate([ '/admin/friends/list' ]);
          },

          err => this.notificationsService.error('Error', err.text)
        );
      }
    );
  }

  private getNotEmptyHosts() {
    const notEmptyHosts = [];

    Object.keys(this.form.value).forEach((hostKey) => {
      const host = this.form.value[hostKey];
      if (host !== '') notEmptyHosts.push(host);
    });

    return notEmptyHosts;
  }

  private isHostsUnique(hosts: string[]) {
    return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host));
  }
}