2021-09-08 05:26:18 -04:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import { FetchError, postJSON } from '../../infrastructure/fetch-json'
|
2022-01-26 06:15:19 -05:00
|
|
|
import { canSkipCaptcha, 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 {
|
2022-01-26 06:15:19 -05:00
|
|
|
let data
|
|
|
|
try {
|
|
|
|
const captchaResponse = await validateCaptcha(formEl)
|
|
|
|
data = await sendFormRequest(formEl, captchaResponse)
|
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
e instanceof FetchError &&
|
|
|
|
e.data?.errorReason === 'cannot_verify_user_not_robot'
|
|
|
|
) {
|
|
|
|
// Trigger captcha unconditionally.
|
|
|
|
const captchaResponse = await validateCaptchaV2()
|
2022-01-26 07:56:37 -05:00
|
|
|
if (!captchaResponse) {
|
|
|
|
throw e
|
|
|
|
}
|
2022-01-26 06:15:19 -05:00
|
|
|
data = await sendFormRequest(formEl, captchaResponse)
|
|
|
|
} else {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
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
|
2022-07-28 05:20:44 -04:00
|
|
|
if (data.redir || data.redirect) {
|
|
|
|
window.location = data.redir || data.redirect
|
2021-09-08 05:26:18 -04:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
2022-01-25 05:14:17 -05:00
|
|
|
|
2022-04-27 04:42:39 -04:00
|
|
|
// Handle reloads
|
|
|
|
if (formEl.hasAttribute('data-ol-reload-on-success')) {
|
|
|
|
window.setTimeout(window.location.reload.bind(window.location), 1000)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:14:17 -05:00
|
|
|
// Let the user re-submit the form.
|
|
|
|
formEl.dispatchEvent(new Event('idle'))
|
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,
|
2022-05-16 04:02:32 -04:00
|
|
|
hints: error.data?.message?.hints,
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
|
2022-01-25 05:14:17 -05:00
|
|
|
// Let the user re-submit the form.
|
2021-09-16 04:45:19 -04:00
|
|
|
formEl.dispatchEvent(new Event('idle'))
|
2022-01-25 05:14:17 -05:00
|
|
|
} finally {
|
2024-04-04 11:24:19 -04:00
|
|
|
// call old and new notification builder functions
|
|
|
|
// but only one will be rendered
|
2022-01-25 05:14:17 -05:00
|
|
|
showMessages(formEl, messageBag)
|
2024-04-04 11:24:19 -04:00
|
|
|
showMessagesNewStyle(formEl, messageBag)
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateCaptcha(formEl) {
|
|
|
|
let captchaResponse
|
|
|
|
if (formEl.hasAttribute('captcha')) {
|
2022-01-26 06:15:19 -05:00
|
|
|
if (
|
|
|
|
formEl.getAttribute('action') === '/login' &&
|
|
|
|
(await canSkipCaptcha(new FormData(formEl).get('email')))
|
|
|
|
) {
|
|
|
|
// The email is present in the deviceHistory, and we can skip the display
|
|
|
|
// of a captcha challenge.
|
|
|
|
// The actual login POST request will be checked against the deviceHistory
|
|
|
|
// again and the server can trigger the display of a captcha if needed by
|
|
|
|
// sending a 400 with errorReason set to 'cannot_verify_user_not_robot'.
|
|
|
|
return ''
|
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
captchaResponse = await validateCaptchaV2()
|
|
|
|
}
|
|
|
|
return captchaResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendFormRequest(formEl, captchaResponse) {
|
|
|
|
const formData = new FormData(formEl)
|
|
|
|
if (captchaResponse) {
|
|
|
|
formData.set('g-recaptcha-response', captchaResponse)
|
|
|
|
}
|
2022-07-28 10:11:20 -04:00
|
|
|
const body = Object.fromEntries(
|
|
|
|
Array.from(formData.keys(), key => {
|
|
|
|
// forms may have multiple keys with the same name, eg: checkboxes
|
|
|
|
const val = formData.getAll(key)
|
|
|
|
return [key, val.length > 1 ? val : val.pop()]
|
|
|
|
})
|
|
|
|
)
|
2021-09-08 05:26:18 -04:00
|
|
|
const url = formEl.getAttribute('action')
|
|
|
|
return postJSON(url, { body })
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:24:17 -04:00
|
|
|
function hideFormElements(formEl) {
|
|
|
|
for (const e of formEl.elements) {
|
|
|
|
e.hidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 11:24:19 -04:00
|
|
|
// TODO: remove the showMessages function after every form alerts are updated to use the new style
|
|
|
|
// TODO: rename showMessagesNewStyle to showMessages after the above is done
|
2021-09-08 05:26:18 -04:00
|
|
|
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 => {
|
2023-06-13 06:16:14 -04:00
|
|
|
const customErrorElements = message.key
|
|
|
|
? formEl.querySelectorAll(
|
|
|
|
`[data-ol-custom-form-message="${message.key}"]`
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
if (message.key && customErrorElements.length > 0) {
|
|
|
|
// Found at least one custom error element for key, show them
|
|
|
|
customErrorElements.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// No custom error element for key on page, append a new error message
|
|
|
|
const messageEl = document.createElement('div')
|
|
|
|
messageEl.className = classNames('alert mb-2', {
|
|
|
|
'alert-danger': message.type === 'error',
|
|
|
|
'alert-success': message.type !== 'error',
|
|
|
|
})
|
|
|
|
messageEl.textContent = message.text || `Error: ${message.key}`
|
|
|
|
messageEl.setAttribute('aria-live', 'assertive')
|
|
|
|
messageEl.setAttribute(
|
|
|
|
'role',
|
|
|
|
message.type === 'error' ? 'alert' : 'status'
|
|
|
|
)
|
|
|
|
if (message.hints && message.hints.length) {
|
|
|
|
const listEl = document.createElement('ul')
|
|
|
|
message.hints.forEach(hint => {
|
|
|
|
const listItemEl = document.createElement('li')
|
|
|
|
listItemEl.textContent = hint
|
|
|
|
listEl.append(listItemEl)
|
2021-09-13 04:13:07 -04:00
|
|
|
})
|
2023-06-13 06:16:14 -04:00
|
|
|
messageEl.append(listEl)
|
|
|
|
}
|
|
|
|
messagesEl.append(messageEl)
|
|
|
|
}
|
|
|
|
if (message.key) {
|
2022-09-27 07:24:17 -04:00
|
|
|
// Hide the form elements on specific message types
|
|
|
|
const hideOnError = formEl.attributes['data-ol-hide-on-error']
|
|
|
|
if (
|
|
|
|
hideOnError &&
|
|
|
|
hideOnError.value &&
|
|
|
|
hideOnError.value.match(message.key)
|
|
|
|
) {
|
|
|
|
hideFormElements(formEl)
|
|
|
|
}
|
2022-12-06 05:22:20 -05:00
|
|
|
// Hide any elements with specific `data-ol-hide-on-error-message` message
|
|
|
|
document
|
|
|
|
.querySelectorAll(`[data-ol-hide-on-error-message="${message.key}"]`)
|
2024-04-04 11:24:19 -04:00
|
|
|
.forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMessagesNewStyle(formEl, messageBag) {
|
|
|
|
const messagesEl = formEl.querySelector('[data-ol-form-messages-new-style]')
|
|
|
|
if (!messagesEl) return
|
|
|
|
|
|
|
|
// Clear content
|
|
|
|
messagesEl.textContent = ''
|
|
|
|
formEl.querySelectorAll('[data-ol-custom-form-message]').forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
|
|
|
|
|
|
|
// Render messages
|
|
|
|
messageBag.forEach(message => {
|
|
|
|
const customErrorElements = message.key
|
|
|
|
? formEl.querySelectorAll(
|
|
|
|
`[data-ol-custom-form-message="${message.key}"]`
|
|
|
|
)
|
|
|
|
: []
|
|
|
|
if (message.key && customErrorElements.length > 0) {
|
|
|
|
// Found at least one custom error element for key, show them
|
|
|
|
customErrorElements.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// No custom error element for key on page, append a new error message
|
|
|
|
const messageElContainer = document.createElement('div')
|
2024-04-11 08:36:56 -04:00
|
|
|
messageElContainer.className = classNames('notification', {
|
|
|
|
'notification-type-error': message.type === 'error',
|
|
|
|
'notification-type-success': message.type !== 'error',
|
|
|
|
})
|
2024-04-04 11:24:19 -04:00
|
|
|
const messageEl = document.createElement('div')
|
|
|
|
|
2024-04-11 08:36:56 -04:00
|
|
|
// create the message text
|
|
|
|
messageEl.className = 'notification-content text-left'
|
2024-04-04 11:24:19 -04:00
|
|
|
messageEl.textContent = message.text || `Error: ${message.key}`
|
|
|
|
messageEl.setAttribute('aria-live', 'assertive')
|
|
|
|
messageEl.setAttribute(
|
|
|
|
'role',
|
|
|
|
message.type === 'error' ? 'alert' : 'status'
|
|
|
|
)
|
|
|
|
if (message.hints && message.hints.length) {
|
|
|
|
const listEl = document.createElement('ul')
|
|
|
|
message.hints.forEach(hint => {
|
|
|
|
const listItemEl = document.createElement('li')
|
|
|
|
listItemEl.textContent = hint
|
|
|
|
listEl.append(listItemEl)
|
|
|
|
})
|
|
|
|
messageEl.append(listEl)
|
|
|
|
}
|
|
|
|
|
2024-04-11 08:36:56 -04:00
|
|
|
// create the left icon
|
|
|
|
const icon = document.createElement('span')
|
|
|
|
icon.className = 'material-symbols'
|
|
|
|
icon.setAttribute('aria-hidden', 'true')
|
|
|
|
icon.innerText = message.type === 'error' ? 'error' : 'check_circle'
|
|
|
|
const messageIcon = document.createElement('div')
|
|
|
|
messageIcon.className = 'notification-icon'
|
|
|
|
messageIcon.appendChild(icon)
|
|
|
|
|
|
|
|
// append icon first so it's on the left
|
|
|
|
messageElContainer.appendChild(messageIcon)
|
2024-04-04 11:24:19 -04:00
|
|
|
messageElContainer.appendChild(messageEl)
|
|
|
|
messagesEl.append(messageElContainer)
|
|
|
|
}
|
|
|
|
if (message.key) {
|
|
|
|
// Hide the form elements on specific message types
|
|
|
|
const hideOnError = formEl.attributes['data-ol-hide-on-error']
|
|
|
|
if (
|
|
|
|
hideOnError &&
|
|
|
|
hideOnError.value &&
|
|
|
|
hideOnError.value.match(message.key)
|
|
|
|
) {
|
|
|
|
hideFormElements(formEl)
|
|
|
|
}
|
|
|
|
// Hide any elements with specific `data-ol-hide-on-error-message` message
|
|
|
|
document
|
|
|
|
.querySelectorAll(`[data-ol-hide-on-error-message="${message.key}"]`)
|
2022-12-06 05:22:20 -05:00
|
|
|
.forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
2022-05-16 04:02:32 -04:00
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-29 05:04:58 -04:00
|
|
|
function formAutoSubmitHelper(el) {
|
|
|
|
if (el.hasAttribute('data-ol-auto-submit')) {
|
|
|
|
setTimeout(() => {
|
|
|
|
el.querySelector('[type="submit"]').click()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-29 05:04:58 -04:00
|
|
|
formAutoSubmitHelper(el)
|
2021-09-27 06:45:04 -04:00
|
|
|
}
|
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-29 05:04:58 -04:00
|
|
|
|
|
|
|
formAutoSubmitHelper(el)
|
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)
|