1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
7 import { FormReactive, UserValidatorsService } from '@app/shared/forms'
8 import { UserService } from '@app/shared/users'
9 import { User } from '../../../../../shared'
12 selector: 'my-user-ban-modal',
13 templateUrl: './user-ban-modal.component.html',
14 styleUrls: [ './user-ban-modal.component.scss' ]
16 export class UserBanModalComponent extends FormReactive implements OnInit {
17 @ViewChild('modal', { static: true }) modal: NgbModal
18 @Output() userBanned = new EventEmitter<User | User[]>()
20 private usersToBan: User | User[]
21 private openedModal: NgbModalRef
24 protected formValidatorService: FormValidatorService,
25 private modalService: NgbModal,
26 private notifier: Notifier,
27 private userService: UserService,
28 private userValidatorsService: UserValidatorsService,
36 reason: this.userValidatorsService.USER_BAN_REASON
40 openModal (user: User | User[]) {
41 this.usersToBan = user
42 this.openedModal = this.modalService.open(this.modal, { centered: true })
46 this.usersToBan = undefined
47 this.openedModal.close()
51 const reason = this.form.value['reason'] || undefined
53 this.userService.banUsers(this.usersToBan, reason)
56 const message = Array.isArray(this.usersToBan)
57 ? this.i18n('{{num}} users banned.', { num: this.usersToBan.length })
58 : this.i18n('User {{username}} banned.', { username: this.usersToBan.username })
60 this.notifier.success(message)
62 this.userBanned.emit(this.usersToBan)
66 err => this.notifier.error(err.message)