-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatings.tsx
More file actions
286 lines (270 loc) · 8.48 KB
/
Ratings.tsx
File metadata and controls
286 lines (270 loc) · 8.48 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
import { MouseEventHandler, useState } from 'react';
import { UseQueryResult, useQuery } from '@tanstack/react-query';
import { RatingAdminListRetrieve } from 'api/models';
import { requestVideoRatingsListQuery } from 'api/queries';
import RatingDialog from 'components/RatingDialog/RatingDialog';
import { dateTimeToLocaleString } from 'helpers/DateToLocaleStringCoverters';
import { getUserId, isAdmin } from 'helpers/LocalStorageHelper';
import TimeAgo from 'helpers/TimeAgo';
import { UI_AVATAR_URL } from 'localConstants';
import { Avatar } from 'primereact/avatar';
import { Button } from 'primereact/button';
import { Dropdown } from 'primereact/dropdown';
import { Tooltip } from 'primereact/tooltip';
import { classNames } from 'primereact/utils';
interface RatingAdminListDates // TODO: Rename?
extends Omit<RatingAdminListRetrieve, 'created'> {
created: Date;
}
type RatingProps = {
authorName: string;
avatarUrl?: string;
creationDate: Date;
onEdit: MouseEventHandler<HTMLButtonElement>;
rating: number;
review: string;
showEdit: boolean;
};
type RatingSummaryProps = {
portion: number;
rating: number;
};
type RatingsProps = {
avgRating: number;
isRated: boolean;
requestId: number;
videoId: number;
videoStatus: number;
videoTitle: string;
};
const Rating = ({
authorName,
avatarUrl,
creationDate,
onEdit,
rating,
review,
showEdit,
}: RatingProps) => {
return (
<div className="col-12 lg:col-6">
<div className="p-2">
<div className="border-1 border-round p-3 surface-border">
<div className="align-items-center flex mb-3">
<Tooltip className="text-xs" target=".created-date-text" />
<Avatar
className="flex-shrink-0 h-2rem mr-2 w-2rem"
icon="pi pi-user"
image={avatarUrl || UI_AVATAR_URL + authorName}
shape="circle"
/>
<span className="font-medium mr-3 text-900">{authorName}</span>
<span
className="created-date-text font-medium text-500 text-sm"
data-pr-position="bottom"
data-pr-tooltip={dateTimeToLocaleString(creationDate)}
>
<TimeAgo datetime={creationDate} locale="hu_HU" />
</span>
<span className="ml-auto">
{[...Array(5).keys()].map((item) => (
<i
className={classNames('pi pi-star-fill', {
'mr-1': item < 4,
'text-300': rating <= item,
'text-yellow-500': rating > item,
})}
key={item}
></i>
))}
</span>
</div>
<p className="comment-text line-height-3 m-0 p-0 text-600">
{review}
</p>
{showEdit && (
<div className="flex flex-wrap justify-content-end">
<Button
className="p-1"
icon="pi pi-pencil"
label="Szerkesztés"
onClick={onEdit}
size="small"
text
/>
</div>
)}
</div>
</div>
</div>
);
};
const RatingSummary = ({ portion, rating }: RatingSummaryProps) => {
return (
<li className="align-items-center flex mb-2">
<span className="font-medium mr-2 text-900 w-1rem">{rating}</span>
<div
style={{ height: '7px' }}
className="border-round flex-auto overflow-hidden surface-300"
>
{portion > 0 && (
<div className={`bg-yellow-500 border-round h-full w-${portion}`} />
)}
</div>
</li>
);
};
const Ratings = ({
avgRating,
isRated,
requestId,
videoId,
videoStatus,
videoTitle,
}: RatingsProps) => {
const getRatings = ({
data: ratings,
}: UseQueryResult<RatingAdminListRetrieve[]>): RatingAdminListDates[] => {
return [...(ratings || [])].map((rating) => {
return {
...rating,
created: new Date(rating.created),
};
});
};
const data = getRatings(
useQuery(requestVideoRatingsListQuery(requestId, videoId)),
);
const [ordering, setOrdering] = useState<
'highest' | 'lowest' | 'oldest' | 'newest'
>('newest');
const [ratingDialogAuthorName, setRatingDialogAuthorName] =
useState<string>('');
const [ratingDialogId, setRatingDialogId] = useState<number>(0);
const [ratingDialogVisible, setRatingDialogVisible] =
useState<boolean>(false);
const orderingOptions = [
{ name: 'Legújabb', value: 'newest' },
{ name: 'Legrégebbi', value: 'oldest' },
{ name: 'Legjobb', value: 'highest' },
{ name: 'Legrosszabb', value: 'lowest' },
];
const onRatingDialogHide = () => {
setRatingDialogVisible(false);
setRatingDialogAuthorName('');
setRatingDialogId(0);
};
const onRatingEdit = (authorName: string, ratingId: number) => {
setRatingDialogVisible(true);
setRatingDialogAuthorName(authorName);
setRatingDialogId(ratingId);
};
const numberOfRating = (rating: number) => {
return data.reduce(
(total, next) => (next.rating == rating ? ++total : total),
0,
);
};
function compare(a: RatingAdminListDates, b: RatingAdminListDates) {
if (ordering === 'highest') {
if (a.rating - b.rating !== 0) {
return b.rating - a.rating;
}
} else if (ordering === 'lowest') {
if (a.rating - b.rating !== 0) {
return a.rating - b.rating;
}
} else if (ordering === 'oldest') {
return a.created.getTime() - b.created.getTime();
}
return b.created.getTime() - a.created.getTime();
}
return (
<>
<RatingDialog
isRated={isRated}
onHide={onRatingDialogHide}
ratingAuthorName={ratingDialogAuthorName}
ratingId={ratingDialogId}
requestId={requestId}
videoId={videoId}
videoTitle={videoTitle}
visible={ratingDialogVisible}
/>
<div className="flex flex-column md:flex-row">
<div className="md:w-6 w-full">
<ul className="list-none m-0 p-0">
{[...Array(5).keys()].reverse().map((item) => (
<RatingSummary
key={item}
portion={Math.round(
(numberOfRating(item + 1) / data.length) * 12,
)}
rating={item + 1}
/>
))}
</ul>
</div>
<div className="align-items-center flex flex-column justify-content-center md:mt-0 md:w-6 mt-4 w-full">
<span className="font-medium mb-3 text-900 text-5xl">
{(avgRating || 0).toFixed(2)}
</span>
<span className="mb-2">
{[...Array(5).keys()].map((item) => (
<i
className={classNames('pi pi-star-fill text-2xl', {
'mr-1': item < 4,
'text-300': Math.round(avgRating) <= item,
'text-yellow-500': Math.round(avgRating) > item,
})}
key={item}
></i>
))}
</span>
<a
tabIndex={0}
className="cursor-pointer font-medium hover:text-blue-600 text-blue-500 transition-colors transition-duration-300"
>
{`${data.length} Értékelés`}
</a>
</div>
</div>
<div className="border-top-1 mt-5 pt-5 surface-border">
<div className="align-items-center flex justify-content-between mb-5">
<Button
disabled={videoStatus < 3}
label={isRated ? 'Értékelés szerkesztése' : 'Értékelés írása'}
onClick={() => {
setRatingDialogVisible(true);
}}
/>
<Dropdown
onChange={(e) => {
setOrdering(e.value);
}}
options={orderingOptions}
optionLabel="name"
value={ordering}
/>
</div>
<div className="grid -ml-3 -mr-3 -mt-3">
{data.sort(compare).map((rating) => (
<Rating
authorName={rating.author.full_name}
avatarUrl={rating.author.avatar_url}
creationDate={rating.created}
key={rating.id}
onEdit={() => {
onRatingEdit(rating.author.full_name, rating.id);
}}
rating={rating.rating}
review={rating.review}
showEdit={rating.author.id === getUserId() || isAdmin()}
/>
))}
</div>
</div>
</>
);
};
export default Ratings;