2018-08-09 11:51:25 -04:00
|
|
|
import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
|
2018-12-19 10:04:34 -05:00
|
|
|
import { Notifier } from '@app/core'
|
2018-08-09 11:51:25 -04:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|
|
|
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
|
|
|
|
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
|
2018-10-05 09:24:29 -04:00
|
|
|
import { FormReactive, UserValidatorsService } from '@app/shared/forms'
|
2018-10-05 10:56:14 -04:00
|
|
|
import { UserService } from '@app/shared/users'
|
|
|
|
import { User } from '../../../../../shared'
|
2018-08-09 11:51:25 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-user-ban-modal',
|
|
|
|
templateUrl: './user-ban-modal.component.html',
|
|
|
|
styleUrls: [ './user-ban-modal.component.scss' ]
|
|
|
|
})
|
|
|
|
export class UserBanModalComponent extends FormReactive implements OnInit {
|
2019-07-24 10:05:59 -04:00
|
|
|
@ViewChild('modal', { static: true }) modal: NgbModal
|
2018-10-08 09:15:11 -04:00
|
|
|
@Output() userBanned = new EventEmitter<User | User[]>()
|
2018-08-09 11:51:25 -04:00
|
|
|
|
2018-10-08 09:15:11 -04:00
|
|
|
private usersToBan: User | User[]
|
2018-08-09 11:51:25 -04:00
|
|
|
private openedModal: NgbModalRef
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
protected formValidatorService: FormValidatorService,
|
|
|
|
private modalService: NgbModal,
|
2018-12-19 10:04:34 -05:00
|
|
|
private notifier: Notifier,
|
2018-08-09 11:51:25 -04:00
|
|
|
private userService: UserService,
|
|
|
|
private userValidatorsService: UserValidatorsService,
|
|
|
|
private i18n: I18n
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm({
|
|
|
|
reason: this.userValidatorsService.USER_BAN_REASON
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-08 09:15:11 -04:00
|
|
|
openModal (user: User | User[]) {
|
|
|
|
this.usersToBan = user
|
2020-02-05 14:54:37 -05:00
|
|
|
this.openedModal = this.modalService.open(this.modal, { centered: true })
|
2018-08-09 11:51:25 -04:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:05:40 -05:00
|
|
|
hide () {
|
2018-10-08 09:15:11 -04:00
|
|
|
this.usersToBan = undefined
|
2018-08-09 11:51:25 -04:00
|
|
|
this.openedModal.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
async banUser () {
|
|
|
|
const reason = this.form.value['reason'] || undefined
|
|
|
|
|
2018-10-08 09:15:11 -04:00
|
|
|
this.userService.banUsers(this.usersToBan, reason)
|
2018-08-09 11:51:25 -04:00
|
|
|
.subscribe(
|
|
|
|
() => {
|
2018-10-08 09:15:11 -04:00
|
|
|
const message = Array.isArray(this.usersToBan)
|
|
|
|
? this.i18n('{{num}} users banned.', { num: this.usersToBan.length })
|
|
|
|
: this.i18n('User {{username}} banned.', { username: this.usersToBan.username })
|
2018-08-09 11:51:25 -04:00
|
|
|
|
2018-12-19 10:04:34 -05:00
|
|
|
this.notifier.success(message)
|
2018-10-08 09:15:11 -04:00
|
|
|
|
|
|
|
this.userBanned.emit(this.usersToBan)
|
2019-01-16 10:05:40 -05:00
|
|
|
this.hide()
|
2018-08-09 11:51:25 -04:00
|
|
|
},
|
|
|
|
|
2018-12-19 10:04:34 -05:00
|
|
|
err => this.notifier.error(err.message)
|
2018-08-09 11:51:25 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|