-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
55 lines (35 loc) · 1.44 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const MODAL_ACTIVE_CLASS = 'modal-active';
const callFormBtn = document.querySelector('#call-form-btn');
const modalForm = document.querySelector('#modal-form');
const modalSuccess = document.querySelector('#modal-success');
const form = document.querySelector('#modal-form form');
function closeModals(e) {
e.preventDefault();
modalForm.classList.remove(MODAL_ACTIVE_CLASS);
modalSuccess.classList.remove(MODAL_ACTIVE_CLASS);
document.body.classList.remove('body-fixed');
}
function openSuccessModal() {
modalForm.classList.remove(MODAL_ACTIVE_CLASS);
modalSuccess.classList.add(MODAL_ACTIVE_CLASS);
document.body.classList.add('body-fixed');
const modalFormClose = document.querySelector('#modal-success-close');
modalFormClose.addEventListener('click', closeModals);
};
function sendUserInfo(e) {
e.preventDefault()
const formData = new FormData(form);
// openSuccessModal();
fetch('/', {
method: 'POST',
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(formData).toString()
}).then(openSuccessModal);
}
callFormBtn.addEventListener('click', function () {
modalForm.classList.add(MODAL_ACTIVE_CLASS);
const modalFormClose = document.querySelector('#modal-form-close');
modalFormClose.addEventListener('click', closeModals);
form.addEventListener('submit', sendUserInfo);
document.body.classList.add('body-fixed');
});