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