]>
Commit | Line | Data |
---|---|---|
9589907c C |
1 | import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
2 | import { Notifier, ServerService } from '@app/core' | |
3 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' | |
4 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
6 | import { UserRegistration } from '@shared/models' | |
7 | import { AdminRegistrationService } from './admin-registration.service' | |
8 | import { REGISTRATION_MODERATION_RESPONSE_VALIDATOR } from './process-registration-validators' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-process-registration-modal', | |
12 | templateUrl: './process-registration-modal.component.html', | |
13 | styleUrls: [ './process-registration-modal.component.scss' ] | |
14 | }) | |
15 | export class ProcessRegistrationModalComponent extends FormReactive implements OnInit { | |
16 | @ViewChild('modal', { static: true }) modal: NgbModal | |
17 | ||
18 | @Output() registrationProcessed = new EventEmitter() | |
19 | ||
20 | registration: UserRegistration | |
21 | ||
22 | private openedModal: NgbModalRef | |
23 | private processMode: 'accept' | 'reject' | |
24 | ||
25 | constructor ( | |
26 | protected formReactiveService: FormReactiveService, | |
27 | private server: ServerService, | |
28 | private modalService: NgbModal, | |
29 | private notifier: Notifier, | |
30 | private registrationService: AdminRegistrationService | |
31 | ) { | |
32 | super() | |
33 | } | |
34 | ||
35 | ngOnInit () { | |
36 | this.buildForm({ | |
37 | moderationResponse: REGISTRATION_MODERATION_RESPONSE_VALIDATOR | |
38 | }) | |
39 | } | |
40 | ||
41 | isAccept () { | |
42 | return this.processMode === 'accept' | |
43 | } | |
44 | ||
45 | isReject () { | |
46 | return this.processMode === 'reject' | |
47 | } | |
48 | ||
49 | openModal (registration: UserRegistration, mode: 'accept' | 'reject') { | |
50 | this.processMode = mode | |
51 | this.registration = registration | |
52 | ||
53 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
54 | } | |
55 | ||
56 | hide () { | |
57 | this.form.reset() | |
58 | ||
59 | this.openedModal.close() | |
60 | } | |
61 | ||
62 | getSubmitValue () { | |
63 | if (this.isAccept()) { | |
64 | return $localize`Accept registration` | |
65 | } | |
66 | ||
67 | return $localize`Reject registration` | |
68 | } | |
69 | ||
70 | processRegistration () { | |
71 | if (this.isAccept()) return this.acceptRegistration() | |
72 | ||
73 | return this.rejectRegistration() | |
74 | } | |
75 | ||
76 | isEmailEnabled () { | |
77 | return this.server.getHTMLConfig().email.enabled | |
78 | } | |
79 | ||
80 | private acceptRegistration () { | |
81 | this.registrationService.acceptRegistration(this.registration, this.form.value.moderationResponse) | |
82 | .subscribe({ | |
83 | next: () => { | |
84 | this.notifier.success($localize`${this.registration.username} account created`) | |
85 | ||
86 | this.registrationProcessed.emit() | |
87 | this.hide() | |
88 | }, | |
89 | ||
90 | error: err => this.notifier.error(err.message) | |
91 | }) | |
92 | } | |
93 | ||
94 | private rejectRegistration () { | |
95 | this.registrationService.rejectRegistration(this.registration, this.form.value.moderationResponse) | |
96 | .subscribe({ | |
97 | next: () => { | |
98 | this.notifier.success($localize`${this.registration.username} registration rejected`) | |
99 | ||
100 | this.registrationProcessed.emit() | |
101 | this.hide() | |
102 | }, | |
103 | ||
104 | error: err => this.notifier.error(err.message) | |
105 | }) | |
106 | } | |
107 | } |