aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/admin/users/user-add/user-add.component.ts
blob: b7efd3a8004c68a794c7d538a5f2ba3412747af8 (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
import { Validators } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { Router } from '@angular/router';

import { UserService } from '../shared';

@Component({
  selector: 'my-user-add',
  template: require('./user-add.component.html'),
  directives: [ REACTIVE_FORM_DIRECTIVES ]
})
export class UserAddComponent implements OnInit {
  userAddForm: FormGroup;
  error: string = null;
  username = '';
  password = '';

  constructor(private router: Router, private userService: UserService) {}

  ngOnInit() {
    this.userAddForm = new FormGroup({
      username: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(20) ]),
      password: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]),
    });
  }

  addUser() {
    this.error = null;

    this.userService.addUser(this.username, this.password).subscribe(
      ok => this.router.navigate([ '/admin/users/list' ]),

      err => this.error = err
    );
  }
}