diff --git a/js/addKartProduct.js b/js/addKartProduct.js index 26a52139..0b0d30e0 100644 --- a/js/addKartProduct.js +++ b/js/addKartProduct.js @@ -1,112 +1,144 @@ document.addEventListener("DOMContentLoaded", () => { - [ + const elementsToLoad = [ { id: "footer-addkart", url: "../footer/footer.html" }, { id: "header-addkart", url: "../header/header.html" }, - ].forEach(({ id, url }) => - fetch(url) - .then((res) => res.text()) - .then((data) => (document.getElementById(id).innerHTML = data)) - .catch((err) => console.error(`Error loading ${url}:`, err)) - ); + ]; + + elementsToLoad.forEach(({ id, url }) => loadHtml(id, url)); }); -const getQueryParameter = (name) => new URLSearchParams(window.location.search).get(name); +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); +}; -let simiCategory; const formatIndianRupee = (number) => { const [integerPart, decimalPart] = number.toString().split("."); return integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (decimalPart ? "." + decimalPart : ""); }; -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); +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); + return ` - -Error: ${error.message}
`; + } }; -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)); +displayProduct(); diff --git a/viewcart/index.html b/viewcart/index.html index b80f6bff..798b7945 100644 --- a/viewcart/index.html +++ b/viewcart/index.html @@ -123,7 +123,8 @@