]>
Commit | Line | Data |
---|---|---|
141b177d | 1 | import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
67ed6552 C |
2 | import { Notifier, UserService } from '@app/core' |
3 | import { FormReactive, FormValidatorService, UserValidatorsService } 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 C |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
7 | import { User } from '@shared/models' | |
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, |
141b177d C |
25 | private userService: UserService, |
26 | private userValidatorsService: UserValidatorsService, | |
27 | private i18n: I18n | |
28 | ) { | |
29 | super() | |
30 | } | |
31 | ||
32 | ngOnInit () { | |
33 | this.buildForm({ | |
34 | reason: this.userValidatorsService.USER_BAN_REASON | |
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 }) |
141b177d C |
41 | } |
42 | ||
457bb213 | 43 | hide () { |
791645e6 | 44 | this.usersToBan = undefined |
141b177d C |
45 | this.openedModal.close() |
46 | } | |
47 | ||
48 | async banUser () { | |
49 | const reason = this.form.value['reason'] || undefined | |
50 | ||
791645e6 | 51 | this.userService.banUsers(this.usersToBan, reason) |
141b177d C |
52 | .subscribe( |
53 | () => { | |
791645e6 C |
54 | const message = Array.isArray(this.usersToBan) |
55 | ? this.i18n('{{num}} users banned.', { num: this.usersToBan.length }) | |
56 | : this.i18n('User {{username}} banned.', { username: this.usersToBan.username }) | |
141b177d | 57 | |
f8b2c1b4 | 58 | this.notifier.success(message) |
791645e6 C |
59 | |
60 | this.userBanned.emit(this.usersToBan) | |
457bb213 | 61 | this.hide() |
141b177d C |
62 | }, |
63 | ||
f8b2c1b4 | 64 | err => this.notifier.error(err.message) |
141b177d C |
65 | ) |
66 | } | |
67 | ||
68 | } |