-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
cbReviewModal.tsx
553 lines (504 loc) · 12.8 KB
/
cbReviewModal.tsx
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* eslint-disable react/no-unused-state */
/*
* Copyright (C) 2022 Ansh Goyal
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import * as bootstrap from 'react-bootstrap';
// eslint-disable-next-line import/no-internal-modules
import * as eng from '@cospired/i18n-iso-languages/langs/en.json';
import * as iso from '@cospired/i18n-iso-languages';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
// eslint-disable-next-line import/named
import {IconProp} from '@fortawesome/fontawesome-svg-core';
import {Rating} from 'react-simple-star-rating';
import React from 'react';
import ReactTooltip from 'react-tooltip';
import {countWords} from '../../../helpers/utils';
import {faInfoCircle} from '@fortawesome/free-solid-svg-icons';
import request from 'superagent';
const {Alert, Modal} = bootstrap;
iso.registerLocale(eng);
export interface CBReviewModalProps {
entityBBID: string;
entityName: string;
entityType: string;
userId: number;
showModal: boolean;
handleModalToggle: () => void;
handleUpdateReviews: () => void;
}
export interface CBReviewModalState {
acceptLicense: boolean;
alert: Record<string, any>;
language: string;
rating: number;
reviewValidateAlert: string | null;
success: boolean;
textContent: string;
reviewID?: string;
}
class CBReviewModal extends React.Component<
CBReviewModalProps,
CBReviewModalState
> {
constructor(props: CBReviewModalProps) {
super(props);
this.state = {
acceptLicense: false,
alert: {
message: '',
title: '',
type: ''
},
language: 'en',
rating: 0,
reviewID: '',
reviewValidateAlert: null,
success: false,
textContent: ''
};
}
// eslint-disable-next-line react/sort-comp
readonly minTextLength = 25;
readonly maxTextLength = 100000;
private CBBaseUrl = 'https://critiquebrainz.org';
private MBBaseUrl = 'https://metabrainz.org';
// gets all iso-639-1 languages and codes for dropdown
private allLanguagesKeyValue = Object.entries(iso.getNames('en'));
private CBInfoButton = (
<span>
<span
className="CBInfoButton"
data-event="click focus"
data-tip={`CritiqueBrainz is a <a href='${this.MBBaseUrl}/projects'>
MetaBrainz project</a> aimed at providing an open platform for music critics
and hosting Creative Commons licensed music reviews. </br></br>
Your reviews will be independently visible on CritiqueBrainz and appear publicly
on your CritiqueBrainz profile. To view or delete your reviews, visit your
<a href='${this.CBBaseUrl}'>CritiqueBrainz</a> profile.`}
>
<FontAwesomeIcon
icon={faInfoCircle as IconProp}
style={{color: 'black'}}
/>
</span>
<ReactTooltip
clickable
html
className="cb-data-tip"
globalEventOff="click"
place="bottom"
/>
</span>
);
handleError = (error: string | Error, title?: string): void => {
if (!error) {
return;
}
this.setState({
alert: {
message: typeof error === 'object' ? error.message : error,
title: title || 'Error',
type: 'danger'
}
});
};
getAccessToken = async () => {
try {
const response = await request
.post('/external-service/critiquebrainz/refresh');
if (response?.status === 200 && response?.body?.accessToken) {
return response.body.accessToken;
}
return null;
}
catch (error) {
this.handleError(error, 'We could not submit your review');
}
return null;
};
handleInputChange = (
event: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
) => {
const {target} = event;
const value =
target.type === 'checkbox' ?
(target as HTMLInputElement).checked :
target.value;
const {name} = target;
this.setState({
[name]: value
} as any);
};
handleTextInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const {reviewValidateAlert} = this.state;
event.preventDefault();
// remove excessive line breaks to match formatting to CritiqueBrainz
const input = event.target.value.replace(/\n\s*\n\s*\n/g, '\n');
if (input.length <= this.maxTextLength) {
// cap input at maxTextLength
this.setState({textContent: input});
}
if (reviewValidateAlert && input.length >= this.minTextLength) {
// if warning was shown, rehide it when the input meets minTextLength
this.setState({
reviewValidateAlert: null
});
}
};
handleRatingsChange = (rate: number) => {
this.setState({
rating: rate / 20
});
};
resetCBReviewForm = () => {
this.setState({
acceptLicense: false,
rating: 0,
reviewValidateAlert: null,
success: true,
textContent: ''
});
};
handleCloseModal = () => {
this.setState({
acceptLicense: false,
alert: {
message: '',
title: '',
type: ''
},
language: 'en',
rating: 0,
reviewValidateAlert: null,
textContent: ''
});
this.props.handleModalToggle();
};
handleSubmitToCB = async (
event?: React.FormEvent<HTMLFormElement>
): Promise<null> => {
if (event) {
event.preventDefault();
}
const {
entityBBID,
entityType,
userId
} = this.props;
const {
acceptLicense,
language,
rating,
textContent
} = this.state;
if (textContent.length < this.minTextLength) {
this.setState({
reviewValidateAlert: `Your review needs to be longer than ${this.minTextLength} characters.`
});
return null;
}
if (userId &&
this.accessToken &&
acceptLicense
) {
let nonZeroRating: number;
if (rating !== 0) {
nonZeroRating = rating;
}
const review = {
entityBBID,
entityType,
language,
rating: nonZeroRating,
textContent
};
try {
let result: any;
const response = await request.post(`/${entityType}/${entityBBID}/reviews`)
.set('Content-Type', 'application/json')
.send({
accessToken: this.accessToken,
review
});
if (response.ok) {
result = response.body;
}
if (result?.reviewID) {
this.setState({
alert: {
message: 'Your review was submitted to CritiqueBrainz!',
title: 'Success',
type: 'success'
}
});
this.setState({
reviewID: result?.reviewID
});
this.resetCBReviewForm();
this.props.handleUpdateReviews();
}
else {
this.setState({
alert: {
message: result?.message,
title: 'Error submitting your review',
type: 'danger'
}
});
}
}
catch (error) {
this.handleError(
error,
'Error while submitting review to CritiqueBrainz'
);
}
}
return null;
};
getModalBody = (hasPermissions: boolean) => {
const {
acceptLicense,
alert,
language,
rating,
reviewID,
reviewValidateAlert,
success,
textContent
} = this.state;
if (!hasPermissions) {
return (
<div>
Before you can submit reviews to{' '}
<a href={this.CBBaseUrl}>CritiqueBrainz</a>, you must{' '}
<b> connect to your CritiqueBrainz </b> account from
BookBrainz.
{this.CBInfoButton}
<br/>
<br/>
You can connect to your CritiqueBrainz account by visiting
the
<a href={`${window.location.origin}/external-service/`}>
{' '}
external services page.
</a>
</div>
);
}
if (success) {
return (
<div>
Thanks for submitting your review for{' '}
<b>{this.props.entityName}</b>!
<br/>
<br/>
You can access your CritiqueBrainz review by clicking{' '}
<a href={`${this.CBBaseUrl}/review/${reviewID}`}>
{' '}
here.
</a>
</div>
);
}
return (
<>
{alert?.message && (
<Alert variant={alert.type}>
<Alert.Heading>{alert.title}</Alert.Heading>
<p>{alert.message}</p>
</Alert>
)}
{reviewValidateAlert && (
<Alert variant="danger">
<p>{reviewValidateAlert}</p>
</Alert>
)}
You are reviewing
<b>
{` ${this.props.entityName} (${this.props.entityType}) `}
</b>
for <a href={this.CBBaseUrl}>CritiqueBrainz</a>.{' '}
{this.CBInfoButton}
<div className="form-group">
<textarea
required
className="form-control"
id="review-text"
name="review-text"
placeholder={`Review length must be at least ${this.minTextLength} characters.`}
rows={6}
spellCheck="false"
style={{resize: 'vertical'}}
value={textContent}
onChange={this.handleTextInputChange}
/>
</div>
<small
className={
textContent.length < this.minTextLength ?
'text-danger' :
''
}
style={{display: 'block', textAlign: 'right'}}
>
Words: {countWords(textContent)} / Characters:{' '}
{textContent?.length}
</small>
<div className="rating-container">
<b>Rating (optional): </b>
<Rating
transition
className="rating-stars"
ratingValue={rating}
size={20}
onClick={this.handleRatingsChange}
/>
</div>
<div className="dropdown">
<b>Language of your review: </b>
<select
id="language-selector"
name="language"
value={language}
onChange={this.handleInputChange}
>
{this.allLanguagesKeyValue.map((lang: any) => (
<option key={lang[0]} value={lang[0]}>
{lang[1]}
</option>
))}
</select>
</div>
<div className="checkbox">
<label>
<input
required
checked={acceptLicense}
id="acceptLicense"
name="acceptLicense"
type="checkbox"
onChange={this.handleInputChange}
/>
<small>
You acknowledge and agree that your
contributed reviews to CritiqueBrainz are licensed
under a Creative Commons Attribution-ShareAlike 3.0
Unported (CC BY-SA 3.0) license. You agree to license your work
under this license. You represent and warrant that
you own or control all rights in and to the work,
that nothing in the work infringes the rights of any
third-party, and that you have the permission to use
and to license the work under the selected Creative
Commons license. Finally, you give the MetaBrainz
Foundation permission to license this content for
commercial use outside of Creative Commons licenses
in order to support the operations of the
organization.
</small>
</label>
</div>
</>
);
};
getModalFooter = (hasPermissions: boolean) => {
const {success} = this.state;
if (!hasPermissions) {
return (
<a
className="btn btn-success"
href={`${window.location.origin}/external-service/`}
role="button"
>
{' '}
Connect To CritiqueBrainz{' '}
</a>
);
}
if (!success) {
const {reviewValidateAlert} = this.state;
return (
<button
className={`btn btn-success ${
reviewValidateAlert ? 'disabled' : ''
}`}
id="submitReviewButton"
type="submit"
>
Submit Review to CritiqueBrainz
</button>
);
}
/* default: close modal button */
return (
<button
className="btn btn-default"
type="button"
onClick={this.handleCloseModal}
>
Close
</button>
);
};
private accessToken = '';
/* executes getAccessToken() only in a browser to avoid unnecessary server-side calls during component mounting */
componentDidMount = async () => {
if (typeof window !== 'undefined') {
this.accessToken = await this.getAccessToken();
}
};
render() {
const hasPermissions = this.accessToken !== null;
const modalBody = this.getModalBody(hasPermissions);
const modalFooter = this.getModalFooter(hasPermissions);
return (
<Modal
centered
aria-labelledby="CBReviewModalLabel"
id="CBReviewModal"
show={this.props.showModal}
onHide={this.handleCloseModal}
>
<form
className="modal-content"
onSubmit={this.handleSubmitToCB}
>
<Modal.Header
closeButton
>
<h4
className="modal-title"
id="CBReviewModalLabel"
style={{padding: 'auto'}}
>
<img
alt="CritiqueBrainz Logo"
className="cb-img-responsive"
height="30"
src="/images/critiquebrainz-logo.svg"
style={{margin: '8px'}}
/>
</h4>
</Modal.Header>
<Modal.Body>{modalBody}</Modal.Body>
<Modal.Footer>{modalFooter}</Modal.Footer>
</form>
</Modal>
);
}
}
export default CBReviewModal;