-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (87 loc) · 2.71 KB
/
main.js
File metadata and controls
99 lines (87 loc) · 2.71 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { app, db, collection, addDoc } from "./firebaseConfig.js";
import "./style.css";
import favicon from "./assets/images/calin.codes-favicon.jpg";
import logoDark from "./assets/images/calin.codes-logo-dark.jpg";
import {
mobileMenuBtn,
toggleMenu,
textAnimation,
animationContainer,
} from "./modulesJS/utility.js";
import { checkCanvas } from "./modulesJS/trailEffect.js";
window.addEventListener("load", () => {
const faviconElement = document.getElementById("favicon");
const logoElement = document.getElementById("logo");
logoElement.src = logoDark;
if (faviconElement !== null) {
faviconElement.href = favicon;
}
});
mobileMenuBtn.addEventListener("click", () => {
toggleMenu();
});
// contact form
const contactDetails = document.getElementById("contact-details");
const contactForm = document.getElementById("contact-form");
if (contactForm !== null) {
contactForm.addEventListener("submit", (e) => {
e.preventDefault();
const name = getInputValue("contact-name");
const email = getInputValue("contact-email");
const subject = getInputValue("contact-subject");
const message = getInputValue("contact-message");
const dateAndTime = new Date();
const data = {
name,
email,
subject,
message,
dateAndTime,
};
if (!validateEmail(email)) {
const emailError = document.createElement("p");
emailError.classList.add("error");
emailError.textContent = "Please enter a valid email address";
contactDetails.appendChild(emailError);
setTimeout(() => {
emailError.remove();
}, 3000);
return;
} else {
try {
addDoc(collection(db, "messages"), data);
const successMessage = document.getElementById("success-message");
successMessage.setAttribute("data-visible", true);
setTimeout(() => {
successMessage.setAttribute("data-visible", false);
}, 3200);
} catch (e) {
emailError.textContent = "Something went wrong. Please try again later";
contactDetails.appendChild(emailError);
setTimeout(() => {
emailError.remove();
}, 3000);
} finally {
contactForm.reset();
}
}
});
}
function getInputValue(id) {
return document.getElementById(id).value;
}
//validate form
const emailRegex =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function validateEmail(email) {
return emailRegex.test(String(email).toLowerCase());
}
// text animation
function checkAnimationContainer() {
if (animationContainer) {
textAnimation();
}
}
//calls
checkAnimationContainer();
checkCanvas();