]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/user-ban-modal.component.ts
Split user service
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-ban-modal.component.ts
CommitLineData
141b177d 1import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
d92d070c 2import { Notifier } from '@app/core'
7ed1edbb 3import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
141b177d
C
4import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
67ed6552 6import { User } from '@shared/models'
7ed1edbb 7import { USER_BAN_REASON_VALIDATOR } from '../form-validators/user-validators'
d92d070c 8import { UserAdminService } from '../shared-users'
141b177d
C
9
10@Component({
11 selector: 'my-user-ban-modal',
12 templateUrl: './user-ban-modal.component.html',
13 styleUrls: [ './user-ban-modal.component.scss' ]
14})
15export class UserBanModalComponent extends FormReactive implements OnInit {
f36da21e 16 @ViewChild('modal', { static: true }) modal: NgbModal
791645e6 17 @Output() userBanned = new EventEmitter<User | User[]>()
141b177d 18
791645e6 19 private usersToBan: User | User[]
141b177d 20 private openedModal: NgbModalRef
884b1777 21 modalMessage = ''
141b177d
C
22
23 constructor (
24 protected formValidatorService: FormValidatorService,
25 private modalService: NgbModal,
f8b2c1b4 26 private notifier: Notifier,
d92d070c 27 private userAdminService: UserAdminService
141b177d
C
28 ) {
29 super()
30 }
31
32 ngOnInit () {
33 this.buildForm({
7ed1edbb 34 reason: USER_BAN_REASON_VALIDATOR
141b177d
C
35 })
36 }
37
791645e6
C
38 openModal (user: User | User[]) {
39 this.usersToBan = user
24e7916c 40 this.openedModal = this.modalService.open(this.modal, { centered: true })
884b1777
TCT
41
42 const isSingleUser = !(Array.isArray(this.usersToBan) && this.usersToBan.length > 1)
43 this.modalMessage = isSingleUser ? $localize`Ban this user` : $localize`Ban these users`
141b177d
C
44 }
45
457bb213 46 hide () {
791645e6 47 this.usersToBan = undefined
141b177d
C
48 this.openedModal.close()
49 }
50
98ab5dc8 51 banUser () {
141b177d
C
52 const reason = this.form.value['reason'] || undefined
53
d92d070c 54 this.userAdminService.banUsers(this.usersToBan, reason)
1378c0d3
C
55 .subscribe({
56 next: () => {
791645e6 57 const message = Array.isArray(this.usersToBan)
66357162
C
58 ? $localize`${this.usersToBan.length} users banned.`
59 : $localize`User ${this.usersToBan.username} banned.`
141b177d 60
f8b2c1b4 61 this.notifier.success(message)
791645e6
C
62
63 this.userBanned.emit(this.usersToBan)
457bb213 64 this.hide()
141b177d
C
65 },
66
1378c0d3
C
67 error: err => this.notifier.error(err.message)
68 })
141b177d
C
69 }
70
71}