-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (133 loc) · 5.07 KB
/
Copy pathscript.js
File metadata and controls
151 lines (133 loc) · 5.07 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const cart = [];
const productsContainer = document.querySelector(".products");
const searchInput = document.querySelector("#search-input");
const searchButton = document.querySelector("#search-button");
const lg_btn = document.getElementById("login_btn");
fetch("main.json")
.then((response) => response.json())
.then((p) => {
if (p.loggedin == 1) {
lg_btn.innerHTML = "logout";
} else {
lg_btn.innerHTML = "login";
}
lg = p.loggedin;
});
lg_btn.addEventListener("click", () => {
if (lg == 1) {
location.reload();
fetch(`http://127.0.0.1:3000/logout`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
console.log(response);
})
.catch((error) => console.error("Error:", error));
} else {
window.location.href = "/login.html";
}
});
function updateCart() {
const cartList = document.querySelector("#cart ul");
const totalDisplay = document.querySelector("#cart p");
cartList.innerHTML = "";
let total = 0;
cart.forEach((item) => {
const li = document.createElement("li");
li.textContent = `${item.name} - $${item.price.toFixed(2)}`;
cartList.appendChild(li);
total += item.price;
});
totalDisplay.textContent = `Total: $${total.toFixed(2)}`;
}
let allProducts = []; // Store all products for filtering
// Fetch product data from JSON
fetch("products.json")
.then((response) => response.json())
.then((products) => {
allProducts = products; // Store the fetched products for search
displayProducts(allProducts); // Display all products initially
});
// Function to display products dynamically
function displayProducts(products) {
productsContainer.innerHTML = ""; // Clear the product container
products.forEach((product) => {
const productElement = document.createElement("div");
productElement.classList.add("product");
productElement.setAttribute("data-name", product.name);
productElement.setAttribute("data-price", product.price);
productElement.innerHTML = `
<a href="product.html?id=${encodeURIComponent(product.id)}">
<img src="${product.image}" alt="${product.name}">
</a>
<h3>${product.name}</h3>
<p>₹${product.price.toFixed(2)}</p>
<button class="add-to-cart-btn">Add to Cart</button>
`;
productsContainer.appendChild(productElement);
// Add event listener to the "Add to Cart" button
productElement
.querySelector(".add-to-cart-btn")
.addEventListener("click", () => {
console.log("q---");
fetch(
`http://127.0.0.1:3000/add-product?${encodeURIComponent(product.id)}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => {
if (response.ok) {
console.log("ki");
} else {
console.log("op");
}
console.log(response);
})
.catch((error) => console.error("Error:", error));
});
// Add event listener to the "Buy now" button
// productElement.querySelector(".buy-now-btn").addEventListener("click", () => {
// // Store the product information in localStorage or pass via URL
// localStorage.setItem("selectedProduct", JSON.stringify(product)); // Store product data
// window.location.href = `payment.html?id= ${encodeURIComponent(product.id)}`; // Redirect to the payment page
// });
});
}
// // Search functionality (triggered by input and button)
function searchProducts() {
const searchTerm = searchInput.value.toLowerCase(); // Get the search term
const filteredProducts = allProducts.filter(
(product) => product.name.toLowerCase().includes(searchTerm) // Filter products based on name
);
displayProducts(filteredProducts); // Update the displayed products
}
// Event listener for the search button
searchButton.addEventListener("click", () => {
searchProducts(); // Call the search function when the button is clicked
});
// Real-time search functionality (triggered while typing)
searchInput.addEventListener("input", () => {
searchProducts(); // Call the search function on every input change
});
// Optional: Trigger search on Enter keypress in the search bar
searchInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
e.preventDefault(); // Prevent form submission
searchProducts(); // Call the search function
}
document.addEventListener("DOMContentLoaded", () => {
const shopButton = document.querySelector('a[href="#products"]'); // Target the "Shop" button
const productsSection = document.getElementById("products"); // Target the "Products" section
shopButton.addEventListener("click", (event) => {
event.preventDefault(); // Prevent default anchor behavior
productsSection.scrollIntoView({ behavior: "smooth" }); // Smooth scrolling
});
});
});