2021-09-08 05:26:18 -04:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import { FetchError, postJSON } from '../../infrastructure/fetch-json'
|
|
|
|
import { validateCaptchaV2 } from './captcha'
|
2021-09-15 04:42:56 -04:00
|
|
|
import inputValidator from './input-validator'
|
2021-09-27 06:45:04 -04:00
|
|
|
import { disableElement, enableElement } from '../utils/disableElement'
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
// Form helper(s) to handle:
|
|
|
|
// - Attaching to the relevant form elements
|
|
|
|
// - Listening for submit event
|
|
|
|
// - Validating captcha
|
|
|
|
// - Sending fetch request
|
|
|
|
// - Redirect handling
|
|
|
|
// - Showing errors
|
|
|
|
// - Disabled state
|
|
|
|
|
|
|
|
function formSubmitHelper(formEl) {
|
|
|
|
formEl.addEventListener('submit', async e => {
|
|
|
|
e.preventDefault()
|
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
formEl.dispatchEvent(new Event('pending'))
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
const messageBag = []
|
|
|
|
|
|
|
|
try {
|
|
|
|
const captchaResponse = await validateCaptcha(formEl)
|
|
|
|
|
|
|
|
const data = await sendFormRequest(formEl, captchaResponse)
|
2021-09-08 05:26:31 -04:00
|
|
|
formEl.dispatchEvent(new Event('sent'))
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
// Handle redirects
|
2021-09-08 05:26:18 -04:00
|
|
|
if (data.redir) {
|
|
|
|
window.location = data.redir
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show a success message (e.g. used on 2FA page)
|
|
|
|
if (data.message) {
|
|
|
|
messageBag.push({
|
|
|
|
type: 'message',
|
2021-09-23 05:48:42 -04:00
|
|
|
text: data.message.text || data.message,
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
let text = error.message
|
|
|
|
if (error instanceof FetchError) {
|
|
|
|
text = error.getUserFacingMessage()
|
|
|
|
}
|
|
|
|
messageBag.push({
|
|
|
|
type: 'error',
|
2021-09-13 04:13:07 -04:00
|
|
|
key: error.data?.message?.key,
|
2021-09-08 05:26:18 -04:00
|
|
|
text,
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
showMessages(formEl, messageBag)
|
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
formEl.dispatchEvent(new Event('idle'))
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
})
|
2021-09-16 04:14:24 -04:00
|
|
|
if (formEl.hasAttribute('data-ol-auto-submit')) {
|
|
|
|
setTimeout(() => {
|
|
|
|
formEl.querySelector('[type="submit"]').click()
|
|
|
|
}, 0)
|
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function validateCaptcha(formEl) {
|
|
|
|
let captchaResponse
|
|
|
|
if (formEl.hasAttribute('captcha')) {
|
|
|
|
captchaResponse = await validateCaptchaV2()
|
|
|
|
}
|
|
|
|
return captchaResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendFormRequest(formEl, captchaResponse) {
|
|
|
|
const formData = new FormData(formEl)
|
|
|
|
if (captchaResponse) {
|
|
|
|
formData.set('g-recaptcha-response', captchaResponse)
|
|
|
|
}
|
|
|
|
const body = Object.fromEntries(formData.entries())
|
|
|
|
const url = formEl.getAttribute('action')
|
|
|
|
return postJSON(url, { body })
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMessages(formEl, messageBag) {
|
|
|
|
const messagesEl = formEl.querySelector('[data-ol-form-messages]')
|
|
|
|
if (!messagesEl) return
|
|
|
|
|
|
|
|
// Clear content
|
|
|
|
messagesEl.textContent = ''
|
2021-09-13 04:13:07 -04:00
|
|
|
formEl.querySelectorAll('[data-ol-custom-form-message]').forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
// Render messages
|
|
|
|
messageBag.forEach(message => {
|
2021-09-13 04:13:07 -04:00
|
|
|
if (message.key) {
|
|
|
|
formEl
|
|
|
|
.querySelectorAll(`[data-ol-custom-form-message="${message.key}"]`)
|
|
|
|
.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:26:18 -04:00
|
|
|
const messageEl = document.createElement('div')
|
|
|
|
messageEl.className = classNames('alert', {
|
|
|
|
'alert-danger': message.type === 'error',
|
|
|
|
'alert-success': message.type !== 'error',
|
|
|
|
})
|
|
|
|
messageEl.textContent = message.text
|
2021-09-16 04:14:41 -04:00
|
|
|
messageEl.setAttribute('aria-live', 'assertive')
|
|
|
|
messageEl.setAttribute(
|
|
|
|
'role',
|
|
|
|
message.type === 'error' ? 'alert' : 'status'
|
|
|
|
)
|
2021-09-08 05:26:18 -04:00
|
|
|
messagesEl.append(messageEl)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:47:09 -04:00
|
|
|
export function inflightHelper(el) {
|
|
|
|
const disabledInflight = el.querySelectorAll('[data-ol-disabled-inflight]')
|
|
|
|
const showWhenNotInflight = el.querySelectorAll('[data-ol-inflight="idle"]')
|
|
|
|
const showWhenInflight = el.querySelectorAll('[data-ol-inflight="pending"]')
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
el.addEventListener('pending', () => {
|
2021-09-27 06:45:04 -04:00
|
|
|
disabledInflight.forEach(disableElement)
|
2021-09-21 09:47:09 -04:00
|
|
|
toggleDisplay(showWhenNotInflight, showWhenInflight)
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
el.addEventListener('idle', () => {
|
2021-09-27 06:45:04 -04:00
|
|
|
disabledInflight.forEach(enableElement)
|
2021-09-21 09:47:09 -04:00
|
|
|
toggleDisplay(showWhenInflight, showWhenNotInflight)
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
2021-09-08 05:26:31 -04:00
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-08 05:26:31 -04:00
|
|
|
function formSentHelper(el) {
|
2021-09-21 09:47:09 -04:00
|
|
|
const showWhenPending = el.querySelectorAll('[data-ol-not-sent]')
|
|
|
|
const showWhenDone = el.querySelectorAll('[data-ol-sent]')
|
|
|
|
if (showWhenDone.length === 0) return
|
2021-09-08 05:26:31 -04:00
|
|
|
|
|
|
|
el.addEventListener('sent', () => {
|
|
|
|
toggleDisplay(showWhenPending, showWhenDone)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function formValidationHelper(el) {
|
|
|
|
el.querySelectorAll('input').forEach(inputEl => {
|
|
|
|
if (
|
|
|
|
inputEl.willValidate &&
|
|
|
|
!inputEl.hasAttribute('data-ol-no-custom-form-validation-messages')
|
|
|
|
) {
|
|
|
|
inputValidator(inputEl)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-23 05:48:09 -04:00
|
|
|
export function toggleDisplay(hide, show) {
|
2021-09-21 09:47:09 -04:00
|
|
|
hide.forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
|
|
|
show.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function hydrateAsyncForm(el) {
|
2021-09-08 05:26:18 -04:00
|
|
|
formSubmitHelper(el)
|
2021-09-21 09:47:09 -04:00
|
|
|
inflightHelper(el)
|
2021-09-08 05:26:31 -04:00
|
|
|
formSentHelper(el)
|
2021-09-27 06:45:04 -04:00
|
|
|
formValidationHelper(el)
|
|
|
|
}
|
2021-09-15 04:42:56 -04:00
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function hydrateRegularForm(el) {
|
|
|
|
inflightHelper(el)
|
|
|
|
formValidationHelper(el)
|
|
|
|
|
|
|
|
el.addEventListener('submit', () => {
|
|
|
|
el.dispatchEvent(new Event('pending'))
|
2021-09-15 04:42:56 -04:00
|
|
|
})
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
document.querySelectorAll(`[data-ol-async-form]`).forEach(hydrateAsyncForm)
|
|
|
|
|
|
|
|
document.querySelectorAll(`[data-ol-regular-form]`).forEach(hydrateRegularForm)
|