/
emailform.js
181 lines (143 loc) · 5.43 KB
/
emailform.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
$(function () {
'use strict';
const formDataElements = {};
const inputs = document.querySelectorAll('.form-data');
inputs.forEach((el) => {
formDataElements[el.getAttribute('name')] = el;
});
const recaptcha = document.querySelector('.g-recaptcha');
const customErrors = {
tooShort: (fieldName, min) => 'Pole ' + fieldName + ' musi zawierać co najmniej ' + min + ' znaki.',
tooLong: (fieldName, max) => 'Pole ' + fieldName + ' może zawierać co najwyżej ' + max + ' znaków.',
empty: fieldName => 'Pole ' + fieldName + ' nie może być puste.',
type: fieldName => 'Pole ' + fieldName + ' jest niepoprawne.',
errorsInForm: 'W formularzu występują błędy.',
recaptcha: 'Potwierdź, że nie jesteś robotem.',
};
const contactForm = $('#formularz-kontaktowy');
const formAlert = document.querySelector('.emailFormAlert');
function toggleContactForm(state) {
if (typeof state !== 'boolean') throw new TypeError('State must be a boolean');
if (state === true) {
contactForm.fadeIn();
contactForm.attr('aria-hidden', 'false');
firstInput.focus();
} else {
contactForm.fadeOut();
contactForm.attr('aria-hidden', 'true');
for (const input in formDataElements) {
formDataElements[input].value = '';
}
grecaptcha.reset();
openContactBtn.focus();
}
}
const closeContactBtn = $('#close-contact-btn');
const openContactBtn = $('#open-contact-btn');
openContactBtn.click(() => {
toggleContactForm(true);
});
closeContactBtn.click(() => {
toggleContactForm(false);
});
const firstInput = $('.form-data:first');
if (document.location.hash === '#formularz-kontaktowy') {
openContactBtn.focus();
openContactBtn.click();
}
closeContactBtn.on('keydown', (e) => {
if ((e.which === 9 && !e.shiftKey)) { // tab
e.preventDefault();
firstInput.focus();
}
});
firstInput.on('keydown', (e) => {
if ((e.which === 9 && e.shiftKey)) { // tab
e.preventDefault();
closeContactBtn.focus();
}
});
contactForm.on('keydown', (e) => {
if ((e.which === 27)) { // esc
toggleContactForm(false);
}
});
$('.emailFormSubmit').click((event) => {
formAlert.innerHTML = '<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>';
event.preventDefault();
const isValid = validateEmailForm();
if (isValid === true) {
const formData = {
'g-recaptcha-response': grecaptcha.getResponse(),
};
grecaptcha.reset();
for (const el in formDataElements) {
formData[el] = formDataElements[el].value;
}
const sendEmail = $.ajax({
type: 'POST',
url: document.querySelector('.emailForm').getAttribute('action'),
dataType: 'json',
data: formData,
});
sendEmail.fail((error) => {
console.log(error);
formAlert.innerHTML = customErrors.errorsInForm;
});
sendEmail.done((response) => {
console.log(response);
formAlert.innerHTML = 'Wysłano! Dzięki za wiadomość!';
});
} else {
formAlert.innerHTML = customErrors.errorsInForm;
}
});
function validateEmailForm() {
let valid = true;
for (const el in formDataElements) {
const fieldName = formDataElements[el].parentElement.innerText;
if (formDataElements[el].validity.valueMissing === true) {
markWrongInput(formDataElements[el], customErrors.empty(fieldName.toLowerCase()));
} else if (formDataElements[el].validity.tooShort === true) {
const min = formDataElements[el].getAttribute('minlength');
markWrongInput(formDataElements[el], customErrors.tooShort(fieldName.toLowerCase(), min));
} else if (formDataElements[el].validity.tooLong === true) {
const max = formDataElements[el].getAttribute('maxlength');
markWrongInput(formDataElements[el], customErrors.tooLong(fieldName.toLowerCase(), max));
} else if (formDataElements[el].validity.typeMismatch === true) {
markWrongInput(formDataElements[el], customErrors.type(fieldName.toLowerCase()));
}
if (formDataElements[el].validity.valid === false) {
valid = false;
}
}
if (grecaptcha.getResponse().length === 0) {
markWrongInput(recaptcha, customErrors.recaptcha);
valid = false;
}
return valid;
}
function markWrongInput(wrongElement, alert) {
if (wrongElement.classList.contains('wrongInput')) {
return;
}
const errorMessageEl = document.createElement('p');
errorMessageEl.classList.add('error');
errorMessageEl.classList.add('wrongInput');
errorMessageEl.textContent = alert;
wrongElement.parentElement.append(errorMessageEl);
wrongElement.classList.add('wrongInput');
wrongElement.addEventListener('focus', clearErrors);
}
function clearErrors() {
this.classList.remove('wrongInput');
this.parentElement.removeChild(this.parentElement.getElementsByClassName('error')[0]);
formAlert.innerHTML = '';
}
window.recaptchaClearErr = () => {
document.querySelector('.g-recaptcha').focus();
};
});
function RecaptchaClearMsg() {
document.querySelector('.g-recaptcha').focus();
}