From 4e2255fb4fa328a04aec28c8ab4395925522ffe4 Mon Sep 17 00:00:00 2001
From: Chandrashekhar Athnere <58299457+csathnere@users.noreply.github.com>
Date: Sun, 11 Aug 2024 19:51:14 +0530
Subject: [PATCH] Revert "js updated ad bug fixed"
---
js/addKartProduct.js | 204 ++++++++++++++++++-------------------------
viewcart/index.html | 5 +-
2 files changed, 88 insertions(+), 121 deletions(-)
diff --git a/js/addKartProduct.js b/js/addKartProduct.js
index 0b0d30e0..26a52139 100644
--- a/js/addKartProduct.js
+++ b/js/addKartProduct.js
@@ -1,144 +1,112 @@
document.addEventListener("DOMContentLoaded", () => {
- const elementsToLoad = [
+ [
{ id: "footer-addkart", url: "../footer/footer.html" },
{ id: "header-addkart", url: "../header/header.html" },
- ];
-
- elementsToLoad.forEach(({ id, url }) => loadHtml(id, url));
+ ].forEach(({ id, url }) =>
+ fetch(url)
+ .then((res) => res.text())
+ .then((data) => (document.getElementById(id).innerHTML = data))
+ .catch((err) => console.error(`Error loading ${url}:`, err))
+ );
});
-const loadHtml = (id, url) => {
- fetch(url)
- .then((res) => res.ok ? res.text() : Promise.reject(res.status))
- .then((data) => document.getElementById(id).innerHTML = data)
- .catch((err) => {
- console.error(`Error loading ${url}:`, err);
- document.getElementById(id).innerHTML = `
Failed to load content. Please try again later.
`;
- });
-};
-
-const getQueryParameter = (name) => {
- return new URLSearchParams(window.location.search).get(name);
-};
+const getQueryParameter = (name) => new URLSearchParams(window.location.search).get(name);
+let simiCategory;
const formatIndianRupee = (number) => {
const [integerPart, decimalPart] = number.toString().split(".");
return integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (decimalPart ? "." + decimalPart : "");
};
-const createProductCard = (product) => {
- const discount = Math.floor((product.rating * parseInt(product.price.toString().slice(0, 2))) / 10);
- const discountedPrice = Math.round(((100 - discount) * product.price) / 100);
-
+const createSearchProductCard = (product) => {
+ simiCategory = product.category;
+ const discont = Math.floor((product.rating * parseInt(product.price.toString().slice(0, 2))) / 10);
+ const afterDiscontPrice = Math.round(((100 - discont) * product.price) / 100);
return `
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
${product.name}
-
${product.rating}  
-
Special price
-
- ₹${formatIndianRupee(discountedPrice)}
- ₹${formatIndianRupee(product.price)}
- ${discount}% off
-
-
Available offers
-
-
-
Bank Offer Get ₹50 instant discount on first Flipkart UPI transaction on order of ₹200 and above
T&C
-
-
Bank Offer 5% Cashback on Flipkart Axis Bank Card
T&C
-
-
Special Price Get extra 12% off (price inclusive of cashback/coupon)
T&C
-
View 12 more offers
-
-
-
Warranty
-
Covers Manufacturing Defects Know More
-
-
-
+
+
+
${product.name}
+
${product.rating}  
+
Special price
+
₹${formatIndianRupee(afterDiscontPrice)} ₹${formatIndianRupee(product.price)} ${discont}% off ?
+
Available offers
+
+
Bank Offer Get ₹50 instant discount on first Flipkart UPI transaction on order of ₹200 and above
T&C
+
Bank Offer 5% Cashback on Flipkart Axis Bank Card
T&C
+
Special Price Get extra 12% off (price inclusive of cashback/coupon)
T&C
+
View 12 more offers
+
+
Warranty     Covers Manufacturing Defects Know More
+
+
+
Delivery
+
-
-
-
Delivery by 16 Jul, Tuesday
-
-
Free ?
-
-
if ordered before 9:59 PM
+
+
+
+
Delivery by 16 Jul, Tuesday
+
+
Free ?
-
View Details
+
if ordered before 9:59 PM
-
${product.description}
+
View Details
-
`;
+
${product.description}
+
+
`;
};
-const createSimilarProductCard = (product) => {
- const discount = Math.floor((product.rating * parseInt(product.price.toString().slice(0, 2))) / 10);
- const discountedPrice = Math.round(((100 - discount) * product.price) / 100);
-
+const createSearchSimilarProducts = (product) => {
+ const discont = Math.floor((product.rating * parseInt(product.price.toString().slice(0, 2))) / 10);
+ const afterDiscontPrice = Math.round(((100 - discont) * product.price) / 100);
return `
-
-
- ₹${formatIndianRupee(discountedPrice)}
- ${product.name}
- ₹${formatIndianRupee(product.price)}
- ${discount}% off
- `;
+
+
+
+
+
+
+
${product.name.slice(0, 20)}${product.name.length > 20 ? "... " : ""}
+
${product.rating}  
+
₹${formatIndianRupee(afterDiscontPrice)} ₹${formatIndianRupee(product.price)} ${discont}% off
+
+ `;
};
-const displayProduct = async () => {
- try {
- const productId = getQueryParameter("id");
- if (!productId) throw new Error("Product ID is missing in the URL.");
-
- const response = await fetch("https://raw.githubusercontent.com/csathnere/APIs/main/json-ec/products.json");
- if (!response.ok) throw new Error("Failed to fetch product data.");
-
- const products = await response.json();
- const product = products.find((p) => p.id === parseInt(productId));
- if (!product) throw new Error("Product not found.");
-
- document.getElementById("productkart-card").innerHTML = createProductCard(product);
+const searchFetch = (products) => {
+ document.getElementById("addkart").innerHTML = products.map(createSearchProductCard).join("");
+};
- const similarProductsContainer = document.getElementById("similar-products");
- similarProductsContainer.innerHTML = products
- .filter((p) => p.category === product.category && p.id !== product.id)
- .map((p) => createSimilarProductCard(p))
- .join("");
- } catch (error) {
- console.error(error);
- document.getElementById("productkart-card").innerHTML = `
Error: ${error.message}
`;
- }
+const searchFetchSimi = (products) => {
+ document.getElementById("similarProducts").innerHTML = products.map(createSearchSimilarProducts).join("");
};
-displayProduct();
+fetch("https://raw.githubusercontent.com/csathnere/APIs/main/json-ec/product.json")
+ .then((res) => res.json())
+ .then((data) => {
+ const query = getQueryParameter("query");
+ searchFetch(data.filter((product) => product.name == query));
+ setInterval(() => searchFetchSimi(data.filter((product) => product.category == simiCategory).slice(0, 12)), 1500);
+ })
+ .catch((err) => console.error("Error fetching data:", err));
diff --git a/viewcart/index.html b/viewcart/index.html
index 798b7945..b80f6bff 100644
--- a/viewcart/index.html
+++ b/viewcart/index.html
@@ -123,8 +123,7 @@
Missing Cart items?
-->
- PLACE ORDER
-
+ PLACE ORDER
@@ -141,7 +140,7 @@
PRICE DETAILS
- Buy Now
+ Buy Now