-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (71 loc) · 2.47 KB
/
Copy pathscript.js
File metadata and controls
78 lines (71 loc) · 2.47 KB
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
import { TextWriter } from "./TextWriter.js";
const form = document.getElementById("contact-form");
const fName = document.getElementById("fName");
const fEmail = document.getElementById("fEmail");
const fMessage = document.getElementById("fMessage");
const title = document.getElementById("fTitle");
const writer = new TextWriter(title);
form.addEventListener("submit", (e) => {
e.preventDefault();
!fName.value && errorField(fName);
!fEmail.value && errorField(fEmail);
!fMessage.value && errorField(fMessage);
if (!fName.value || !fEmail.value || !fMessage.value) return;
const writing = writer.write("Getting in touch . . .");
changeContacting();
const url = `https://docs.google.com/forms/d/e/1FAIpQLSdJmv7jU49t4FcWpbdjcycWiGO0sx9xPji1Q8s8QfLva4yc6w/formResponse?submit=Submit&usp=pp_url&entry.1137438515=${fName.value}&entry.32220053=${fEmail.value}&entry.1023053542=${fMessage.value}`;
fetch(url, { mode: "no-cors" })
.then(() => {
writing.then(() => {
writer.write("Thanks for getting in touch!");
changeContacted();
});
})
.catch((error) => {
writing.then(() => {
writer.write("Couldn't get in touch :(").finally(changeContacted);
});
console.error("Error submitting form:", error);
});
});
form.addEventListener("reset", (e) => {
e.preventDefault();
fMessage.value = "";
const writing = writer.write("Get in touch");
changeContacting();
setTimeout(changeReset, 500);
writing.finally(() => writer.clearCursor());
});
const timeouts = {};
function errorField(field) {
if (timeouts[field.id]) {
clearTimeout(timeouts[field.id]);
}
field.classList.add("error");
timeouts[field.id] = setTimeout(() => {
field.classList.remove("error");
}, 5000);
}
function changeContacting() {
[...form.children].forEach((child) => (child.hidden = child.id !== "fTitle"));
form.setAttribute("view", "only-title");
}
function changeContacted() {
[...form.children].forEach(
(child) => (child.hidden = child.id !== "fTitle" && child.id !== "fReset"),
);
form.setAttribute("view", "title-and-again");
}
function changeReset() {
[...form.children].forEach((child) => (child.hidden = child.id == "fReset"));
form.removeAttribute("view");
}
const sections = document.querySelectorAll(".section");
const lastSection = sections[sections.length - 1];
function updateMargin() {
const h = lastSection.offsetHeight;
const margin = (window.innerHeight - h) / 2;
lastSection.style.marginBottom = `${margin}px`;
}
updateMargin();
window.addEventListener("resize", updateMargin);