-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedback-widget.js
504 lines (459 loc) · 14.5 KB
/
feedback-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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
function logGoogleEvent(action, label = undefined) {
if (typeof window.gtag === "function") {
window.gtag("event", action, {
event_category: "Page feedback",
event_label: label,
});
} else if (window.dataLayer != null) {
window.dataLayer.push({
event: "click_feedback_rating",
object_type: label,
});
}
}
const LANG_TO_CONTENT = {
en: {
ratingPrompt: "Did you find what you were looking for on this page?",
ratingPositive: "Yes",
ratingNegative: "No",
commentPromptPositive:
"Great! We're looking for ways to improve this page — what ideas come to mind?",
commentPromptNegative:
"Sorry to hear that. What were you looking for today?",
commentPromptDisclaimer:
"Your feedback helps improve this web page. For specific questions about your situation, ",
commentPromptDisclaimerLink: "contact us",
commentSubmit: "Send feedback",
commentSubmitLoading: "Sending...",
commentConfirmation: "Thanks for sharing your thoughts!",
emailPrompt:
"To hear about paid feedback opportunities in the future, join our user testing list.",
emailLabel: "Email address",
emailSubmit: "Join the list",
emailSubmitLoading: "Joining...",
errorMessage:
"Try again, please. We didn't get your answer because of a technical issue.",
emailConfirmation: "Thanks for signing up!",
},
es: {
ratingPrompt: "¿Encontraste lo que buscabas en esta página?",
ratingPositive: "Sí",
ratingNegative: "No",
commentPromptPositive:
"¡Excelente! Estamos buscando formas de mejorar esta página. ¿Qué ideas se te ocurren?",
commentPromptNegative:
"Lamentamos escuchar eso. ¿De que se trataba su búsqueda?",
commentPromptDisclaimer:
"Sus comentarios nos ayudan a mejorar nuestro sitio de web. Si tiene preguntas específicas sobre su situación, ",
commentPromptDisclaimerLink: "por favor póngase en contacto con nosotros",
commentSubmit: "Enviar comentarios",
commentSubmitLoading: "Enviando...",
commentConfirmation: "¡Gracias por compartir tus ideas!",
emailPrompt:
"Para conocer mas oportunidades de comentarios pagados en el futuro, sea parte de nuestra lista de prueba de usuarios.",
emailLabel: "Dirección de correo electrónico",
emailSubmit: "Sea parte de la lista",
emailSubmitLoading: "Enviando...",
errorMessage:
"Por favor, inténtalo de nuevo. No obtuvimos su respuesta debido a un problema técnico.",
emailConfirmation: "¡Gracias por registrarte!",
},
};
const API_URL = "https://innovation.nj.gov/app/feedback/dev";
const JSON_HEADER = {
"Content-Type": "application/json",
};
class NJFeedbackWidget extends window.HTMLElement {
constructor() {
super();
this.rating = false;
this.feedbackId = undefined;
this.retryRating = false;
this.language = new URL(window.location).searchParams.get("lang") ?? "en";
}
connectedCallback() {
this.innerHTML = this.getHTML();
this.applyListeners();
this.addStyling();
document.addEventListener(
"changeLanguage",
this.handleChangeLanguage.bind(this)
);
}
disconnectedCallback() {
document.removeEventListener(
"changeLanguage",
this.handleChangeLanguage.bind(this)
);
}
handleChangeLanguage(e) {
this.language = e.detail;
this.innerHTML = this.getHTML();
this.applyListeners();
}
applyListeners() {
this.querySelector("#yesButton").addEventListener("click", (_e) => {
this.handleRating(true);
});
this.querySelector("#noButton").addEventListener("click", (_e) => {
this.handleRating(false);
});
const commentForm = this.querySelector("#commentForm");
commentForm.addEventListener("submit", (e) => {
e.preventDefault();
const submitButton = document.getElementById("commentSubmit");
submitButton.disabled = true;
submitButton.textContent =
LANG_TO_CONTENT[this.language].commentSubmitLoading;
this.hideElement("#commentSubmitError");
const comment = e.target.elements.comment.value;
const postData =
this.retryRating || this.feedbackId == null
? { comment, rating: this.rating, pageURL: window.location.href }
: {
feedbackId: this.feedbackId,
comment,
};
fetch(`${API_URL}/comment`, {
method: "POST",
headers: JSON_HEADER,
body: JSON.stringify(postData),
})
.then((response) => response.json())
.then((data) => {
if (this.feedbackId == null) {
this.feedbackId = data.feedbackId;
}
if (data.message === "Success" && this.feedbackId != null) {
this.hideElement("#commentPrompt");
if (this.getAttribute("skip-email-step") === "true") {
this.showElement("#confirmation", "flex");
} else {
this.showElement("#emailPrompt");
}
} else {
this.showElement("#commentSubmitError");
}
})
.catch((e) => {
this.showElement("#commentSubmitError");
})
.finally(() => {
submitButton.disabled = false;
submitButton.textContent =
LANG_TO_CONTENT[this.language].commentSubmit;
});
});
const emailForm = this.querySelector("#emailForm");
emailForm.addEventListener("submit", (e) => {
e.preventDefault();
const submitButton = document.getElementById("emailSubmit");
submitButton.disabled = true;
submitButton.textContent =
LANG_TO_CONTENT[this.language].emailSubmitLoading;
this.hideElement("#emailSubmitError");
const postData = {
feedbackId: this.feedbackId,
email: e.target.elements.email.value,
};
fetch(`${API_URL}/email`, {
method: "POST",
headers: JSON_HEADER,
body: JSON.stringify(postData),
})
.then((response) => response.json())
.then((data) => {
if (data.message === "Success" && data.feedbackId != null) {
this.hideElement("#emailPrompt");
this.showElement("#confirmation", "flex");
} else {
this.showElement("#emailSubmitError");
}
})
.catch((e) => {
this.showElement("#emailSubmitError");
})
.finally(() => {
submitButton.disabled = false;
submitButton.textContent = LANG_TO_CONTENT[this.language].emailSubmit;
});
});
}
handleRating(rating) {
this.rating = rating;
if (!rating) {
this.querySelector("#commentPromptText").innerText =
LANG_TO_CONTENT[this.language].commentPromptNegative;
}
this.hideElement("#ratingPrompt");
this.showElement("#commentPrompt");
const onlySaveRatingToAnalytics =
this.getAttribute("only-save-rating-to-analytics") === "true";
if (onlySaveRatingToAnalytics) {
logGoogleEvent("Clicked initial button", rating ? "Yes" : "No");
} else {
document.getElementById("commentSubmit").disabled = true;
const postData = {
pageURL: window.location.href,
rating,
};
fetch(`${API_URL}/rating`, {
method: "POST",
headers: JSON_HEADER,
body: JSON.stringify(postData),
})
.then((response) => response.json())
.then((data) => {
if (data.message === "Success" && data.feedbackId != null) {
this.feedbackId = data.feedbackId;
logGoogleEvent("Clicked initial button", rating ? "Yes" : "No");
} else {
this.retryRating = true;
}
})
.catch((e) => {
this.retryRating = true;
})
.finally(() => {
document.getElementById("commentSubmit").disabled = false;
});
}
}
showElement(selector, displayType = "block") {
this.querySelector(selector).style.display = displayType;
}
hideElement(selector) {
this.querySelector(selector).style.display = "none";
}
getHTML() {
const content = LANG_TO_CONTENT[this.language];
const contactLink =
this.getAttribute("contact-link") ||
"https://www.nj.gov/nj/feedback.html";
const showCommentDisclaimer =
this.getAttribute("show-comment-disclaimer") !== "false";
const html = /*html*/ `
<div class="feedback-container">
<div id="ratingPrompt" class="flex-box">
<span class="feedback-text">${content.ratingPrompt}</span>
<div class="feedback-button-group">
<button id="yesButton" class="feedback-button">
${content.ratingPositive}
</button>
<button id="noButton" class="feedback-button">
${content.ratingNegative}
</button>
</div>
</div>
<div id="commentPrompt">
<form id="commentForm">
<div class="grid-box">
<div>
<label id="commentPromptText" for="comment" class="feedback-text"
>${content.commentPromptPositive}</label
>
${
showCommentDisclaimer
? `<p class="disclaimer-text">
${content.commentPromptDisclaimer}<a
target="_blank"
rel="noopener noreferrer"
href="${contactLink}"
>${content.commentPromptDisclaimerLink}</a
>.
</p>`
: ""
}
</div>
<div>
<textarea
type="text"
id="comment"
name="comment"
class="feedback-input"
required
></textarea>
<div id="commentSubmitError" class="error-text">
${content.errorMessage}
</div>
<button
id="commentSubmit"
class="feedback-button float-right submit-button"
type="submit"
>
${content.commentSubmit}
</button>
</div>
</div>
</form>
</div>
<div id="emailPrompt">
<form id="emailForm">
<div class="grid-box">
<div>
<div class="feedback-text">${content.commentConfirmation}</div>
<p class="disclaimer-text">${content.emailPrompt}</p>
</div>
<div>
<label for="email" class="email-label">${
content.emailLabel
}</label>
<input
type="email"
id="email"
name="email"
class="feedback-input email-input"
required
/>
<div id="emailSubmitError" class="error-text">
${content.errorMessage}
</div>
<button
id="emailSubmit"
class="feedback-button float-right submit-button"
type="submit"
>
${content.emailSubmit}
</button>
</div>
</div>
</form>
</div>
<div id="confirmation" class="confirmation">
<img
src="https://beta.nj.gov/files/feedback-widget/check_icon.svg"
alt=""
/>
<div class="confirmation-text">${
this.getAttribute("skip-email-step") === "true"
? content.commentConfirmation
: content.emailConfirmation
}</div>
</div>
</div>
`;
return html;
}
addStyling() {
const style = document.createElement("style");
style.textContent = /*css*/ `
.feedback-container {
background-color: #f0f0f0;
padding: 1.9rem 2.5rem;
}
.flex-box {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1.5rem;
}
.grid-box {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 1.5rem;
}
@media screen and (max-width: 765px) {
.flex-box {
justify-content: center;
}
.grid-box {
grid-template-columns: 1fr;
}
}
.feedback-text {
font-weight: 600;
font-size: 22px;
color: #1b1b1b;
}
.disclaimer-text {
margin: 1rem 0;
}
.email-label {
font-weight: 600;
margin: 0.5rem 0;
}
.email-input {
margin-bottom: 0.5rem;
}
.error-text {
font-size: 14px;
color: #D63E04;
margin-bottom: 0.75rem;
}
.submit-button {
margin-top: 0.75rem;
}
.confirmation {
display: flex;
align-items: center;
}
.confirmation-text {
font-weight: 600;
font-size: 22px;
margin-left: 1rem;
}
.feedback-button-group {
display: flex;
gap: 1.25rem;
flex-wrap: wrap;
}
.feedback-button {
font-family: inherit;
font-weight: 600;
font-size: 16px;
color: #1b1b1b !important;
background-color: #ffffff;
border-style: solid;
border-width: 2px;
border-color: #1b1b1b;
border-radius: 4px;
padding: 0.75rem 3rem;
cursor: pointer;
text-decoration: none;
text-align: center;
}
.feedback-button.float-right {
float: right;
}
.feedback-button:hover {
border-color: #3d4551;
background-color: #3d4551;
color: #ffffff !important;
text-decoration: none;
}
.feedback-button:disabled {
opacity: 0.65;
}
@media screen and (max-width: 450px) {
.feedback-button-group {
justify-content: center;
}
.feedback-button {
width: 100%;
}
}
.feedback-input {
width: 100%;
border: 1px solid #a9aeb1;
border-radius: 0px;
padding: 0.5rem;
}
.feedback-input:focus {
outline: none;
border-color: #86b7fe;
box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
}
#commentPrompt,
#emailPrompt,
#emailFormSubmitted,
#commentSubmitError,
#emailSubmitError,
#confirmation {
display: none;
}
`;
document.querySelector("head").appendChild(style);
}
}
window.customElements.define("feedback-widget", NJFeedbackWidget);