send telegram
This commit is contained in:
64
index3.js
64
index3.js
@@ -22,6 +22,7 @@ const mobileToggle = document.getElementById('mobileToggle')
|
||||
const orderSuccess = document.getElementById('orderSuccess')
|
||||
const contactSuccess = document.getElementById('contactSuccess')
|
||||
const wooCommerceConfig = window.test1WooCommerce || null
|
||||
const leadFormConfig = window.test1LeadForm || null
|
||||
const cartPageUrl = (wooCommerceConfig && wooCommerceConfig.cartUrl) ? wooCommerceConfig.cartUrl : '/cart/'
|
||||
const checkoutPageUrl = (wooCommerceConfig && wooCommerceConfig.checkoutUrl) ? wooCommerceConfig.checkoutUrl : '/checkout/'
|
||||
const cartPanel = cartDrawer?.querySelector('.cart-panel')
|
||||
@@ -138,6 +139,52 @@ const mobileToggle = document.getElementById('mobileToggle')
|
||||
})
|
||||
}
|
||||
|
||||
const submitLeadForm = (formElement, successElement, formType, onSuccess) => {
|
||||
if (!formElement || !successElement) {
|
||||
return
|
||||
}
|
||||
|
||||
formElement.addEventListener('submit', async event => {
|
||||
event.preventDefault()
|
||||
|
||||
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)
|
||||
|
||||
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 +319,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, 'contacts')
|
||||
|
||||
document.addEventListener('keydown', event => {
|
||||
if (event.key !== 'Escape') {
|
||||
|
||||
Reference in New Issue
Block a user