* Update index to make it clear what has hasn't updated to Next/React yet * Typescriptify events and experiments * Typescript the old JS for easier integration * Update release-notes.ts * Lint * Run npm i * Fix a few lint issues * Update airgap-links.ts * Update airgap-links.ts * Update set-next-env to ts * Update airgap-links.ts * Update package-lock.json * Update set-next-env.ts * Update package-lock.json * Revert "Update package-lock.json" This reverts commit b45e8250beeb700719d3b44e1092b0bbd093baba. * readd fsevents * Revert "readd fsevents" This reverts commit 419f3c35080ac4a9072f0b4e8e291e1712ce3639. * Update openapi-schema-check.yml * Revert "Update openapi-schema-check.yml" This reverts commit 5e9f4a29ea11ee343ca17291a40a751920c5b923. * Update package-lock.json * Update package-lock.json
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { sendEvent, EventType } from './events'
|
|
|
|
function showElement(el: HTMLElement) {
|
|
el.removeAttribute('hidden')
|
|
}
|
|
|
|
function hideElement(el: HTMLElement) {
|
|
el.setAttribute('hidden', 'hidden')
|
|
}
|
|
|
|
function updateDisplay(form: HTMLFormElement, state: string) {
|
|
const allSelector = ['start', 'yes', 'no', 'end']
|
|
.map((xstate) => '[data-help-' + xstate + ']')
|
|
.join(',')
|
|
const stateSelector = '[data-help-' + state + ']'
|
|
const allEls = Array.from(form.querySelectorAll(allSelector)) as Array<HTMLElement>
|
|
allEls.forEach(hideElement)
|
|
const stateEls = Array.from(form.querySelectorAll(stateSelector)) as Array<HTMLElement>
|
|
stateEls.forEach(showElement)
|
|
}
|
|
|
|
function submitForm(form: HTMLFormElement) {
|
|
const formData = new FormData(form)
|
|
return trackEvent(formData)
|
|
}
|
|
|
|
function trackEvent(formData: FormData) {
|
|
return sendEvent({
|
|
type: EventType.survey,
|
|
survey_token: (formData.get('survey-token') as string) || undefined, // Honeypot
|
|
survey_vote: formData.get('survey-vote') === 'Yes',
|
|
survey_comment: (formData.get('survey-comment') as string) || undefined,
|
|
survey_email: (formData.get('survey-email') as string) || undefined,
|
|
})
|
|
}
|
|
|
|
export default function survey() {
|
|
// @ts-ignore
|
|
if (window.IS_NEXTJS_PAGE) return
|
|
|
|
const form = document.querySelector('.js-survey') as HTMLFormElement | null
|
|
const texts = Array.from(
|
|
document.querySelectorAll('.js-survey input, .js-survey textarea')
|
|
) as Array<HTMLElement>
|
|
const votes = Array.from(document.querySelectorAll('.js-survey [type=radio]'))
|
|
if (!form || !texts.length || !votes.length) return
|
|
|
|
form.addEventListener('submit', (evt) => {
|
|
evt.preventDefault()
|
|
submitForm(form)
|
|
updateDisplay(form, 'end')
|
|
})
|
|
|
|
votes.forEach((voteEl) => {
|
|
voteEl.addEventListener('change', (evt) => {
|
|
const radio = evt.target as HTMLInputElement
|
|
const state = radio.value.toLowerCase()
|
|
submitForm(form)
|
|
updateDisplay(form, state)
|
|
})
|
|
})
|
|
|
|
// Prevent the site search from overtaking your input
|
|
texts.forEach((text) => {
|
|
text.addEventListener('keydown', (evt: KeyboardEvent) => {
|
|
if (evt.code === 'Slash') evt.stopPropagation()
|
|
})
|
|
})
|
|
}
|