aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/users/user-list/user-list.component.ts
blob: ca08ed3050ea41752b54111136ff584993d0441e (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
import { Component, OnInit } from '@angular/core';

import { NotificationsService } from 'angular2-notifications';

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

@Component({
  selector: 'my-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: [ './user-list.component.scss' ]
})
export class UserListComponent implements OnInit {
  totalUsers: number;
  users: User[];

  constructor(
    private notificationsService: NotificationsService,
    private userService: UserService
  ) {}

  ngOnInit() {
    this.getUsers();
  }

  getUsers() {
    this.userService.getUsers().subscribe(
      ({ users, totalUsers }) => {
        this.users = users;
        this.totalUsers = totalUsers;
      },

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


  removeUser(user: User) {
    if (confirm('Are you sure?')) {
      this.userService.removeUser(user).subscribe(
        () => {
          this.notificationsService.success('Success', `User ${user.username} deleted.`);
          this.getUsers();
        },

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