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

import { NotificationsService } from 'angular2-notifications'

import { ConfirmService } from '../../../core'
import { RestDataSource, User, Utils } from '../../../shared'
import { UserService } from '../shared'

@Component({
  selector: 'my-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: [ './user-list.component.scss' ]
})
export class UserListComponent {
  usersSource: RestDataSource = null
  tableSettings = {
    mode: 'external',
    attr: {
      class: 'table-hover'
    },
    hideSubHeader: true,
    actions: {
      position: 'right',
      add: false,
      edit: false,
      delete: true
    },
    delete: {
      deleteButtonContent: Utils.getRowDeleteButton()
    },
    pager: {
      display: true,
      perPage: 1
    },
    columns: {
      id: {
        title: 'ID',
        sortDirection: 'asc'
      },
      username: {
        title: 'Username'
      },
      email: {
        title: 'Email'
      },
      videoQuota: {
        title: 'Video quota'
      },
      role: {
        title: 'Role',
        sort: false
      },
      createdAt: {
        title: 'Created Date',
        valuePrepareFunction: Utils.dateToHuman
      }
    }
  }

  constructor (
    private notificationsService: NotificationsService,
    private confirmService: ConfirmService,
    private userService: UserService
  ) {
    this.usersSource = this.userService.getDataSource()
  }

  removeUser ({ data }) {
    const user: User = data

    if (user.username === 'root') {
      this.notificationsService.error('Error', 'You cannot delete root.')
      return
    }

    this.confirmService.confirm('Do you really want to delete this user?', 'Delete').subscribe(
      res => {
        if (res === false) return

        this.userService.removeUser(user).subscribe(
          () => {
            this.notificationsService.success('Success', `User ${user.username} deleted.`)
            this.usersSource.refresh()
          },

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