]>
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({ | |
4115f200 C |
37 | moderationResponse: REGISTRATION_MODERATION_RESPONSE_VALIDATOR, |
38 | preventEmailDelivery: null | |
9589907c C |
39 | }) |
40 | } | |
41 | ||
42 | isAccept () { | |
43 | return this.processMode === 'accept' | |
44 | } | |
45 | ||
46 | isReject () { | |
47 | return this.processMode === 'reject' | |
48 | } | |
49 | ||
50 | openModal (registration: UserRegistration, mode: 'accept' | 'reject') { | |
51 | this.processMode = mode | |
52 | this.registration = registration | |
53 | ||
4115f200 C |
54 | this.form.patchValue({ |
55 | preventEmailDelivery: !this.isEmailEnabled() || registration.emailVerified !== true | |
56 | }) | |
57 | ||
9589907c C |
58 | this.openedModal = this.modalService.open(this.modal, { centered: true }) |
59 | } | |
60 | ||
61 | hide () { | |
62 | this.form.reset() | |
63 | ||
64 | this.openedModal.close() | |
65 | } | |
66 | ||
67 | getSubmitValue () { | |
68 | if (this.isAccept()) { | |
69 | return $localize`Accept registration` | |
70 | } | |
71 | ||
72 | return $localize`Reject registration` | |
73 | } | |
74 | ||
75 | processRegistration () { | |
76 | if (this.isAccept()) return this.acceptRegistration() | |
77 | ||
78 | return this.rejectRegistration() | |
79 | } | |
80 | ||
81 | isEmailEnabled () { | |
82 | return this.server.getHTMLConfig().email.enabled | |
83 | } | |
84 | ||
4115f200 C |
85 | isPreventEmailDeliveryChecked () { |
86 | return this.form.value.preventEmailDelivery | |
87 | } | |
9589907c | 88 | |
4115f200 C |
89 | private acceptRegistration () { |
90 | this.registrationService.acceptRegistration({ | |
91 | registration: this.registration, | |
92 | moderationResponse: this.form.value.moderationResponse, | |
93 | preventEmailDelivery: this.form.value.preventEmailDelivery | |
94 | }).subscribe({ | |
95 | next: () => { | |
96 | this.notifier.success($localize`${this.registration.username} account created`) | |
97 | ||
98 | this.registrationProcessed.emit() | |
99 | this.hide() | |
100 | }, | |
101 | ||
102 | error: err => this.notifier.error(err.message) | |
103 | }) | |
9589907c C |
104 | } |
105 | ||
106 | private rejectRegistration () { | |
4115f200 C |
107 | this.registrationService.rejectRegistration({ |
108 | registration: this.registration, | |
109 | moderationResponse: this.form.value.moderationResponse, | |
110 | preventEmailDelivery: this.form.value.preventEmailDelivery | |
111 | }).subscribe({ | |
112 | next: () => { | |
113 | this.notifier.success($localize`${this.registration.username} registration rejected`) | |
114 | ||
115 | this.registrationProcessed.emit() | |
116 | this.hide() | |
117 | }, | |
118 | ||
119 | error: err => this.notifier.error(err.message) | |
120 | }) | |
9589907c C |
121 | } |
122 | } |