send telegram
This commit is contained in:
@@ -1,18 +1,46 @@
|
||||
(() => {
|
||||
const contactForm = document.getElementById('contactForm')
|
||||
const contactSuccess = document.getElementById('contactSuccess')
|
||||
const formConfig = window.test1ContactsForm || null
|
||||
|
||||
if (!contactForm || !contactSuccess) {
|
||||
return
|
||||
}
|
||||
|
||||
contactForm.addEventListener('submit', event => {
|
||||
contactForm.addEventListener('submit', async event => {
|
||||
event.preventDefault()
|
||||
contactSuccess.classList.add('visible')
|
||||
|
||||
setTimeout(() => {
|
||||
contactForm.reset()
|
||||
contactSuccess.classList.remove('visible')
|
||||
}, 2200)
|
||||
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')
|
||||
|
||||
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