-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvibecheck-widget.js
103 lines (97 loc) · 3.69 KB
/
vibecheck-widget.js
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
class VibecheckWidget extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
<style>
.container {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
width: 100%;
height: 100%;
}
.vibecheck-button {
font-size: 2rem;
border-radius: 50%;
border: none;
width: 3rem;
height: 3rem;
cursor: pointer;
}
.vibecheck-button:hover {
transform: scale(1.1);
}
.vibecheck-button:active {
transform: scale(0.9);
}
.happy {
background-color: #aaffaa;
}
.meh {
background-color: #dddd77;
}
.sad {
background-color: #ffaaaa;
}
.message {
font-size: 1rem;
text-align: center;
margin-top: 1rem;
}
</style>
<div class="container">
<button class="vibecheck-button happy">🙂</button>
<button class="vibecheck-button meh">😐</button>
<button class="vibecheck-button sad">🙁</button>
</div>
<div class="message"></div>
`;
// Get the attribute values and store them as properties
this.projectName = this.getAttribute('project');
this.userKey = this.getAttribute('user-key');
this.server = this.getAttribute('server').replace(/\/$/, '');
this.sectionId = this.getAttribute('section-id');
}
connectedCallback() {
const happyButton = this.shadowRoot.querySelector('.happy');
const sadButton = this.shadowRoot.querySelector('.sad');
const mehButton = this.shadowRoot.querySelector('.meh');
happyButton.addEventListener('click', () => this.sendFeedback('happy'));
mehButton.addEventListener('click', () => this.sendFeedback('medium'));
sadButton.addEventListener('click', () => this.sendFeedback('sad'));
}
sendFeedback(feeling) {
// Make an API call with the specified attributes
const feedbackData = {
button_name: feeling,
feedback: '',
section_id: this.sectionId,
timestamp_utc: new Date().toISOString(),
};
fetch(`${this.server}/api/v1/projects/${this.projectName}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-User-Key": this.userKey,
},
body: JSON.stringify(feedbackData),
}).then(response => {
if (response.ok) {
console.log('Feedback sent successfully!');
this.shadowRoot.querySelector('.message').innerHTML = 'Feedback sent successfully!';
// Deactivate the buttons
this.shadowRoot.querySelector('.happy').disabled = true;
this.shadowRoot.querySelector('.meh').disabled = true;
this.shadowRoot.querySelector('.sad').disabled = true;
} else {
console.error('Failed to send feedback.');
this.shadowRoot.querySelector('.message').innerHTML = 'Failed to send feedback.';
}
}).catch(error => {
console.error('An error occurred while sending the feedback:', error);
this.shadowRoot.querySelector('.message').innerHTML = 'An error occurred while sending the feedback.';
});
}
}