Add ability to delete and update abuse on client
[oweals/peertube.git] / client / src / app / +admin / users / user-list / user-ban-modal.component.ts
1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { FormReactive, User, UserValidatorsService } from '../../../shared'
4 import { UserService } from '../shared'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
7 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9
10 @Component({
11   selector: 'my-user-ban-modal',
12   templateUrl: './user-ban-modal.component.html',
13   styleUrls: [ './user-ban-modal.component.scss' ]
14 })
15 export 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 }