-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathbuycar.js
More file actions
74 lines (67 loc) · 2.48 KB
/
Copy pathbuycar.js
File metadata and controls
74 lines (67 loc) · 2.48 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
// Modal logic
const modal = document.getElementById("modal");
const modalTitle = document.getElementById("modal-title");
const modalMessage = document.getElementById("modal-message");
const modalAction = document.getElementById("modal-action");
const closeModal = document.getElementById("close-modal");
function showModal(title, message, actionText, actionCallback) {
modalTitle.textContent = title;
modalMessage.textContent = message;
if (actionText) {
modalAction.textContent = actionText;
modalAction.style.display = "inline-block";
modalAction.onclick = actionCallback;
} else {
modalAction.style.display = "none";
}
modal.classList.add("active");
}
function hideModal() {
modal.classList.remove("active");
}
closeModal.onclick = hideModal;
modal.onclick = function (e) {
if (e.target === modal) hideModal();
};
// Event listeners are best placed inside a DOMContentLoaded to ensure all elements are loaded
document.addEventListener("DOMContentLoaded", () => {
// Button event listeners
document.querySelectorAll(".buy-btn").forEach((btn) => {
btn.addEventListener("click", function () {
const car = btn.getAttribute("data-car");
showModal(
"Buy Car",
`Thank you for choosing to buy the ${car}! Our team will contact you soon for further process.`,
null
);
});
});
document.querySelectorAll(".rent-btn").forEach((btn) => {
btn.addEventListener("click", function () {
const car = btn.getAttribute("data-car");
showModal(
"Rent Car",
`You have selected to rent the ${car}. Please login or contact us to proceed.`,
"Go to Login",
() => {
// Correct path from feat/signup-login branch
window.location.href = "src/pages/login.html";
}
);
});
});
// Search Bar Logic from main branch
const searchBar = document.getElementById("searchBar");
const carCards = document.querySelectorAll(".car-card");
// Check if searchBar exists to avoid errors on pages without it
if (searchBar) {
searchBar.addEventListener("input", () => {
const searchTerm = searchBar.value.toLowerCase();
carCards.forEach(card => {
const carName = card.querySelector("h2").textContent.toLowerCase();
// Toggle display based on whether the car name includes the search term
card.style.display = carName.includes(searchTerm) ? "block" : "none";
});
});
}
});