-
Notifications
You must be signed in to change notification settings - Fork 92
/
ReviewForm.tsx
155 lines (145 loc) · 4.89 KB
/
ReviewForm.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
import { onStoreChange, removeOnStoreChange, TCromwellNotify, TProductReview, TUser } from '@cromwell/core';
import { CContainer, CText, getRestApiClient, useUserInfo } from '@cromwell/core-frontend';
import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { getNotifier } from '../../helpers/notifier';
import { BaseAlert } from '../shared/Alert';
import { BaseButton } from '../shared/Button';
import { BaseRating } from '../shared/Rating';
import { BaseTextField } from '../shared/TextField';
import { BaseTooltip } from '../shared/Tooltip';
import { ProductReviewsProps } from './ProductReviews';
import styles from './ReviewForm.module.scss';
export type ReviewFormProps = {
productId: number;
notifier?: TCromwellNotify;
parentProps: ProductReviewsProps;
};
/** @internal */
export const ReviewForm = ({ productId, notifier = getNotifier(), parentProps }: ReviewFormProps) => {
const userInfo = useUserInfo();
const [name, setName] = useState(userInfo?.fullName ?? '');
const [rating, setRating] = useState<number | null>(0);
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [canValidate, setCanValidate] = useState(false);
const [placedReview, setPlacedReview] = useState<TProductReview | null>(null);
const { text = {}, elements, classes, disableEdit } = parentProps;
const {
Alert = BaseAlert,
Button = BaseButton,
Rating = BaseRating,
TextField = BaseTextField,
Tooltip = BaseTooltip,
} = elements ?? {};
useEffect(() => {
const userInfoChange = (userInfo: TUser | undefined) => {
setName(userInfo?.fullName ?? '');
};
const infoCbId = onStoreChange('userInfo', userInfoChange);
return () => {
removeOnStoreChange('userInfo', infoCbId);
};
}, []);
const handleSubmit = async () => {
if (!canValidate) setCanValidate(true);
if (!name || name == '' || !rating) {
return;
}
let reviewRes;
setIsLoading(true);
try {
reviewRes = await getRestApiClient()?.placeProductReview({
productId: productId,
title,
description,
rating,
userName: name,
userId: userInfo?.id,
});
} catch (e) {
console.error(e);
notifier?.error?.(text.failedToSubmit ?? 'Failed to post review');
}
setIsLoading(false);
if (reviewRes) {
setPlacedReview(reviewRes);
}
};
if (placedReview) {
return (
<div className={clsx(styles.ReviewForm, classes?.ReviewForm)}>
<Alert severity="success">
{text.submitSuccess ??
'Thank you! Your review will appear on this page after approval by the website moderator'}
</Alert>
</div>
);
}
const fieldRequired = text.fieldRequired ?? 'This field is required';
return (
<CContainer
id="ccom_product_reviews_form"
className={clsx(styles.ReviewForm, classes?.ReviewForm)}
editorHidden={disableEdit}
>
<CText
id="ccom_product_reviews_write_review_title"
className={clsx(styles.reviewFormTitle, classes?.reviewFormTitle)}
editorHidden={disableEdit}
>
{text.writeReview ?? 'Write a review'}
</CText>
<Tooltip open={canValidate && (!name || name == '')} title={fieldRequired} arrow>
<TextField
label={text.fieldNameLabel ?? 'Name'}
name="Name"
variant="outlined"
size="small"
fullWidth
className={clsx(styles.reviewInput, classes?.reviewInput)}
value={name}
onChange={(e) => setName(e.target.value)}
/>
</Tooltip>
<Tooltip open={canValidate && !rating} title={fieldRequired} arrow>
<Rating
name="read-only"
value={rating}
className={clsx(styles.reviewInput, classes?.reviewInput)}
onChange={(e, value) => setRating(value)}
precision={0.5}
/>
</Tooltip>
<TextField
label={text.fieldTitleLabel ?? 'Title'}
name="Title"
variant="outlined"
size="small"
fullWidth
className={clsx(styles.reviewInput, classes?.reviewInput)}
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<TextField
label={text.fieldReviewLabel ?? 'Review'}
name="Review"
variant="outlined"
size="small"
fullWidth
multiline
minRows={4}
maxRows={20}
className={clsx(styles.reviewInput, classes?.reviewInput)}
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
<div className={clsx(styles.submitBtnWrapper, classes?.submitBtnWrapper)}>
<Button variant="contained" color="primary" size="large" onClick={handleSubmit} disabled={isLoading}>
{text.submitButton ?? 'Submit'}
</Button>
</div>
</CContainer>
);
};