forked from CenterForOpenScience/angular-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprint-make-decision.component.ts
More file actions
332 lines (271 loc) · 11.4 KB
/
preprint-make-decision.component.ts
File metadata and controls
332 lines (271 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { createDispatchMap, select } from '@ngxs/store';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { Button } from 'primeng/button';
import { Dialog } from 'primeng/dialog';
import { Message } from 'primeng/message';
import { RadioButton } from 'primeng/radiobutton';
import { Textarea } from 'primeng/textarea';
import { Tooltip } from 'primeng/tooltip';
import { finalize } from 'rxjs';
import { TitleCasePipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, computed, effect, inject, input, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { ReviewAction } from '@osf/features/moderation/models';
import { decisionExplanation, decisionSettings, formInputLimits } from '@osf/features/preprints/constants';
import { ProviderReviewsWorkflow, ReviewsState } from '@osf/features/preprints/enums';
import { PreprintProviderDetails, PreprintRequest } from '@osf/features/preprints/models';
import {
PreprintSelectors,
SubmitRequestsDecision,
SubmitReviewsDecision,
} from '@osf/features/preprints/store/preprint';
import { InputLimits } from '@osf/shared/constants/input-limits.const';
import { StringOrNull } from '@osf/shared/helpers/types.helper';
@Component({
selector: 'osf-preprint-make-decision',
imports: [Button, Dialog, Message, RadioButton, Textarea, Tooltip, FormsModule, TranslatePipe, TitleCasePipe],
templateUrl: './preprint-make-decision.component.html',
styleUrl: './preprint-make-decision.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PreprintMakeDecisionComponent {
private readonly translateService = inject(TranslateService);
private readonly router = inject(Router);
private readonly actions = createDispatchMap({
submitReviewsDecision: SubmitReviewsDecision,
submitRequestsDecision: SubmitRequestsDecision,
});
readonly provider = input.required<PreprintProviderDetails>();
readonly latestAction = input.required<ReviewAction | null>();
readonly latestWithdrawalRequest = input.required<PreprintRequest | null>();
readonly isPendingWithdrawal = input.required<boolean>();
readonly preprint = select(PreprintSelectors.getPreprint);
dialogVisible = false;
didValidate = signal<boolean>(false);
decision = signal<ReviewsState>(ReviewsState.Accepted);
initialReviewerComment = signal<StringOrNull>(null);
reviewerComment = signal<StringOrNull>(null);
requestDecisionJustification = signal<StringOrNull>(null);
saving = signal<boolean>(false);
readonly decisionCommentLimit = InputLimits.decisionComment.maxLength;
readonly ReviewsState = ReviewsState;
labelDecisionButton = computed(() => {
const preprint = this.preprint();
if (preprint?.reviewsState === ReviewsState.Withdrawn) {
return 'preprints.details.decision.withdrawalReason';
} else if (this.isPendingWithdrawal()) {
return 'preprints.details.decision.makeDecision';
} else {
return preprint?.reviewsState === ReviewsState.Pending
? 'preprints.details.decision.makeDecision'
: 'preprints.details.decision.modifyDecision';
}
});
makeDecisionButtonDisabled = computed(() => {
const reason = this.latestAction()?.comment;
const state = this.preprint()?.reviewsState;
return state === ReviewsState.Withdrawn && !reason;
});
labelDecisionDialogHeader = computed(() => {
const preprint = this.preprint();
if (preprint?.reviewsState === ReviewsState.Withdrawn) {
return 'preprints.details.decision.header.withdrawalReason';
} else if (this.isPendingWithdrawal()) {
return 'preprints.details.decision.header.submitDecision';
} else {
return preprint?.reviewsState === ReviewsState.Pending
? 'preprints.details.decision.header.submitDecision'
: 'preprints.details.decision.header.modifyDecision';
}
});
labelSubmitButton = computed(() => {
const preprint = this.preprint();
const reviewsState = preprint?.reviewsState;
if (this.isPendingWithdrawal() || reviewsState === ReviewsState.Pending) {
return 'preprints.details.decision.submitButton.submitDecision';
}
if (this.commentEdited() && !this.decisionChanged()) {
return 'preprints.details.decision.submitButton.updateComment';
}
return 'preprints.details.decision.submitButton.modifyDecision';
});
submitButtonDisabled = computed(
() => (!this.decisionChanged() && !this.commentEdited()) || this.commentExceedsLimit()
);
acceptOptionExplanation = computed(() => {
const reviewsWorkflow = this.provider().reviewsWorkflow;
if (reviewsWorkflow === ProviderReviewsWorkflow.PostModeration) {
return 'preprints.details.decision.accept.post';
}
return 'preprints.details.decision.accept.pre';
});
rejectOptionLabel = computed(() =>
this.preprint()?.isPublished
? 'preprints.details.decision.withdrawn.label'
: 'preprints.details.decision.reject.label'
);
labelRequestDecisionJustification = computed(() => {
if (this.decision() === ReviewsState.Rejected) {
return 'preprints.details.decision.denialJustification';
}
return 'preprints.details.decision.withdrawalJustification';
});
rejectOptionExplanation = computed(() => {
const provider = this.provider();
const reviewsWorkflow = provider.reviewsWorkflow;
const isPreModeration = reviewsWorkflow === ProviderReviewsWorkflow.PreModeration;
if (isPreModeration && this.preprint()?.reviewsState === ReviewsState.Accepted) {
return 'preprints.details.decision.approve.explanation';
}
if (isPreModeration) {
return decisionExplanation.reject[reviewsWorkflow];
}
return decisionExplanation.withdrawn[ProviderReviewsWorkflow.PostModeration];
});
rejectRadioButtonValue = computed(() =>
this.preprint()?.isPublished ? ReviewsState.Withdrawn : ReviewsState.Rejected
);
settingsComments = computed(() => {
const commentType = this.provider().reviewsCommentsPrivate ? 'private' : 'public';
return decisionSettings.comments[commentType];
});
settingsNames = computed(() => {
const commentType = this.provider().reviewsCommentsAnonymous ? 'anonymous' : 'named';
return decisionSettings.names[commentType];
});
settingsModeration = computed(
() => decisionSettings.moderation[this.provider().reviewsWorkflow || ProviderReviewsWorkflow.PreModeration]
);
commentEdited = computed(() => this.reviewerComment()?.trim() !== this.initialReviewerComment()?.trim());
commentExceedsLimit = computed(() => {
const comment = this.reviewerComment();
if (!comment) return false;
return comment.length > this.decisionCommentLimit;
});
requestDecisionJustificationErrorMessage = computed(() => {
const justification = this.requestDecisionJustification();
const minLength = formInputLimits.requestDecisionJustification.minLength;
const trimmedJustification = justification?.trim() ?? '';
if (!trimmedJustification) {
return this.translateService.instant('preprints.details.decision.justificationRequiredError');
}
if (trimmedJustification.length < minLength) {
return this.translateService.instant('preprints.details.decision.justificationLengthError', { minLength });
}
return null;
});
decisionChanged = computed(() => this.preprint()?.reviewsState !== this.decision());
constructor() {
effect(() => {
const preprint = this.preprint();
if (!preprint) {
return;
}
const latestAction = this.latestAction();
if (preprint.reviewsState === ReviewsState.Pending) {
this.decision.set(ReviewsState.Accepted);
this.initialReviewerComment.set(null);
this.reviewerComment.set(null);
} else {
this.decision.set(preprint.reviewsState);
this.initialReviewerComment.set(latestAction?.comment ?? null);
this.reviewerComment.set(latestAction?.comment ?? null);
}
});
effect(() => {
const withdrawalRequest = this.latestWithdrawalRequest();
if (!withdrawalRequest) return;
this.requestDecisionJustification.set(withdrawalRequest.comment);
});
}
submit() {
// Don't remove comments
const preprint = this.preprint();
if (!preprint) return;
let trigger = '';
if (preprint.reviewsState !== ReviewsState.Pending && this.commentEdited() && !this.decisionChanged()) {
// If the submission is not pending,
// the decision has not changed and the comment is edited.
// the trigger would be 'edit_comment'
trigger = 'edit_comment';
} else {
let actionType = '';
if (preprint.isPublished && this.isPendingWithdrawal()) {
// if the submission is published and is pending withdrawal.
// actionType would be 'reject'
// meaning moderators could accept/reject the withdrawl request
actionType = 'reject';
} else if (preprint.isPublished && !this.isPendingWithdrawal()) {
// if the submission is published and is not pending withdrawal
// actionType would be 'withdraw'
// meaning moderators could approve/directly withdraw the submission
actionType = 'withdraw';
} else {
// Otherwise
// actionType would be 'reject'
// meaning the moderator could either accept or reject the submission
actionType = 'reject';
}
// If the decision is to accept the submission or the withdrawal request,
// the trigger is 'accept'
// If not, then the trigger is whatever 'actionType' set above.
trigger = this.decision() === ReviewsState.Accepted ? 'accept' : actionType;
}
let comment: StringOrNull = '';
if (this.isPendingWithdrawal()) {
if (trigger === 'reject') {
this.didValidate.set(true);
if (this.requestDecisionJustificationErrorMessage() !== null) {
return;
}
}
comment = this.requestDecisionJustification()?.trim() || null;
} else {
comment = this.reviewerComment()?.trim() || null;
}
this.saving.set(true);
if (this.isPendingWithdrawal()) {
const latestWithdrawalRequest = this.latestWithdrawalRequest();
if (!latestWithdrawalRequest) {
this.saving.set(false);
return;
}
this.actions
.submitRequestsDecision(latestWithdrawalRequest.id, trigger, comment)
.pipe(finalize(() => this.saving.set(false)))
.subscribe({
next: () => {
this.router.navigate(['preprints', this.provider().id, 'moderation', 'withdrawals']);
},
});
} else {
this.actions
.submitReviewsDecision(trigger, comment)
.pipe(finalize(() => this.saving.set(false)))
.subscribe({
next: () => {
this.router.navigate(['preprints', this.provider().id, 'moderation', 'submissions']);
},
});
}
}
requestDecisionToggled() {
if (!this.isPendingWithdrawal()) {
return;
}
if (this.decision() === ReviewsState.Accepted) {
this.requestDecisionJustification.set(this.latestWithdrawalRequest()?.comment || null);
} else if (this.decision() === ReviewsState.Rejected) {
this.requestDecisionJustification.set(null);
}
}
cancel() {
this.dialogVisible = false;
const preprint = this.preprint();
if (preprint) {
this.decision.set(preprint.reviewsState);
}
this.reviewerComment.set(this.initialReviewerComment());
}
}