-
Notifications
You must be signed in to change notification settings - Fork 215
/
feedback.client.ts
56 lines (48 loc) · 1.64 KB
/
feedback.client.ts
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
// This runs in the browser
// deno-lint-ignore-file no-window
import type { FeedbackSubmission } from "./types.ts";
let lastFeedbackItemId: string | null = null;
const feedbackYes = document.getElementById("feedback-yes");
const feedbackNo = document.getElementById("feedback-no");
const feedbackForm = document.getElementById("feedback-form");
const feedbackSection = document.getElementById("feedback-section");
feedbackYes?.addEventListener("click", () => {
sendFeedback({
sentiment: "yes",
});
});
feedbackNo?.addEventListener("click", () => {
sendFeedback({
sentiment: "no",
});
});
feedbackForm?.addEventListener("submit", (event) => {
event.preventDefault();
const formData = new FormData(feedbackForm as HTMLFormElement);
const form = Object.fromEntries(formData.entries());
sendFeedback({
sentiment: form["feedback-vote"] as "yes" | "no",
comment: form["feedback-comment"] as string,
contact: form["feedback-contact"] as string,
});
if (feedbackSection) {
feedbackSection.innerHTML =
"<p>Thank you for helping make the Deno docs awesome!</p>";
}
});
async function sendFeedback(feedback: Partial<FeedbackSubmission>) {
feedback.path = feedback.path || new URL(window.location.href).pathname;
feedback.id = lastFeedbackItemId ? lastFeedbackItemId : null;
const result = await fetch("/_api/send-feedback", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(feedback),
});
const responseBody = await result.json();
lastFeedbackItemId = responseBody.id;
if (!result.ok) {
console.error("Failed to send feedback", responseBody);
}
}