]>
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', | |
5995a28f | 12 | templateUrl: './process-registration-modal.component.html' |
9589907c C |
13 | }) |
14 | export class ProcessRegistrationModalComponent extends FormReactive implements OnInit { | |
15 | @ViewChild('modal', { static: true }) modal: NgbModal | |
16 | ||
17 | @Output() registrationProcessed = new EventEmitter() | |
18 | ||
19 | registration: UserRegistration | |
20 | ||
21 | private openedModal: NgbModalRef | |
22 | private processMode: 'accept' | 'reject' | |
23 | ||
24 | constructor ( | |
25 | protected formReactiveService: FormReactiveService, | |
26 | private server: ServerService, | |
27 | private modalService: NgbModal, | |
28 | private notifier: Notifier, | |
29 | private registrationService: AdminRegistrationService | |
30 | ) { | |
31 | super() | |
32 | } | |
33 | ||
34 | ngOnInit () { | |
35 | this.buildForm({ | |
4115f200 C |
36 | moderationResponse: REGISTRATION_MODERATION_RESPONSE_VALIDATOR, |
37 | preventEmailDelivery: null | |
9589907c C |
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 | ||
4115f200 C |
53 | this.form.patchValue({ |
54 | preventEmailDelivery: !this.isEmailEnabled() || registration.emailVerified !== true | |
55 | }) | |
56 | ||
9589907c C |
57 | this.openedModal = this.modalService.open(this.modal, { centered: true }) |
58 | } | |
59 | ||
60 | hide () { | |
61 | this.form.reset() | |
62 | ||
63 | this.openedModal.close() | |
64 | } | |
65 | ||
66 | getSubmitValue () { | |
67 | if (this.isAccept()) { | |
68 | return $localize`Accept registration` | |
69 | } | |
70 | ||
71 | return $localize`Reject registration` | |
72 | } | |
73 | ||
74 | processRegistration () { | |
75 | if (this.isAccept()) return this.acceptRegistration() | |
76 | ||
77 | return this.rejectRegistration() | |
78 | } | |
79 | ||
80 | isEmailEnabled () { | |
81 | return this.server.getHTMLConfig().email.enabled | |
82 | } | |
83 | ||
4115f200 C |
84 | isPreventEmailDeliveryChecked () { |
85 | return this.form.value.preventEmailDelivery | |
86 | } | |
9589907c | 87 | |
4115f200 C |
88 | private acceptRegistration () { |
89 | this.registrationService.acceptRegistration({ | |
90 | registration: this.registration, | |
91 | moderationResponse: this.form.value.moderationResponse, | |
92 | preventEmailDelivery: this.form.value.preventEmailDelivery | |
93 | }).subscribe({ | |
94 | next: () => { | |
95 | this.notifier.success($localize`${this.registration.username} account created`) | |
96 | ||
97 | this.registrationProcessed.emit() | |
98 | this.hide() | |
99 | }, | |
100 | ||
101 | error: err => this.notifier.error(err.message) | |
102 | }) | |
9589907c C |
103 | } |
104 | ||
105 | private rejectRegistration () { | |
4115f200 C |
106 | this.registrationService.rejectRegistration({ |
107 | registration: this.registration, | |
108 | moderationResponse: this.form.value.moderationResponse, | |
109 | preventEmailDelivery: this.form.value.preventEmailDelivery | |
110 | }).subscribe({ | |
111 | next: () => { | |
112 | this.notifier.success($localize`${this.registration.username} registration rejected`) | |
113 | ||
114 | this.registrationProcessed.emit() | |
115 | this.hide() | |
116 | }, | |
117 | ||
118 | error: err => this.notifier.error(err.message) | |
119 | }) | |
9589907c C |
120 | } |
121 | } |