|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useCallback, useEffect, useRef, useState } from "react" |
|
4
|
import clsx from "clsx" |
|
5
|
import { Star, StarSolid } from "@medusajs/icons" |
|
6
|
import { Button } from "@/components/Button" |
|
7
|
import { Label } from "@/components/Label" |
|
8
|
import { TextArea } from "@/components/TextArea" |
|
9
|
import { useAnalytics } from "@/providers/Analytics" |
|
10
|
import { useNotifications } from "@/providers/Notification" |
|
11
|
|
|
12
|
export type RatingProps = { |
|
13
|
event?: string |
|
14
|
className?: string |
|
15
|
onRating?: (rating?: number) => void |
|
16
|
additionalQuestion?: string |
|
17
|
parentNotificationId?: string |
|
18
|
} & React.HTMLAttributes<HTMLDivElement> |
|
19
|
|
|
20
|
export const Rating: React.FC<RatingProps> = ({ |
|
21
|
event = "rating", |
|
22
|
className = "", |
|
23
|
onRating, |
|
24
|
additionalQuestion = "What should we improve?", |
|
25
|
parentNotificationId, |
|
26
|
}) => { |
|
27
|
const [rating, setRating] = useState(0) |
|
28
|
const [additionalFeedback, setAdditionalFeedback] = useState("") |
|
29
|
const [hoverRating, setHoverRating] = useState(0) |
|
30
|
const starElms = useRef<HTMLElement[]>([]) |
|
31
|
const starArr = Array.from(Array(5).keys()) |
|
32
|
const { track } = useAnalytics() |
|
33
|
const { updateNotification } = useNotifications(true) || {} |
|
34
|
|
|
35
|
const submitTracking = useCallback( |
|
36
|
(selectedRating?: number, feedback?: string) => { |
|
37
|
track({ |
|
38
|
event: { |
|
39
|
event, |
|
40
|
options: { |
|
41
|
rating: selectedRating || rating, |
|
42
|
additionalFeedback: feedback || additionalFeedback, |
|
43
|
}, |
|
44
|
callback: () => onRating?.(selectedRating || rating), |
|
45
|
}, |
|
46
|
}) |
|
47
|
}, |
|
48
|
[rating, additionalFeedback] |
|
49
|
) |
|
50
|
|
|
51
|
const handleRating = (selectedRating: number) => { |
|
52
|
if (rating) { |
|
53
|
return |
|
54
|
} |
|
55
|
setHoverRating(0) |
|
56
|
setRating(selectedRating) |
|
57
|
if (selectedRating >= 4) { |
|
58
|
for (let i = 0; i < selectedRating; i++) { |
|
59
|
starElms.current[i].classList.add("animate-tada") |
|
60
|
} |
|
61
|
submitTracking(selectedRating) |
|
62
|
} |
|
63
|
} |
|
64
|
|
|
65
|
useEffect(() => { |
|
66
|
if ( |
|
67
|
rating > 0 && |
|
68
|
rating < 4 && |
|
69
|
parentNotificationId && |
|
70
|
updateNotification |
|
71
|
) { |
|
72
|
// update parent notification ID |
|
73
|
updateNotification(parentNotificationId, { |
|
74
|
closeButtonText: "Submit", |
|
75
|
onClose: () => submitTracking(rating, additionalFeedback), |
|
76
|
}) |
|
77
|
} |
|
78
|
}, [additionalFeedback, rating]) |
|
79
|
|
|
80
|
return ( |
|
81
|
<div className={clsx("flex flex-col gap-docs_1")}> |
|
82
|
<div className={clsx("flex gap-docs_0.5", className)}> |
|
83
|
{starArr.map((i) => { |
|
84
|
const isSelected = |
|
85
|
(rating !== 0 && rating - 1 >= i) || |
|
86
|
(hoverRating !== 0 && hoverRating - 1 >= i) |
|
87
|
return ( |
|
88
|
<Button |
|
89
|
variant="transparent" |
|
90
|
buttonRef={(element) => { |
|
91
|
if (starElms.current.length - 1 < i) { |
|
92
|
starElms.current.push(element as HTMLElement) |
|
93
|
} |
|
94
|
}} |
|
95
|
key={i} |
|
96
|
onMouseOver={() => { |
|
97
|
if (!rating) { |
|
98
|
setHoverRating(i + 1) |
|
99
|
} |
|
100
|
}} |
|
101
|
onMouseLeave={() => { |
|
102
|
if (!rating) { |
|
103
|
setHoverRating(0) |
|
104
|
} |
|
105
|
}} |
|
106
|
onClick={() => handleRating(i + 1)} |
|
107
|
> |
|
108
|
{!isSelected && <Star />} |
|
109
|
{isSelected && ( |
|
110
|
<StarSolid className="text-medusa-tag-orange-icon" /> |
|
111
|
)} |
|
112
|
</Button> |
|
113
|
) |
|
114
|
})} |
|
115
|
</div> |
|
116
|
{rating !== 0 && rating < 4 && ( |
|
117
|
<div |
|
118
|
className={clsx( |
|
119
|
"text-medusa-fg-subtle", |
|
120
|
"flex flex-col gap-docs_0.5" |
|
121
|
)} |
|
122
|
> |
|
123
|
<Label>{additionalQuestion}</Label> |
|
124
|
<TextArea |
|
125
|
placeholder="I didn't like..." |
|
126
|
rows={4} |
|
127
|
onChange={(e) => setAdditionalFeedback(e.target.value)} |
|
128
|
value={additionalFeedback} |
|
129
|
className="w-full" |
|
130
|
/> |
|
131
|
</div> |
|
132
|
)} |
|
133
|
</div> |
|
134
|
) |
|
135
|
} |