Skip to content

Commit 9e70497

Browse files
committed
docs: add events
1 parent 4da6c63 commit 9e70497

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# Events
6+
7+
This guide explains how to listen for events emitted by the feedback widget and handle them with JavaScript.
8+
9+
## Events overview
10+
11+
The feedback widget emits the following events:
12+
13+
- **`feedbackSent`**: Emitted when feedback is successfully submitted.
14+
- **`feedbackError`**: Emitted if there is an error during feedback submission.
15+
16+
By listening to these events, you can handle feedback dynamically, such as storing it in your database or performing any custom actions.
17+
18+
## How to listen for events
19+
20+
You can listen for `feedbackSent` and `feedbackError` events globally by attaching event listeners directly to the `document` object.
21+
This approach captures events emitted anywhere in the DOM, making it convenient to handle feedback without needing direct access to the feedback widget.
22+
23+
Here’s how you can add event listeners to capture these events:
24+
25+
import Tabs from '@theme/Tabs';
26+
import TabItem from '@theme/TabItem';
27+
28+
<Tabs>
29+
<TabItem value="JavaScript" label="JavaScript" default>
30+
31+
```javascript
32+
document.addEventListener('DOMContentLoaded', () => {
33+
document.addEventListener('feedbackSent', (event) => {
34+
const feedback = event.detail.feedback;
35+
console.log('Feedback submitted:', feedback);
36+
37+
// Example custom action: store feedback in your database
38+
storeFeedbackInDatabase(feedback);
39+
});
40+
41+
document.addEventListener('feedbackError', (event) => {
42+
const error = event.detail.error;
43+
console.error('Feedback submission error:', error);
44+
45+
// Example custom action: show error message to the user
46+
showErrorToUser(error.message);
47+
});
48+
});
49+
```
50+
51+
</TabItem>
52+
<TabItem value="React" label="React">
53+
54+
```ts
55+
import React, { useEffect } from 'react';
56+
57+
const FeedbackListener = () => {
58+
useEffect(() => {
59+
// Define the handler for feedbackSent
60+
const handleFeedbackSent = (event) => {
61+
const feedback = event.detail.feedback;
62+
console.log('Feedback submitted:', feedback);
63+
64+
// Example custom action: store feedback in your database
65+
storeFeedbackInDatabase(feedback);
66+
};
67+
68+
// Define the handler for feedbackError
69+
const handleFeedbackError = (event) => {
70+
const error = event.detail.error;
71+
console.error('Feedback submission error:', error);
72+
73+
// Example custom action: show error message to the user
74+
showErrorToUser(error.message);
75+
};
76+
77+
// Add event listeners
78+
document.addEventListener('feedbackSent', handleFeedbackSent);
79+
document.addEventListener('feedbackError', handleFeedbackError);
80+
81+
// Cleanup event listeners on unmount
82+
return () => {
83+
document.removeEventListener('feedbackSent', handleFeedbackSent);
84+
document.removeEventListener('feedbackError', handleFeedbackError);
85+
};
86+
}, []); // Empty dependency array means this effect runs only on mount and unmount
87+
88+
return <div>Listening for feedback events...</div>;
89+
};
90+
91+
export default FeedbackListener;
92+
```
93+
94+
</TabItem>
95+
</Tabs>
96+
97+
## Feedback event object format
98+
99+
The `feedbackSent` event includes a `detail` object with the following structure:
100+
101+
```json
102+
{
103+
feedback: {
104+
id: string; // Unique ID for the feedback returned from the API
105+
url: string; // The URL where the feedback was submitted
106+
message: string; // Feedback message provided by the user
107+
email: string; // User's email address
108+
project: string; // Project identifier
109+
screenshot: string; // Base64-encoded screenshot, if available
110+
rating: number; // User's rating, if provided
111+
ratingMode: string; // Rating mode selected by the user
112+
verification: string; // Verification information
113+
session: string; // Session ID from localStorage
114+
};
115+
}
116+
```
117+
118+
The feedbackError event also includes a detail object with the following structure:
119+
120+
```json
121+
{
122+
error: {
123+
status: number; // HTTP status code returned by the server or 500 for general errors
124+
message: string; // Detailed error message returned from the server or error object for general errors
125+
};
126+
}
127+
```
128+

0 commit comments

Comments
 (0)