Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Add form logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dreit-p committed Nov 12, 2020
1 parent c65b8cf commit 3119c73
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
122 changes: 116 additions & 6 deletions src/components/modals/contents/feedback.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,119 @@
(()=>{
let form = document.querySelector('form.feedback-form');

form.querySelectorAll('.submit-btn').forEach(function(btn) {
btn.addEventListener('click', function (e) {
btn.classList.toggle('done');
/*=============================================
= number inputs handler =
=============================================*/

(()=>{
let inputs = document.querySelectorAll('input[type="number"]');
inputs.forEach(function(input) {
input.addEventListener('blur', function (e) {
e.target.value = e.target.value.replace(/[^+\s\d]/g, '');
let val = e.target.value
if (!!e.target.getAttribute('max') && +val > e.target.getAttribute('max')) {
e.target.value = e.target.getAttribute('max');
}
if (!!e.target.getAttribute('min') && +val < e.target.getAttribute('min')) {
e.target.value = e.target.getAttribute('min');
}
}, false);
});
})()
})();

/*===== End of number inputs handler ======*/


/*=======================================
= required states =
=======================================*/

(()=>{
// generate groups

let groups = new Map();
document.querySelectorAll('input[data-required-group]').forEach(function(input) {
if (groups.get(input.dataset.requiredGroup)) {
groups.get(input.dataset.requiredGroup).push(input);
} else {
groups.set(input.dataset.requiredGroup, [input]);
}
});

// set connections

groups.forEach(function(group) {
group.forEach(function(input) {
let groupmates = [...group];
groupmates.splice(groupmates.indexOf(input), 1);

input.addEventListener('input', function () {
groupmates.forEach(function(mate) {
if (input.value.length > 0) {
mate.removeAttribute("required");
mate.closest(".input-wrapper").classList.remove("required");
} else {
mate.setAttribute("required", "required");
mate.closest(".input-wrapper").classList.add("required");
}
});
}, false);
});
});

})();

/*===== End of required states ======*/



(()=>{
let forms = [
...document.querySelectorAll('form.feedback-form')
];
if (forms.length < 1) return false;

forms.forEach((form)=>{
form.addEventListener('submit', function (e) {
e.preventDefault();
if (form.classList.contains('sent')) return false;
form.classList.add('sent');
let data = new FormData(form);
let url = form.getAttribute('action');
let sendBtn = form.querySelector('.submit-btn');

fetch(process.env.API_HOST + url, {
method: 'POST',
body: data
}).then((response)=>{
if (response.ok) {
form.classList.add('successful');
sendBtn.classList.toggle('done', true);
setTimeout(function() {
form.reset();
form.classList.remove('successful');
form.classList.remove('sent');
sendBtn.classList.toggle('done', false);
}, 4000);
} else {
if (response.statusText) {
new window.Notification({
type: "error",
title: "Error",
message: response.statusText
});
}
if (response.errors) {
for (var key in response.errors) {
new window.Notification({
type: "error",
title: key,
message: response.errors[key]
});
}
}
form.classList.remove('sent');
}
});

}, false);
})
})();
13 changes: 7 additions & 6 deletions src/components/modals/contents/feedback.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
.description
| Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown.
.half
form.feedback-form
form.feedback-form(action="../api/requests")
.notice
span.icon
span.text необходимые поля
.ui-input.input-wrapper.required
input(placeholder="Ваше имя" required pattern="[A-Za-zА-Яа-я -]+")
input(placeholder="Ваше имя" name="name" maxlength="191" required pattern="[A-Za-zА-Яа-я -]+")
.ui-input.input-wrapper.required
input(placeholder="Email" type="email" required pattern="^[a-zA-Z0-9.!#$%&’*+=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
input(placeholder="Email" data-required-group="contact" name="email" type="email" maxlength="191" required pattern="^[a-zA-Z0-9.!#$%&’*+=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
.ui-input.input-wrapper.required
input(placeholder="Номер телефона" type="tel" required pattern="([(+]*[0-9]+[()+. -]*)")
input(placeholder="Номер телефона" data-required-group="contact" name="phone" type="tel" maxlength="32" required pattern="([(+]*[0-9]+[()+. -]*)")
.ui-input.input-wrapper
input(placeholder="Бюджет")
.input-icon &#8381;
input(placeholder="Бюджет" name="budget" type="number" step="0" max="99999999" min="0")
.ui-input.input-wrapper.required
textarea(placeholder="Ваше сообщение" rows="4" required)
textarea(placeholder="Ваше сообщение" name="description" maxlength="8392" rows="4" required)

button.submit-btn.chamber(data-first-text="Chamber" data-last-text="Done!") Отправить
br
Expand Down
6 changes: 6 additions & 0 deletions src/components/modals/contents/feedback.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
.submit-btn {
min-width: 49%;
}
&.sent {
.input-wrapper {
opacity: 0.6;
pointer-events: none;
}
}
}
.title {
font-size: 56px;
Expand Down
15 changes: 15 additions & 0 deletions src/components/ui-elems/inputs.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.ui-input {
padding-right: 1rem;
position: relative;
input,
textarea {
padding: 18px 24px 14px;
Expand All @@ -19,6 +20,20 @@
textarea {
resize: vertical;
}
.input-icon {
position: absolute;
right: calc(1rem + 20px);
top: 0;
bottom: 0;
margin: auto;
font-size: 18px;
height: 1em;
opacity: 0.4;
font-weight: 500;
& + input {
padding-right: 40px;
}
}
&.required {
position: relative;
&:after {
Expand Down

0 comments on commit 3119c73

Please sign in to comment.