11import { sendEvent } from './events'
22
3+ function showElement ( el ) {
4+ el . removeAttribute ( 'hidden' )
5+ }
6+
7+ function hideElement ( el ) {
8+ el . setAttribute ( 'hidden' , true )
9+ }
10+
11+ export function updateDisplay ( form , state ) {
12+ Array . from (
13+ form . querySelectorAll (
14+ [ 'start' , 'yes' , 'no' , 'end' ]
15+ . map ( xstate => '[data-help-' + xstate + ']' )
16+ . join ( ',' )
17+ )
18+ )
19+ . forEach ( hideElement )
20+ Array . from ( form . querySelectorAll ( '[data-help-' + state + ']' ) )
21+ . forEach ( showElement )
22+ }
23+
24+ export function submitForm ( form ) {
25+ const formData = new FormData ( form )
26+ const data = Object . fromEntries (
27+ Array . from ( formData . entries ( ) )
28+ . map (
29+ ( [ key , value ] ) => [
30+ key . replace ( 'helpfulness-' , '' ) ,
31+ value || undefined // Convert empty strings to undefined
32+ ]
33+ )
34+ )
35+ return trackEvent ( data )
36+ }
37+
38+ function trackEvent ( { token, vote, email, comment } ) {
39+ return sendEvent ( {
40+ type : 'survey' ,
41+ token, // Honeypot
42+ survey_vote : vote === 'Yes' ,
43+ survey_comment : comment ,
44+ survey_email : email
45+ } )
46+ }
47+
348export default function helpfulness ( ) {
449 const form = document . querySelector ( '.js-helpfulness' )
550 const texts = Array . from ( document . querySelectorAll ( '.js-helpfulness input, .js-helpfulness textarea' ) )
651 const votes = Array . from ( document . querySelectorAll ( '.js-helpfulness [type=radio]' ) )
752 if ( ! form || ! texts . length || ! votes . length ) return
853
9- form . addEventListener ( 'submit' , async evt => {
54+ form . addEventListener ( 'submit' , evt => {
1055 evt . preventDefault ( )
11- await submitForm ( evt . target )
56+ submitForm ( form )
1257 updateDisplay ( form , 'end' )
1358 } )
1459
1560 votes . forEach ( voteEl => {
16- voteEl . addEventListener ( 'change' , async evt => {
61+ voteEl . addEventListener ( 'change' , evt => {
1762 const state = evt . target . value . toLowerCase ( )
1863 const form = voteEl . closest ( 'form' )
19- await submitForm ( form )
64+ submitForm ( form )
2065 updateDisplay ( form , state )
2166 } )
2267 } )
@@ -27,62 +72,4 @@ export default function helpfulness () {
2772 if ( evt . code === 'Slash' ) evt . stopPropagation ( )
2873 } )
2974 } )
30-
31- function showElement ( el ) {
32- el . removeAttribute ( 'hidden' )
33- }
34-
35- function hideElement ( el ) {
36- el . setAttribute ( 'hidden' , true )
37- }
38-
39- function isRequired ( el ) {
40- el . setAttribute ( 'required' , true )
41- }
42-
43- function notRequired ( el ) {
44- el . removeAttribute ( 'required' )
45- }
46-
47- function updateDisplay ( form , state ) {
48- Array . from (
49- form . querySelectorAll (
50- [ 'start' , 'yes' , 'no' , 'end' ]
51- . map ( xstate => '[data-help-' + xstate + ']' )
52- . join ( ',' )
53- )
54- )
55- . forEach ( hideElement )
56- Array . from ( form . querySelectorAll ( '[data-help-' + state + ']' ) )
57- . forEach ( showElement )
58- if ( state === 'no' ) {
59- isRequired ( form . querySelector ( 'select' ) )
60- } else {
61- notRequired ( form . querySelector ( 'select' ) )
62- }
63- }
64-
65- async function submitForm ( form ) {
66- const formData = new FormData ( form )
67- const data = Object . fromEntries (
68- Array . from ( formData . entries ( ) )
69- . map (
70- ( [ key , value ] ) => [
71- key . replace ( 'helpfulness-' , '' ) ,
72- value || undefined // Convert empty strings to undefined
73- ]
74- )
75- )
76- return trackEvent ( data )
77- }
78-
79- async function trackEvent ( { token, vote, email, comment } ) {
80- return sendEvent ( {
81- type : 'survey' ,
82- token, // Honeypot
83- survey_vote : vote === 'Yes' ,
84- survey_comment : comment ,
85- survey_email : email
86- } )
87- }
8875}
0 commit comments