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

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

@Component({
  selector: 'my-user-add',
  template: require('./user-add.component.html'),
})
export class UserAddComponent implements OnInit {
  userAddForm: ControlGroup;
  error: string = null;

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

  ngOnInit() {
    this.userAddForm = new ControlGroup({
      username: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(20) ])),
      password: new Control('', Validators.compose([ Validators.required, Validators.minLength(6) ])),
    });
  }

  addUser(username: string, password: string) {
    this.error = null;

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

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