-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedback-widget.min.js
228 lines (206 loc) · 12.1 KB
/
feedback-widget.min.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
function logGoogleEvent(action,label=void 0){"function"==typeof window.gtag?window.gtag("event",action,{event_category:"Page feedback",event_label:label}):null!=window.dataLayer&&window.dataLayer.push({event:"click_feedback_rating",object_type:label})}let 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!"}},API_URL="https://innovation.nj.gov/app/feedback/dev",JSON_HEADER={"Content-Type":"application/json"};class NJFeedbackWidget extends window.HTMLElement{constructor(){super(),this.rating=!1,this.feedbackId=void 0,this.retryRating=!1,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(!0)}),this.querySelector("#noButton").addEventListener("click",_e=>{this.handleRating(!1)}),this.querySelector("#commentForm").addEventListener("submit",e=>{e.preventDefault();let submitButton=document.getElementById("commentSubmit");submitButton.disabled=!0,submitButton.textContent=LANG_TO_CONTENT[this.language].commentSubmitLoading,this.hideElement("#commentSubmitError");e=e.target.elements.comment.value,e=this.retryRating||null==this.feedbackId?{comment:e,rating:this.rating,pageURL:window.location.href}:{feedbackId:this.feedbackId,comment:e};fetch(API_URL+"/comment",{method:"POST",headers:JSON_HEADER,body:JSON.stringify(e)}).then(response=>response.json()).then(data=>{null==this.feedbackId&&(this.feedbackId=data.feedbackId),"Success"===data.message&&null!=this.feedbackId?(this.hideElement("#commentPrompt"),"true"===this.getAttribute("skip-email-step")?this.showElement("#confirmation","flex"):this.showElement("#emailPrompt")):this.showElement("#commentSubmitError")}).catch(e=>{this.showElement("#commentSubmitError")}).finally(()=>{submitButton.disabled=!1,submitButton.textContent=LANG_TO_CONTENT[this.language].commentSubmit})}),this.querySelector("#emailForm").addEventListener("submit",e=>{e.preventDefault();let submitButton=document.getElementById("emailSubmit");submitButton.disabled=!0,submitButton.textContent=LANG_TO_CONTENT[this.language].emailSubmitLoading,this.hideElement("#emailSubmitError");e={feedbackId:this.feedbackId,email:e.target.elements.email.value};fetch(API_URL+"/email",{method:"POST",headers:JSON_HEADER,body:JSON.stringify(e)}).then(response=>response.json()).then(data=>{"Success"===data.message&&null!=data.feedbackId?(this.hideElement("#emailPrompt"),this.showElement("#confirmation","flex")):this.showElement("#emailSubmitError")}).catch(e=>{this.showElement("#emailSubmitError")}).finally(()=>{submitButton.disabled=!1,submitButton.textContent=LANG_TO_CONTENT[this.language].emailSubmit})})}handleRating(rating){var postData;(this.rating=rating)||(this.querySelector("#commentPromptText").innerText=LANG_TO_CONTENT[this.language].commentPromptNegative),this.hideElement("#ratingPrompt"),this.showElement("#commentPrompt"),"true"===this.getAttribute("only-save-rating-to-analytics")?logGoogleEvent("Clicked initial button",rating?"Yes":"No"):(document.getElementById("commentSubmit").disabled=!0,postData={pageURL:window.location.href,rating:rating},fetch(API_URL+"/rating",{method:"POST",headers:JSON_HEADER,body:JSON.stringify(postData)}).then(response=>response.json()).then(data=>{"Success"===data.message&&null!=data.feedbackId?(this.feedbackId=data.feedbackId,logGoogleEvent("Clicked initial button",rating?"Yes":"No")):this.retryRating=!0}).catch(e=>{this.retryRating=!0}).finally(()=>{document.getElementById("commentSubmit").disabled=!1}))}showElement(selector,displayType="block"){this.querySelector(selector).style.display=displayType}hideElement(selector){this.querySelector(selector).style.display="none"}getHTML(){var content=LANG_TO_CONTENT[this.language],contactLink=this.getAttribute("contact-link")||"https://www.nj.gov/nj/feedback.html",showCommentDisclaimer="false"!==this.getAttribute("show-comment-disclaimer");return`
<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">${"true"===this.getAttribute("skip-email-step")?content.commentConfirmation:content.emailConfirmation}</div>
</div>
</div>
`}addStyling(){var style=document.createElement("style");style.textContent=`
.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);