-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (112 loc) · 4.46 KB
/
app.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
// All Offers
let offers = {
offerOne: "بطاقة واحدة لمدة سنتين بـ 200 ريال",
offerTwo: "بطاقتين لمدة سنة بـ 200 ريال",
offerThree: "بطاقتين لمدة سنتين بـ 300 ريال",
offerFour: "4 بطاقات لمدة سنة بـ 300 ريال",
};
// Select element <select></select>
let selectElement = document.getElementById("offers");
// Show\hide inputs depends on selected offer
let inputsTwoCard = document.getElementById("offer-two-cards");
let inputsFourCard = document.getElementById("offer-four-cards");
// Change hiden inputs title "بيانات البطاقات الإضافية" or "بيانات البطاقة الإضافية"
let extraInputsTitle = document.getElementById("extra-inputs-title");
// Add\remove "required" from hiden inputs
let colectionTwo = document.querySelectorAll(".colection-two");
let colectionFour = document.querySelectorAll(".colection-four");
// Add offers <option> to <select> element
const applyOffersToHtml = () => {
for (let key in offers) {
let optionElement = document.createElement("option");
optionElement.value = offers[key];
optionElement.textContent = offers[key];
selectElement.appendChild(optionElement);
}
};
applyOffersToHtml();
// Handle <select> change
selectElement.addEventListener("change", showInputs);
function showInputs() {
let selectedOffer = selectElement.value;
if (selectedOffer === offers.offerOne) {
// Remove "required" from hiden inputs (colectionTwoInputs & colectionFourInputs)
for (let i = 0; i < colectionTwo.length; i++) {
colectionTwo[i].removeAttribute("required");
}
for (let i = 0; i < colectionFour.length; i++) {
colectionFour[i].removeAttribute("required");
}
// Hide inputs box
inputsTwoCard.style.display = "none";
inputsFourCard.style.display = "none";
} else if (
selectedOffer === offers.offerTwo ||
selectedOffer === offers.offerThree
) {
extraInputsTitle.innerHTML = "بيانات البطاقة الإضافية";
inputsTwoCard.style.display = "block";
inputsFourCard.style.display = "none";
// Remove "required" from hiden inputs (colectionFourInputs)
for (let i = 0; i < colectionFour.length; i++) {
colectionFour[i].removeAttribute("required");
}
// Add "required" to showen inputs (colectionTwo)
for (let i = 0; i < colectionTwo.length; i++) {
colectionTwo[i].setAttribute("required", "");
}
} else if (selectedOffer === offers.offerFour) {
extraInputsTitle.innerHTML = "بيانات البطاقات الإضافية";
inputsTwoCard.style.display = "block";
inputsFourCard.style.display = "block";
// Add "required" to showen inputs (colectionFour)
for (let i = 0; i < colectionFour.length; i++) {
colectionFour[i].setAttribute("required", "");
}
// Add "required" to showen inputs (colectionTwo)
for (let i = 0; i < colectionTwo.length; i++) {
colectionTwo[i].setAttribute("required", "");
}
}
}
// Handle form data
const scriptURL =
"https://script.google.com/macros/s/AKfycbxMjDlzvAEXFDlFXFcno5dWYS9oUImYXxS8j1G_RrNSFajF1PqjgIpX9USLpap9E2uw/exec";
// form
let form = document.forms["submit-to-google-sheet"];
// Submit button
let submitBtn = document.getElementById("submit-btn");
form.addEventListener("submit", async (e) => {
e.preventDefault();
// disable button until submit finish
submitBtn.setAttribute("disabled", "");
// Run bootstrap spinner
submitBtn.innerHTML =
'<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
try {
const response = await fetch(scriptURL, {
method: "POST",
body: new FormData(form),
});
console.log("Success!", response);
window.location.href = "./thankyou.html"; // Go to thank you page
// Print data for testing
// const z = new FormData(form)
// for (let entry of z.entries()) {
// console.log(entry);
// }
} catch (error) {
console.error("Error!", error.message);
submitBtn.removeAttribute("disabled"); // Active btn
submitBtn.innerHTML = "اطلب"; //Replace spinner to "اطلب"
}
});
// Hide sidebar(offcanvas) on click on any nav link (in small screen)
let navLinks = document.querySelectorAll(".anchor");
navLinks.forEach((navLink) => {
navLink.addEventListener("click", clickOnCloseBtn);
});
function clickOnCloseBtn() {
let btn = document.getElementsByClassName("btn-close")[0];
btn.click(); // click on hiden close btn
}