Project

General

Profile

1 61381 k.triantaf
import {Component, Input, OnInit} from "@angular/core";
2
import {FormBuilder, FormControl, Validators} from "@angular/forms";
3
import {UserRegistryService} from "../services/user-registry.service";
4
5
@Component({
6
  selector: 'verification',
7
  template: `
8
    <div *ngIf="loading" class="loading-gif"></div>
9
    <div *ngIf="name && !loading" class="uk-text-center uk-text-large">
10
      <ng-container *ngIf="state === 'default'">
11
        <div>
12
          You have been invited to join <span class="uk-text-bold">{{name}}</span> as Manager;<br>
13
          Fill in the verification code, sent to your email, to accept the invitation request.
14
        </div>
15
        <div class="uk-margin-medium-top">
16
          <input [formControl]="code" class="uk-input uk-width-small" [class.uk-form-danger]="code.invalid">
17
        </div>
18
        <div class="uk-margin-medium-top">
19
          <button class="uk-button" [class.portal-button]="code.valid" [class.uk-disabled]="code.invalid"
20
                  (click)="verify()">Accept
21
          </button>
22
          <button class="uk-button uk-button-danger uk-margin-medium-left" (click)="reject()">Reject</button>
23
        </div>
24
      </ng-container>
25
      <ng-container *ngIf="state === 'verified'">
26
        <div>
27
          You are now manager of {{name}}.
28
        </div>
29
      </ng-container>
30
      <ng-container *ngIf="state === 'refused'">
31
        <div>
32
          You have been refused to be manager of {{name}}.
33
        </div>
34
      </ng-container>
35
    </div>
36
  `
37
})
38
export class VerificationComponent implements OnInit {
39
40
  @Input()
41
  public name: string;
42
  @Input()
43
  public invitation;
44
  public code: FormControl;
45
  public loading = false;
46
  public state: 'default' | 'verified' | 'refused' | 'error' = 'default';
47
48
  constructor(private fb: FormBuilder,
49
              private userRegistryService: UserRegistryService) {
50
  }
51
52
  ngOnInit() {
53
    this.code = this.fb.control('', [Validators.required, Validators.pattern('^[+0-9]{6}$')]);
54
  }
55
56
  verify() {
57
    this.loading = true;
58
    this.userRegistryService.verify(this.invitation.id, this.code.value).subscribe(() => {
59
      this.state = 'verified';
60
      this.loading = false;
61
    });
62
  }
63
64
  reject() {
65
    this.loading = true;
66
    this.userRegistryService.deleteVerification(this.invitation.id).subscribe(() => {
67
      this.state = 'refused';
68
      this.loading = false;
69
    });
70
  }
71
}