2017-09-14 11:57:49 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { SortMeta } from 'primeng/primeng'
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
2017-01-27 16:14:11 +01:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
import { ConfirmService } from '../../../core'
|
2017-09-14 11:57:49 +02:00
|
|
|
import { RestTable, RestPagination, User } from '../../../shared'
|
2017-06-16 14:32:15 +02:00
|
|
|
import { UserService } from '../shared'
|
2016-08-09 21:45:21 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-user-list',
|
2016-09-19 22:49:31 +02:00
|
|
|
templateUrl: './user-list.component.html',
|
|
|
|
styleUrls: [ './user-list.component.scss' ]
|
2016-08-09 21:45:21 +02:00
|
|
|
})
|
2017-09-14 11:57:49 +02:00
|
|
|
export class UserListComponent extends RestTable implements OnInit {
|
|
|
|
users: User[] = []
|
|
|
|
totalRecords = 0
|
|
|
|
rowsPerPage = 10
|
|
|
|
sort: SortMeta = { field: 'id', order: 1 }
|
|
|
|
pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
constructor (
|
2017-01-27 16:14:11 +01:00
|
|
|
private notificationsService: NotificationsService,
|
2017-01-27 16:54:44 +01:00
|
|
|
private confirmService: ConfirmService,
|
2017-01-27 16:14:11 +01:00
|
|
|
private userService: UserService
|
2017-01-30 22:41:14 +01:00
|
|
|
) {
|
2017-09-14 11:57:49 +02:00
|
|
|
super()
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 11:57:49 +02:00
|
|
|
ngOnInit () {
|
|
|
|
this.loadData()
|
2017-09-05 21:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 11:57:49 +02:00
|
|
|
removeUser (user: User) {
|
2017-01-30 22:41:14 +01:00
|
|
|
if (user.username === 'root') {
|
2017-06-16 14:32:15 +02:00
|
|
|
this.notificationsService.error('Error', 'You cannot delete root.')
|
|
|
|
return
|
2017-01-30 22:41:14 +01:00
|
|
|
}
|
2016-08-09 21:45:21 +02:00
|
|
|
|
2017-01-27 16:54:44 +01:00
|
|
|
this.confirmService.confirm('Do you really want to delete this user?', 'Delete').subscribe(
|
|
|
|
res => {
|
2017-06-16 14:32:15 +02:00
|
|
|
if (res === false) return
|
2017-01-27 16:54:44 +01:00
|
|
|
|
|
|
|
this.userService.removeUser(user).subscribe(
|
|
|
|
() => {
|
2017-06-16 14:32:15 +02:00
|
|
|
this.notificationsService.success('Success', `User ${user.username} deleted.`)
|
2017-09-14 11:57:49 +02:00
|
|
|
this.loadData()
|
2017-01-27 16:54:44 +01:00
|
|
|
},
|
|
|
|
|
2017-09-14 17:06:31 +02:00
|
|
|
err => this.notificationsService.error('Error', err.message)
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2017-01-27 16:54:44 +01:00
|
|
|
}
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|
2017-09-14 11:57:49 +02:00
|
|
|
|
|
|
|
getRouterUserEditLink (user: User) {
|
|
|
|
return [ '/admin', 'users', user.id, 'update' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
protected loadData () {
|
|
|
|
this.userService.getUsers(this.pagination, this.sort)
|
|
|
|
.subscribe(
|
|
|
|
resultList => {
|
|
|
|
this.users = resultList.data
|
|
|
|
this.totalRecords = resultList.total
|
|
|
|
},
|
|
|
|
|
2017-09-14 17:06:31 +02:00
|
|
|
err => this.notificationsService.error('Error', err.message)
|
2017-09-14 11:57:49 +02:00
|
|
|
)
|
|
|
|
}
|
2016-08-09 21:45:21 +02:00
|
|
|
}
|