1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Notifier, UserService } from '@app/core'
3 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6 import { User } from '@shared/models'
7 import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
10 selector: 'my-user-ban-modal',
11 templateUrl: './user-ban-modal.component.html',
12 styleUrls: [ './user-ban-modal.component.scss' ]
14 export class UserBanModalComponent extends FormReactive implements OnInit {
15 @ViewChild('modal', { static: true }) modal: NgbModal
16 @Output() userBanned = new EventEmitter<User | User[]>()
18 private usersToBan: User | User[]
19 private openedModal: NgbModalRef
22 protected formValidatorService: FormValidatorService,
23 private modalService: NgbModal,
24 private notifier: Notifier,
25 private userService: UserService
32 reason: USER_BAN_REASON_VALIDATOR
36 openModal (user: User | User[]) {
37 this.usersToBan = user
38 this.openedModal = this.modalService.open(this.modal, { centered: true })
42 this.usersToBan = undefined
43 this.openedModal.close()
47 const reason = this.form.value['reason'] || undefined
49 this.userService.banUsers(this.usersToBan, reason)
52 const message = Array.isArray(this.usersToBan)
53 ? $localize`${this.usersToBan.length} users banned.`
54 : $localize`User ${this.usersToBan.username} banned.`
56 this.notifier.success(message)
58 this.userBanned.emit(this.usersToBan)
62 err => this.notifier.error(err.message)