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:
66
index3.js
66
index3.js
@@ -138,6 +138,55 @@ const mobileToggle = document.getElementById('mobileToggle')
|
||||
})
|
||||
}
|
||||
|
||||
const submitLeadForm = (formElement, successElement, formType, onSuccess) => {
|
||||
if (!formElement || !successElement) {
|
||||
return
|
||||
}
|
||||
|
||||
formElement.addEventListener('submit', async event => {
|
||||
event.preventDefault()
|
||||
|
||||
const leadFormConfig = window.test1LeadForm || null
|
||||
|
||||
if (!leadFormConfig?.ajaxUrl || !leadFormConfig?.action || !leadFormConfig?.nonce) {
|
||||
window.alert('Форма временно недоступна. Обновите страницу и попробуйте снова.')
|
||||
return
|
||||
}
|
||||
|
||||
const formData = new window.FormData(formElement)
|
||||
formData.append('action', leadFormConfig.action)
|
||||
formData.append('nonce', leadFormConfig.nonce)
|
||||
formData.append('form_type', formType)
|
||||
formData.append('page_url', window.location.href)
|
||||
|
||||
try {
|
||||
const response = await window.fetch(leadFormConfig.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')
|
||||
}
|
||||
|
||||
successElement.classList.add('visible')
|
||||
if (typeof onSuccess === 'function') {
|
||||
onSuccess()
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
formElement.reset()
|
||||
successElement.classList.remove('visible')
|
||||
}, 2200)
|
||||
} catch (error) {
|
||||
window.alert('Не удалось отправить заявку. Попробуйте еще раз.')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
mobileToggle.addEventListener('click', () => {
|
||||
const isOpen = mobilePanel.classList.contains('active')
|
||||
if (isOpen) {
|
||||
@@ -272,26 +321,15 @@ const mobileToggle = document.getElementById('mobileToggle')
|
||||
})
|
||||
})
|
||||
|
||||
orderForm.addEventListener('submit', event => {
|
||||
event.preventDefault()
|
||||
orderSuccess.classList.add('visible')
|
||||
submitLeadForm(orderForm, orderSuccess, 'landing_order', () => {
|
||||
cart = []
|
||||
renderCart()
|
||||
setTimeout(() => {
|
||||
closeModal()
|
||||
orderForm.reset()
|
||||
orderSuccess.classList.remove('visible')
|
||||
}, 1800)
|
||||
}, 700)
|
||||
})
|
||||
|
||||
contactForm.addEventListener('submit', event => {
|
||||
event.preventDefault()
|
||||
contactSuccess.classList.add('visible')
|
||||
setTimeout(() => {
|
||||
contactForm.reset()
|
||||
contactSuccess.classList.remove('visible')
|
||||
}, 2200)
|
||||
})
|
||||
submitLeadForm(contactForm, contactSuccess, 'landing_contacts')
|
||||
|
||||
document.addEventListener('keydown', event => {
|
||||
if (event.key !== 'Escape') {
|
||||
|
||||
Reference in New Issue
Block a user