]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/moderation/user-ban-modal.component.ts
Move user moderation tool in a separate component
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-ban-modal.component.ts
CommitLineData
141b177d
C
1import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2import { NotificationsService } from 'angular2-notifications'
141b177d
C
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e724fa93
C
7import { FormReactive, UserValidatorsService } from '@app/shared/forms'
8import { User, UserService } from '@app/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 {
16 @ViewChild('modal') modal: NgbModal
17 @Output() userBanned = new EventEmitter<User>()
18
19 private userToBan: User
20 private openedModal: NgbModalRef
21
22 constructor (
23 protected formValidatorService: FormValidatorService,
24 private modalService: NgbModal,
25 private notificationsService: NotificationsService,
26 private userService: UserService,
27 private userValidatorsService: UserValidatorsService,
28 private i18n: I18n
29 ) {
30 super()
31 }
32
33 ngOnInit () {
34 this.buildForm({
35 reason: this.userValidatorsService.USER_BAN_REASON
36 })
37 }
38
39 openModal (user: User) {
40 this.userToBan = user
41 this.openedModal = this.modalService.open(this.modal)
42 }
43
44 hideBanUserModal () {
45 this.userToBan = undefined
46 this.openedModal.close()
47 }
48
49 async banUser () {
50 const reason = this.form.value['reason'] || undefined
51
52 this.userService.banUser(this.userToBan, reason)
53 .subscribe(
54 () => {
55 this.notificationsService.success(
56 this.i18n('Success'),
57 this.i18n('User {{username}} banned.', { username: this.userToBan.username })
58 )
59
60 this.userBanned.emit(this.userToBan)
61 this.hideBanUserModal()
62 },
63
64 err => this.notificationsService.error(this.i18n('Error'), err.message)
65 )
66 }
67
68}