Task_12281 (#1)
Co-authored-by: User A0264400 <a0264400@jarl.from.sh> Reviewed-on: #1 Co-authored-by: Anton.AE <ab.an.ev@yandex.ru> Co-committed-by: Anton.AE <ab.an.ev@yandex.ru>
This commit is contained in:
@@ -6,13 +6,43 @@
|
||||
return
|
||||
}
|
||||
|
||||
contactForm.addEventListener('submit', event => {
|
||||
contactForm.addEventListener('submit', async event => {
|
||||
event.preventDefault()
|
||||
contactSuccess.classList.add('visible')
|
||||
|
||||
setTimeout(() => {
|
||||
contactForm.reset()
|
||||
contactSuccess.classList.remove('visible')
|
||||
}, 2200)
|
||||
const formConfig = window.test1ContactsForm || null
|
||||
|
||||
if (!formConfig?.ajaxUrl || !formConfig?.action || !formConfig?.nonce) {
|
||||
window.alert('Форма временно недоступна. Обновите страницу и попробуйте снова.')
|
||||
return
|
||||
}
|
||||
|
||||
const formData = new window.FormData(contactForm)
|
||||
formData.append('action', formConfig.action)
|
||||
formData.append('nonce', formConfig.nonce)
|
||||
formData.append('form_type', 'contacts_page')
|
||||
formData.append('page_url', window.location.href)
|
||||
|
||||
try {
|
||||
const response = await window.fetch(formConfig.ajaxUrl, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
const payload = await response.json().catch(() => null)
|
||||
|
||||
if (!response.ok || !payload?.success) {
|
||||
throw new Error('submit_failed')
|
||||
}
|
||||
|
||||
contactSuccess.classList.add('visible')
|
||||
|
||||
setTimeout(() => {
|
||||
contactForm.reset()
|
||||
contactSuccess.classList.remove('visible')
|
||||
}, 2200)
|
||||
} catch (error) {
|
||||
window.alert('Не удалось отправить заявку. Попробуйте еще раз.')
|
||||
}
|
||||
})
|
||||
})()
|
||||
|
||||
Reference in New Issue
Block a user