From 6078136d1510683337fde91dbe08a8c07ae55e7d Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Thu, 9 Jan 2025 11:20:04 +0530
Subject: [PATCH 01/31] add backend practice file
---
public/js/backend-practice.js | 7 +++++++
public/js/checkout.js | 1 +
2 files changed, 8 insertions(+)
create mode 100644 public/js/backend-practice.js
diff --git a/public/js/backend-practice.js b/public/js/backend-practice.js
new file mode 100644
index 0000000..680293b
--- /dev/null
+++ b/public/js/backend-practice.js
@@ -0,0 +1,7 @@
+const xhr = new XMLHttpRequest();
+
+xhr.addEventListener('load',()=>{
+ console.log(xhr.response);
+})
+xhr.open('GET','https://supersimplebackend.dev/documentation');
+xhr.send();
\ No newline at end of file
diff --git a/public/js/checkout.js b/public/js/checkout.js
index aa48400..6c4a92e 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,3 +1,4 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
renderOrderSummary();
+import '../js/backend-practice.js';
\ No newline at end of file
From ff00e2b283c50a741bb51c25ae5847e3337ed17f Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Thu, 9 Jan 2025 13:09:19 +0530
Subject: [PATCH 02/31] Load products from the backend
---
public/js/amazon.js | 38 +-
public/js/backend-practice.js | 7 -
public/js/checkout.js | 7 +-
public/js/products.js | 641 ++--------------------------------
4 files changed, 45 insertions(+), 648 deletions(-)
delete mode 100644 public/js/backend-practice.js
diff --git a/public/js/amazon.js b/public/js/amazon.js
index 3ce08e4..a1711c1 100644
--- a/public/js/amazon.js
+++ b/public/js/amazon.js
@@ -1,8 +1,11 @@
import { cart } from "/js/cart-class.js";
-import { products } from "/js/products.js";
-let productsHTML = "";
-products.forEach((product) => {
- productsHTML += `
+import { products, loadProducts } from "/js/products.js";
+loadProducts(renderProductsGrid);
+
+export function renderProductsGrid() {
+ let productsHTML = "";
+ products.forEach((product) => {
+ productsHTML += `
![]()
{
`;
-});
-document.querySelector(".js-products-grid").innerHTML = productsHTML;
-document.querySelectorAll(".js-add-to-cart").forEach((button) => {
- button.addEventListener("click", () => {
- const { productId } = button.dataset;
- cart.addToCart(productId);
- cart.updateCartQuantity(productId);
});
-});
-if (cart.calculateCartQuantity() === 0) {
- document.querySelector(".js-cart-quantity").innerHTML = "";
-} else {
- document.querySelector(".js-cart-quantity").innerHTML =
- cart.calculateCartQuantity();
+ document.querySelector(".js-products-grid").innerHTML = productsHTML;
+ document.querySelectorAll(".js-add-to-cart").forEach((button) => {
+ button.addEventListener("click", () => {
+ const { productId } = button.dataset;
+ cart.addToCart(productId);
+ cart.updateCartQuantity(productId);
+ });
+ });
+ if (cart.calculateCartQuantity() === 0) {
+ document.querySelector(".js-cart-quantity").innerHTML = "";
+ } else {
+ document.querySelector(".js-cart-quantity").innerHTML =
+ cart.calculateCartQuantity();
+ }
}
diff --git a/public/js/backend-practice.js b/public/js/backend-practice.js
deleted file mode 100644
index 680293b..0000000
--- a/public/js/backend-practice.js
+++ /dev/null
@@ -1,7 +0,0 @@
-const xhr = new XMLHttpRequest();
-
-xhr.addEventListener('load',()=>{
- console.log(xhr.response);
-})
-xhr.open('GET','https://supersimplebackend.dev/documentation');
-xhr.send();
\ No newline at end of file
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 6c4a92e..9b011aa 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,4 +1,5 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
-renderOrderSummary();
-
-import '../js/backend-practice.js';
\ No newline at end of file
+import { loadProducts } from "./products.js";
+loadProducts(() => {
+ renderOrderSummary();
+});
diff --git a/public/js/products.js b/public/js/products.js
index afee1a6..bbcc94b 100644
--- a/public/js/products.js
+++ b/public/js/products.js
@@ -68,624 +68,23 @@ class Appliances extends Product {
`;
}
}
-export const products = [
- {
- id: "e43608ze-6aa0-4b85-b27f-e1d07eb67z16",
- image: "images/products/Chanakya Neeti.jpg",
- name: "Chanakya Neeti with Chanakya Sutra Sahit",
- rating: {
- stars: 4.5,
- count: 197,
- },
- priceCents: 1190,
- keywords: ["book", "Chanakya Neeti", "sutra"],
- },
- {
- id: "e43638ye-6aa0-4b85-b27f-e1d07eb67z16",
- image: "images/products/sh.jpg",
- name: "Shoonya Se Aakhir Tak ",
- rating: {
- stars: 4.5,
- count: 197,
- },
- priceCents: 690,
- keywords: ["book", "Shoonya Se Aakhir Tak ", "noval"],
- },
- {
- id: "e43638ze-6aa0-4b85-b27f-e1d07eb67z16",
- image: "images/products/pitaji.jpg",
- name: "Pitaji Aur Tarikh",
- rating: {
- stars: 4.5,
- count: 147,
- },
- priceCents: 790,
- keywords: ["book", "father", "noval"],
- },
- {
- id: "e43638ce-6aa0-4b85-b27f-e1d07eb67z16",
- image: "images/products/gandhi.jpg",
- name: "The Story of My Experiments With Truth: Mahatma Gandhi, An Autobiography (Hindi)",
- rating: {
- stars: 4.5,
- count: 217,
- },
- priceCents: 990,
- keywords: ["book", "gandhi", "autobiography"],
- },
- {
- id: "e43638ce-6aa0-4b85-b27f-e1d07eb67zz6",
- image: "images/products/yogi.jpg",
- name: "Autobiography of A Yogi (Hindi)",
- rating: {
- stars: 4.5,
- count: 117,
- },
- priceCents: 1090,
- keywords: ["book", "yogi", "autobiography"],
- },
- {
- id: "e43638ce-6aa0-4b85-b27f-e1d07eb678c6",
- image: "images/products/athletic-cotton-socks-6-pairs.jpg",
- name: "Black and Gray Athletic Cotton Socks - 6 Pairs",
- rating: {
- stars: 4.5,
- count: 87,
- },
- priceCents: 1090,
- keywords: ["socks", "sports", "apparel"],
- },
- {
- id: "15b6fc6f-327a-4ec4-896f-486349e85a3d",
- image: "images/products/intermediate-composite-basketball.jpg",
- name: "Intermediate Size Basketball",
- rating: {
- stars: 4,
- count: 127,
- },
- priceCents: 2095,
- keywords: ["sports", "basketballs"],
- },
- {
- id: "14rvbae-wef23",
- image: "images/products/box1_image.jpg",
- name: "Home & care",
- rating: {
- stars: 3,
- count: 232,
- },
- priceCents: 3999,
- keywords: [
- "home",
- // "decoration"
- ],
- },
- {
- id: "83d4ca15-0f35-48f5-b7a3-1ea210004f2e",
- image: "images/products/adults-plain-cotton-tshirt-2-pack-teal.jpg",
- name: "Adults Plain Cotton T-Shirt - 2 Pack",
- rating: {
- stars: 4.5,
- count: 56,
- },
- priceCents: 799,
- keywords: ["tshirts", "apparel", "mens"],
- type: "clothing",
- sizeChartLink: "images/clothing-size-chart.png",
- },
- {
- id: "54e0eccd-8f36-462b-b68a-8182611d9add",
- image: "images/products/black-2-slot-toaster.jpg",
- name: "2 Slot Toaster - Black",
- rating: {
- stars: 5,
- count: 2197,
- },
- priceCents: 1899,
- keywords: ["toaster", "kitchen", "appliances"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "3ebe75dc-64d2-4137-8860-1f5a963e534b",
- image: "images/products/6-piece-white-dinner-plate-set.jpg",
- name: "6 Piece White Dinner Plate Set",
- rating: {
- stars: 4,
- count: 37,
- },
- priceCents: 2067,
- keywords: ["plates", "kitchen", "dining"],
- },
- {
- id: "8c9c52b5-5a19-4bcb-a5d1-158a74287c53",
- image: "images/products/6-piece-non-stick-baking-set.webp",
- name: "6-Piece Nonstick, Carbon Steel Oven Bakeware Baking Set",
- rating: {
- stars: 4.5,
- count: 175,
- },
- priceCents: 3499,
- keywords: ["kitchen", "cookware"],
- },
- {
- id: "dd82ca78-a18b-4e2a-9250-31e67412f98d",
- image: "images/products/plain-hooded-fleece-sweatshirt-yellow.jpg",
- name: "Plain Hooded Fleece Sweatshirt",
- rating: {
- stars: 4.5,
- count: 317,
- },
- priceCents: 2400,
- keywords: ["hoodies", "sweaters", "apparel"],
- },
- {
- id: "77919bbe-0e56-475b-adde-4f24dfed3a04",
- image: "images/products/luxury-tower-set-6-piece.jpg",
- name: "Luxury Towel Set - Graphite Gray",
- rating: {
- stars: 4.5,
- count: 144,
- },
- priceCents: 3599,
- keywords: ["bathroom", "washroom", "restroom", "towels", "bath towels"],
- },
- {
- id: "3fdfe8d6-9a15-4979-b459-585b0d0545b9",
- image: "images/products/liquid-laundry-detergent-plain.jpg",
- name: "Liquid Laundry Detergent, 110 Loads, 82.5 Fl Oz",
- rating: {
- stars: 4.5,
- count: 305,
- },
- priceCents: 2899,
- keywords: ["bathroom", "cleaning"],
- },
- {
- id: "58b4fc92-e98c-42aa-8c55-b6b79996769a",
- image: "images/products/knit-athletic-sneakers-gray.jpg",
- name: "Waterproof Knit Athletic Sneakers - Gray",
- rating: {
- stars: 4,
- count: 89,
- },
- priceCents: 3390,
- keywords: ["shoes", "running shoes", "footwear"],
- },
- {
- id: "5968897c-4d27-4872-89f6-5bcb052746d7",
- image: "images/products/women-chiffon-beachwear-coverup-black.jpg",
- name: "Women's Chiffon Beachwear Cover Up - Black",
- rating: {
- stars: 4.5,
- count: 235,
- },
- priceCents: 2070,
- keywords: ["robe", "swimsuit", "swimming", "bathing", "apparel"],
- type: "clothing",
- sizeChartLink: "images/clothing-size-chart.png",
- },
- {
- id: "aad29d11-ea98-41ee-9285-b916638cac4a",
- image: "images/products/round-sunglasses-black.jpg",
- name: "Round Sunglasses",
- rating: {
- stars: 4.5,
- count: 30,
- },
- priceCents: 1560,
- keywords: ["accessories", "shades"],
- },
- {
- id: "04701903-bc79-49c6-bc11-1af7e3651358",
- image: "images/products/women-beach-sandals.jpg",
- name: "Women's Two Strap Buckle Sandals - Tan",
- rating: {
- stars: 4.5,
- count: 562,
- },
- priceCents: 2499,
- keywords: ["footwear", "sandals", "womens", "beach", "summer"],
- },
- {
- id: "901eb2ca-386d-432e-82f0-6fb1ee7bf969",
- image: "images/products/blackout-curtain-set-beige.webp",
- name: "Blackout Curtains Set 4-Pack - Beige",
- rating: {
- stars: 4.5,
- count: 232,
- },
- priceCents: 4599,
- keywords: ["bedroom", "curtains", "home"],
- },
- {
- id: "82bb68d7-ebc9-476a-989c-c78a40ee5cd9",
- image: "images/products/men-slim-fit-summer-shorts-gray.jpg",
- name: "Men's Slim-Fit Summer Shorts",
- rating: {
- stars: 4,
- count: 160,
- },
- priceCents: 1699,
- keywords: ["shorts", "apparel", "mens"],
- },
- {
- id: "c2a82c5e-aff4-435f-9975-517cfaba2ece",
- image: "images/products/electric-glass-and-steel-hot-water-kettle.webp",
- name: "Electric Glass and Steel Hot Tea Water Kettle - 1.7-Liter",
- rating: {
- stars: 5,
- count: 846,
- },
- priceCents: 3074,
- keywords: ["water boiler", "appliances", "kitchen"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "6b07d4e7-f540-454e-8a1e-363f25dbae7d",
- image: "images/products/facial-tissue-2-ply-18-boxes.jpg",
- name: "Ultra Soft Tissue 2-Ply - 18 Box",
- rating: {
- stars: 4,
- count: 99,
- },
- priceCents: 2374,
- keywords: ["kleenex", "tissues", "kitchen", "tissues box", "napkins"],
- },
- {
- id: "a82c6bac-3067-4e68-a5ba-d827ac0be010",
- image: "images/products/straw-sunhat.webp",
- name: "Straw Lifeguard Sun Hat",
- rating: {
- stars: 4,
- count: 215,
- },
- priceCents: 2200,
- keywords: ["hats", "straw hats", "summer", "apparel"],
- },
- {
- id: "e4f64a65-1377-42bc-89a5-e572d19252e2",
- image: "images/products/sky-flower-stud-earrings.webp",
- name: "Sterling Silver Sky Flower Stud Earrings",
- rating: {
- stars: 4.5,
- count: 52,
- },
- priceCents: 1799,
- keywords: ["jewelry", "accessories", "womens"],
- },
- {
- id: "b0f17cc5-8b40-4ca5-9142-b61fe3d98c85",
- image: "images/products/women-stretch-popover-hoodie-black.jpg",
- name: "Women's Stretch Popover Hoodie",
- rating: {
- stars: 4.5,
- count: 2465,
- },
- priceCents: 1374,
- keywords: ["hooded", "hoodies", "sweaters", "womens", "apparel"],
- type: "clothing",
- sizeChartLink: "images/clothing-size-chart.png",
- },
- {
- id: "a93a101d-79ef-4cf3-a6cf-6dbe532a1b4a",
- image: "images/products/bathroom-rug.jpg",
- name: "Bathroom Bath Rug Mat 20 x 31 Inch - Grey",
- rating: {
- stars: 4.5,
- count: 119,
- },
- priceCents: 1250,
- keywords: ["bathmat", "bathroom", "home"],
- },
- {
- id: "4f4fbcc2-4e72-45cc-935c-9e13d79cc57f",
- image: "images/products/women-knit-ballet-flat-black.jpg",
- name: "Women's Knit Ballet Flat",
- rating: {
- stars: 4,
- count: 326,
- },
- priceCents: 2640,
- keywords: ["shoes", "flats", "womens", "footwear"],
- },
- {
- id: "8b5a2ee1-6055-422a-a666-b34ba28b76d4",
- image: "images/products/men-golf-polo-t-shirt-blue.jpg",
- name: "Men's Regular-Fit Quick-Dry Golf Polo Shirt",
- rating: {
- stars: 4.5,
- count: 2556,
- },
- priceCents: 1599,
- keywords: ["tshirts", "shirts", "apparel", "mens"],
- type: "clothing",
- sizeChartLink: "images/clothing-size-chart.png",
- },
- {
- id: "b86ddc8b-3501-4b17-9889-a3bad6fb585f",
- image: "images/products/trash-can-with-foot-pedal-50-liter.jpg",
- name: "Trash Can with Foot Pedal - Brushed Stainless Steel",
- rating: {
- stars: 4.5,
- count: 2286,
- },
- priceCents: 8300,
- keywords: ["garbage", "bins", "cans", "kitchen"],
- },
- {
- id: "19c6a64a-5463-4d45-9af8-e41140a4100c",
- image: "images/products/duvet-cover-set-blue-twin.jpg",
- name: "Duvet Cover Set with Zipper Closure",
- rating: {
- stars: 4,
- count: 456,
- },
- priceCents: 2399,
- keywords: ["bedroom", "bed sheets", "sheets", "covers", "home"],
- },
- {
- id: "d2785924-743d-49b3-8f03-ec258e640503",
- image: "images/products/women-chunky-beanie-gray.webp",
- name: "Women's Chunky Cable Beanie - Gray",
- rating: {
- stars: 5,
- count: 83,
- },
- priceCents: 1250,
- keywords: ["hats", "winter hats", "beanies", "tuques", "apparel", "womens"],
- },
- {
- id: "ee1f7c56-f977-40a4-9642-12ba5072e2b0",
- image: "images/products/men-chino-pants-beige.jpg",
- name: "Men's Classic-fit Pleated Chino Pants",
- rating: {
- stars: 4.5,
- count: 9017,
- },
- priceCents: 2290,
- keywords: ["pants", "apparel", "mens"],
- },
- {
- id: "1c079479-8586-494f-ab53-219325432536",
- image: "images/products/men-athletic-shoes-green.jpg",
- name: "Men's Athletic Sneaker",
- rating: {
- stars: 4,
- count: 229,
- },
- priceCents: 3890,
- keywords: ["shoes", "running shoes", "footwear", "mens"],
- },
- {
- id: "4df68c27-fd59-4a6a-bbd1-e754ddb6d53c",
- image: "images/products/men-navigator-sunglasses-brown.jpg",
- name: "Men's Navigator Sunglasses Pilot",
- rating: {
- stars: 3.5,
- count: 42,
- },
- priceCents: 1690,
- keywords: ["sunglasses", "glasses", "accessories", "shades"],
- },
- {
- id: "4e37dd03-3b23-4bc6-9ff8-44e112a92c64",
- image: "images/products/non-stick-cooking-set-15-pieces.webp",
- name: "Non-Stick Cookware Set, Pots, Pans and Utensils - 15 Pieces",
- rating: {
- stars: 4.5,
- count: 511,
- },
- priceCents: 6797,
- keywords: ["cooking set", "kitchen"],
- },
- {
- id: "a434b69f-1bc1-482d-9ce7-cd7f4a66ce8d",
- image: "images/products/vanity-mirror-silver.jpg",
- name: "Vanity Mirror with Heavy Base - Chrome",
- rating: {
- stars: 4.5,
- count: 130,
- },
- priceCents: 1649,
- keywords: ["bathroom", "washroom", "mirrors", "home"],
- },
- {
- id: "a45cfa0a-66d6-4dc7-9475-e2b01595f7d7",
- image: "images/products/women-french-terry-fleece-jogger-camo.jpg",
- name: "Women's Fleece Jogger Sweatpant",
- rating: {
- stars: 4.5,
- count: 248,
- },
- priceCents: 2400,
- keywords: ["pants", "sweatpants", "jogging", "apparel", "womens"],
- },
- {
- id: "d339adf3-e004-4c20-a120-40e8874c66cb",
- image: "images/products/double-elongated-twist-french-wire-earrings.webp",
- name: "Double Oval Twist French Wire Earrings - Gold",
- rating: {
- stars: 4.5,
- count: 117,
- },
- priceCents: 2400,
- keywords: ["accessories", "womens"],
- },
- {
- id: "d37a651a-d501-483b-aae6-a9659b0757a0",
- image: "images/products/round-airtight-food-storage-containers.jpg",
- name: "Round Airtight Food Storage Containers - 5 Piece",
- rating: {
- stars: 4,
- count: 126,
- },
- priceCents: 2899,
- keywords: ["boxes", "food containers", "kitchen"],
- },
- {
- id: "0d7f9afa-2efe-4fd9-b0fd-ba5663e0a524",
- image: "images/products/coffeemaker-with-glass-carafe-black.jpg",
- name: "Coffeemaker with Glass Carafe and Reusable Filter - 25 Oz, Black",
- rating: {
- stars: 4.5,
- count: 1211,
- },
- priceCents: 2250,
- keywords: ["coffeemakers", "kitchen", "appliances"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "02e3a47e-dd68-467e-9f71-8bf6f723fdae",
- image: "images/products/blackout-curtains-black.jpg",
- name: "Blackout Curtains Set 42 x 84-Inch - Black, 2 Panels",
- rating: {
- stars: 4.5,
- count: 363,
- },
- priceCents: 3099,
- keywords: ["bedroom", "home"],
- },
- {
- id: "8a53b080-6d40-4a65-ab26-b24ecf700bce",
- image: "images/products/cotton-bath-towels-teal.webp",
- name: "100% Cotton Bath Towels - 2 Pack, Light Teal",
- rating: {
- stars: 4.5,
- count: 93,
- },
- priceCents: 2110,
- keywords: ["bathroom", "home", "towels"],
- },
- {
- id: "10ed8504-57db-433c-b0a3-fc71a35c88a1",
- image: "images/products/knit-athletic-sneakers-pink.webp",
- name: "Waterproof Knit Athletic Sneakers - Pink",
- rating: {
- stars: 4,
- count: 89,
- },
- priceCents: 3390,
- keywords: ["shoes", "running shoes", "footwear", "womens"],
- },
- {
- id: "77a845b1-16ed-4eac-bdf9-5b591882113d",
- image: "images/products/countertop-blender-64-oz.jpg",
- name: "Countertop Blender - 64oz, 1400 Watts",
- rating: {
- stars: 4,
- count: 3,
- },
- priceCents: 10747,
- keywords: ["food blenders", "kitchen", "appliances"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "36c64692-677f-4f58-b5ec-0dc2cf109e27",
- image: "images/products/floral-mixing-bowl-set.jpg",
- name: "10-Piece Mixing Bowl Set with Lids - Floral",
- rating: {
- stars: 5,
- count: 679,
- },
- priceCents: 3899,
- keywords: ["mixing bowls", "baking", "cookware", "kitchen"],
- },
- {
- id: "aaa65ef3-8d6f-4eb3-bc9b-a6ea49047d8f",
- image: "images/products/kitchen-paper-towels-30-pack.jpg",
- name: "2-Ply Kitchen Paper Towels - 30 Pack",
- rating: {
- stars: 4.5,
- count: 1045,
- },
- priceCents: 5799,
- keywords: ["kitchen", "kitchen towels", "tissues"],
- },
- {
- id: "bc2847e9-5323-403f-b7cf-57fde044a955",
- image: "images/products/men-cozy-fleece-zip-up-hoodie-red.jpg",
- name: "Men's Full-Zip Hooded Fleece Sweatshirt",
- rating: {
- stars: 4.5,
- count: 3157,
- },
- priceCents: 2400,
- keywords: ["sweaters", "hoodies", "apparel", "mens"],
- },
- {
- id: "bc2847easd23",
- image: "images/products/box2_image.jpg",
- name: "Health & Presonal care",
- rating: {
- stars: 4.5,
- count: 357,
- },
- priceCents: 2100,
- keywords: ["clean", "presonal care", "mens"],
- },
- {
- id: "bc2847e9-5323-103f-b7cf-57fde044a955",
- image: "images/products/fit.jpg",
- name: "For your Fitness needs",
- rating: {
- stars: 4.5,
- count: 3157,
- },
- priceCents: 22400,
- keywords: ["sweaters", "hoodies", "apparel", "mens"],
- },
- {
- id: "adf343f-b7cf-57fde044a955",
- image: "images/products/gitter.jpg",
- name: "Creat with strip lights",
- rating: {
- stars: 4,
- count: 3157,
- },
- priceCents: 1500,
- keywords: ["light", "rgb", "gitter"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "bc2847e9-5023-403f-b7cf-57fde044a955",
- image: "images/products/phone.jpg",
- name: "S22-Ultra SUMSANG",
- rating: {
- stars: 5,
- count: 3157,
- },
- priceCents: 52400,
- keywords: ["sumsang", "phone", "ultra"],
- type: "appliances",
- instructionsLink: "images/appliance-instructions.png",
- warrantyLink: "images/appliance-warranty.png",
- },
- {
- id: "bc2847e9-5323-403f-b7cf-57fde044a955",
- image: "images/products/box3_image.jpg",
- name: "Table & Chair",
- rating: {
- stars: 3,
- count: 37,
- },
- priceCents: 22400,
- keywords: ["table", "chair"],
- },
-].map((productDetails) => {
- if (productDetails.type === "clothing") {
- return new Clothing(productDetails);
- } else if (productDetails.type === "appliances") {
- return new Appliances(productDetails);
- }
- return new Product(productDetails);
-});
+
+export let products = [];
+export function loadProducts(fun) {
+ const xhr = new XMLHttpRequest();
+ xhr.addEventListener("load", () => {
+ products = JSON.parse(xhr.response).map((productDetails) => {
+ if (productDetails.type === "clothing") {
+ return new Clothing(productDetails);
+ } else if (productDetails.type === "appliances") {
+ return new Appliances(productDetails);
+ }
+ return new Product(productDetails);
+ });
+ console.log("load Products");
+ fun();
+ });
+ xhr.open("GET", "https://supersimplebackend.dev/products");
+ xhr.send();
+}
+
From 696203b8317ff29c68ee8eb2f37227c7a5c0c6a4 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Sat, 11 Jan 2025 12:29:33 +0530
Subject: [PATCH 03/31] Refactor product loading to use Promises for better
error handling and asynchronous flow
---
public/js/amazon.js | 29 ++++++++++++++++++++++++++++-
public/js/checkout.js | 41 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 68 insertions(+), 2 deletions(-)
diff --git a/public/js/amazon.js b/public/js/amazon.js
index a1711c1..cc6a30a 100644
--- a/public/js/amazon.js
+++ b/public/js/amazon.js
@@ -1,7 +1,34 @@
import { cart } from "/js/cart-class.js";
import { products, loadProducts } from "/js/products.js";
-loadProducts(renderProductsGrid);
+
+// loadProducts(renderProductsGrid);
+
+
+// new Promise((resolve, reject) => {
+// loadProducts(() => {
+// resolve();
+// });
+// }).then(() => {
+// renderProductsGrid();
+// }).catch((error) => {
+// alert(error);
+// });
+
+
+initializeProducts();
+async function initializeProducts() {
+ try {
+ await new Promise((resolve, reject) => {
+ loadProducts(() => {
+ resolve();
+ });
+ });
+ renderProductsGrid();
+ } catch (error) {
+ alert(error);
+ }
+}
export function renderProductsGrid() {
let productsHTML = "";
products.forEach((product) => {
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 9b011aa..4d0963e 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,5 +1,44 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
import { loadProducts } from "./products.js";
-loadProducts(() => {
+//callbacks
+// loadProducts(() => {
+// renderOrderSummary();
+// });
+
+//promise
+// new Promise((resolve, reject) => {
+// loadProducts(() => {
+// resolve();
+// });
+// }).then(() => {
+// renderOrderSummary();
+// }).catch((error) => {
+// alert(error);
+// });
+
+Promise.all([
+ new Promise((resolve) => {
+ loadProducts(() => {
+ resolve('value1');
+ });
+}),
+new Promise((resolve) => {
renderOrderSummary();
+})
+]).then((values) => {
+ console.log(values);
});
+
+// initializeCart();
+// async function initializeCart() {
+// try {
+// await new Promise((resolve, reject) => {
+// loadProducts(() => {
+// resolve();
+// });
+// });
+// renderOrderSummary();
+// } catch (error) {
+// alert(error);
+// }
+// }
From 349e77e763a265d8dbe7ce83f506e50a0ef179c5 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Sun, 19 Jan 2025 19:22:47 +0530
Subject: [PATCH 04/31] Remove commented-out code for loading products and
initialize product loading
---
public/js/amazon.js | 16 ----------------
1 file changed, 16 deletions(-)
diff --git a/public/js/amazon.js b/public/js/amazon.js
index cc6a30a..40548ec 100644
--- a/public/js/amazon.js
+++ b/public/js/amazon.js
@@ -1,21 +1,5 @@
import { cart } from "/js/cart-class.js";
import { products, loadProducts } from "/js/products.js";
-
-
-// loadProducts(renderProductsGrid);
-
-
-// new Promise((resolve, reject) => {
-// loadProducts(() => {
-// resolve();
-// });
-// }).then(() => {
-// renderProductsGrid();
-// }).catch((error) => {
-// alert(error);
-// });
-
-
initializeProducts();
async function initializeProducts() {
try {
From d7d0c5fe78bfffefc3b74ad390d6f63fa32bcd36 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Sun, 19 Jan 2025 21:27:06 +0530
Subject: [PATCH 05/31] adjust the code to run
---
public/js/checkout.js | 36 +--
public/js/products.js | 681 ++++++++++++++++++++++++++++++++++++++++--
2 files changed, 672 insertions(+), 45 deletions(-)
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 4d0963e..86f5424 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -5,29 +5,21 @@ import { loadProducts } from "./products.js";
// renderOrderSummary();
// });
-//promise
-// new Promise((resolve, reject) => {
-// loadProducts(() => {
-// resolve();
-// });
-// }).then(() => {
-// renderOrderSummary();
-// }).catch((error) => {
-// alert(error);
-// });
-
-Promise.all([
- new Promise((resolve) => {
- loadProducts(() => {
- resolve('value1');
- });
-}),
-new Promise((resolve) => {
- renderOrderSummary();
+new Promise((resolve, reject) => {
+ try {
+ loadProducts(() => {
+ resolve();
+ });
+ } catch (error) {
+ reject("Failed to load products.");
+ }
})
-]).then((values) => {
- console.log(values);
-});
+ .then(() => {
+ renderOrderSummary();
+ })
+ .catch((error) => {
+ console.log(error);
+ });
// initializeCart();
// async function initializeCart() {
diff --git a/public/js/products.js b/public/js/products.js
index bbcc94b..c73979d 100644
--- a/public/js/products.js
+++ b/public/js/products.js
@@ -1,13 +1,16 @@
import { formatCurrency } from "/js/money.js";
-export function getProduct(productId) {
- let matchingProduct;
- products.forEach((product) => {
- if (product.id === productId) {
- matchingProduct = product;
- }
- });
- return matchingProduct;
-}
+// export function getProduct(productId) {
+// loadProducts(()=>{
+// let matchingProduct;
+// products.forEach((product) => {
+// if (product.id === productId) {
+// matchingProduct = product;
+// }
+// });
+// console.log(matchingProduct);
+// return matchingProduct;
+// });
+// }
class Product {
id;
image;
@@ -68,23 +71,655 @@ class Appliances extends Product {
`;
}
}
-
+const kaka = [
+ {
+ id: "e43608ze-6aa0-4b85-b27f-e1d07eb67z16",
+ image: "images/products/Chanakya Neeti.jpg",
+ name: "Chanakya Neeti with Chanakya Sutra Sahit",
+ rating: {
+ stars: 4.5,
+ count: 197,
+ },
+ priceCents: 1190,
+ keywords: ["book", "Chanakya Neeti", "sutra"],
+ },
+ {
+ id: "e43638ye-6aa0-4b85-b27f-e1d07eb67z16",
+ image: "images/products/sh.jpg",
+ name: "Shoonya Se Aakhir Tak ",
+ rating: {
+ stars: 4.5,
+ count: 197,
+ },
+ priceCents: 690,
+ keywords: ["book", "Shoonya Se Aakhir Tak ", "noval"],
+ },
+ {
+ id: "e43638ze-6aa0-4b85-b27f-e1d07eb67z16",
+ image: "images/products/pitaji.jpg",
+ name: "Pitaji Aur Tarikh",
+ rating: {
+ stars: 4.5,
+ count: 147,
+ },
+ priceCents: 790,
+ keywords: ["book", "father", "noval"],
+ },
+ {
+ id: "e43638ce-6aa0-4b85-b27f-e1d07eb67z16",
+ image: "images/products/gandhi.jpg",
+ name: "The Story of My Experiments With Truth: Mahatma Gandhi, An Autobiography (Hindi)",
+ rating: {
+ stars: 4.5,
+ count: 217,
+ },
+ priceCents: 990,
+ keywords: ["book", "gandhi", "autobiography"],
+ },
+ {
+ id: "e43638ce-6aa0-4b85-b27f-e1d07eb67zz6",
+ image: "images/products/yogi.jpg",
+ name: "Autobiography of A Yogi (Hindi)",
+ rating: {
+ stars: 4.5,
+ count: 117,
+ },
+ priceCents: 1090,
+ keywords: ["book", "yogi", "autobiography"],
+ },
+ {
+ id: "e43638ce-6aa0-4b85-b27f-e1d07eb678c6",
+ image: "images/products/athletic-cotton-socks-6-pairs.jpg",
+ name: "Black and Gray Athletic Cotton Socks - 6 Pairs",
+ rating: {
+ stars: 4.5,
+ count: 87,
+ },
+ priceCents: 1090,
+ keywords: ["socks", "sports", "apparel"],
+ },
+ {
+ id: "15b6fc6f-327a-4ec4-896f-486349e85a3d",
+ image: "images/products/intermediate-composite-basketball.jpg",
+ name: "Intermediate Size Basketball",
+ rating: {
+ stars: 4,
+ count: 127,
+ },
+ priceCents: 2095,
+ keywords: ["sports", "basketballs"],
+ },
+ {
+ id: "14rvbae-wef23",
+ image: "images/products/box1_image.jpg",
+ name: "Home & care",
+ rating: {
+ stars: 3,
+ count: 232,
+ },
+ priceCents: 3999,
+ keywords: [
+ "home",
+ // "decoration"
+ ],
+ },
+ {
+ id: "83d4ca15-0f35-48f5-b7a3-1ea210004f2e",
+ image: "images/products/adults-plain-cotton-tshirt-2-pack-teal.jpg",
+ name: "Adults Plain Cotton T-Shirt - 2 Pack",
+ rating: {
+ stars: 4.5,
+ count: 56,
+ },
+ priceCents: 799,
+ keywords: ["tshirts", "apparel", "mens"],
+ type: "clothing",
+ sizeChartLink: "images/clothing-size-chart.png",
+ },
+ {
+ id: "54e0eccd-8f36-462b-b68a-8182611d9add",
+ image: "images/products/black-2-slot-toaster.jpg",
+ name: "2 Slot Toaster - Black",
+ rating: {
+ stars: 5,
+ count: 2197,
+ },
+ priceCents: 1899,
+ keywords: ["toaster", "kitchen", "appliances"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "3ebe75dc-64d2-4137-8860-1f5a963e534b",
+ image: "images/products/6-piece-white-dinner-plate-set.jpg",
+ name: "6 Piece White Dinner Plate Set",
+ rating: {
+ stars: 4,
+ count: 37,
+ },
+ priceCents: 2067,
+ keywords: ["plates", "kitchen", "dining"],
+ },
+ {
+ id: "8c9c52b5-5a19-4bcb-a5d1-158a74287c53",
+ image: "images/products/6-piece-non-stick-baking-set.webp",
+ name: "6-Piece Nonstick, Carbon Steel Oven Bakeware Baking Set",
+ rating: {
+ stars: 4.5,
+ count: 175,
+ },
+ priceCents: 3499,
+ keywords: ["kitchen", "cookware"],
+ },
+ {
+ id: "dd82ca78-a18b-4e2a-9250-31e67412f98d",
+ image: "images/products/plain-hooded-fleece-sweatshirt-yellow.jpg",
+ name: "Plain Hooded Fleece Sweatshirt",
+ rating: {
+ stars: 4.5,
+ count: 317,
+ },
+ priceCents: 2400,
+ keywords: ["hoodies", "sweaters", "apparel"],
+ },
+ {
+ id: "77919bbe-0e56-475b-adde-4f24dfed3a04",
+ image: "images/products/luxury-tower-set-6-piece.jpg",
+ name: "Luxury Towel Set - Graphite Gray",
+ rating: {
+ stars: 4.5,
+ count: 144,
+ },
+ priceCents: 3599,
+ keywords: ["bathroom", "washroom", "restroom", "towels", "bath towels"],
+ },
+ {
+ id: "3fdfe8d6-9a15-4979-b459-585b0d0545b9",
+ image: "images/products/liquid-laundry-detergent-plain.jpg",
+ name: "Liquid Laundry Detergent, 110 Loads, 82.5 Fl Oz",
+ rating: {
+ stars: 4.5,
+ count: 305,
+ },
+ priceCents: 2899,
+ keywords: ["bathroom", "cleaning"],
+ },
+ {
+ id: "58b4fc92-e98c-42aa-8c55-b6b79996769a",
+ image: "images/products/knit-athletic-sneakers-gray.jpg",
+ name: "Waterproof Knit Athletic Sneakers - Gray",
+ rating: {
+ stars: 4,
+ count: 89,
+ },
+ priceCents: 3390,
+ keywords: ["shoes", "running shoes", "footwear"],
+ },
+ {
+ id: "5968897c-4d27-4872-89f6-5bcb052746d7",
+ image: "images/products/women-chiffon-beachwear-coverup-black.jpg",
+ name: "Women's Chiffon Beachwear Cover Up - Black",
+ rating: {
+ stars: 4.5,
+ count: 235,
+ },
+ priceCents: 2070,
+ keywords: ["robe", "swimsuit", "swimming", "bathing", "apparel"],
+ type: "clothing",
+ sizeChartLink: "images/clothing-size-chart.png",
+ },
+ {
+ id: "aad29d11-ea98-41ee-9285-b916638cac4a",
+ image: "images/products/round-sunglasses-black.jpg",
+ name: "Round Sunglasses",
+ rating: {
+ stars: 4.5,
+ count: 30,
+ },
+ priceCents: 1560,
+ keywords: ["accessories", "shades"],
+ },
+ {
+ id: "04701903-bc79-49c6-bc11-1af7e3651358",
+ image: "images/products/women-beach-sandals.jpg",
+ name: "Women's Two Strap Buckle Sandals - Tan",
+ rating: {
+ stars: 4.5,
+ count: 562,
+ },
+ priceCents: 2499,
+ keywords: ["footwear", "sandals", "womens", "beach", "summer"],
+ },
+ {
+ id: "901eb2ca-386d-432e-82f0-6fb1ee7bf969",
+ image: "images/products/blackout-curtain-set-beige.webp",
+ name: "Blackout Curtains Set 4-Pack - Beige",
+ rating: {
+ stars: 4.5,
+ count: 232,
+ },
+ priceCents: 4599,
+ keywords: ["bedroom", "curtains", "home"],
+ },
+ {
+ id: "82bb68d7-ebc9-476a-989c-c78a40ee5cd9",
+ image: "images/products/men-slim-fit-summer-shorts-gray.jpg",
+ name: "Men's Slim-Fit Summer Shorts",
+ rating: {
+ stars: 4,
+ count: 160,
+ },
+ priceCents: 1699,
+ keywords: ["shorts", "apparel", "mens"],
+ },
+ {
+ id: "c2a82c5e-aff4-435f-9975-517cfaba2ece",
+ image: "images/products/electric-glass-and-steel-hot-water-kettle.webp",
+ name: "Electric Glass and Steel Hot Tea Water Kettle - 1.7-Liter",
+ rating: {
+ stars: 5,
+ count: 846,
+ },
+ priceCents: 3074,
+ keywords: ["water boiler", "appliances", "kitchen"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "6b07d4e7-f540-454e-8a1e-363f25dbae7d",
+ image: "images/products/facial-tissue-2-ply-18-boxes.jpg",
+ name: "Ultra Soft Tissue 2-Ply - 18 Box",
+ rating: {
+ stars: 4,
+ count: 99,
+ },
+ priceCents: 2374,
+ keywords: ["kleenex", "tissues", "kitchen", "tissues box", "napkins"],
+ },
+ {
+ id: "a82c6bac-3067-4e68-a5ba-d827ac0be010",
+ image: "images/products/straw-sunhat.webp",
+ name: "Straw Lifeguard Sun Hat",
+ rating: {
+ stars: 4,
+ count: 215,
+ },
+ priceCents: 2200,
+ keywords: ["hats", "straw hats", "summer", "apparel"],
+ },
+ {
+ id: "e4f64a65-1377-42bc-89a5-e572d19252e2",
+ image: "images/products/sky-flower-stud-earrings.webp",
+ name: "Sterling Silver Sky Flower Stud Earrings",
+ rating: {
+ stars: 4.5,
+ count: 52,
+ },
+ priceCents: 1799,
+ keywords: ["jewelry", "accessories", "womens"],
+ },
+ {
+ id: "b0f17cc5-8b40-4ca5-9142-b61fe3d98c85",
+ image: "images/products/women-stretch-popover-hoodie-black.jpg",
+ name: "Women's Stretch Popover Hoodie",
+ rating: {
+ stars: 4.5,
+ count: 2465,
+ },
+ priceCents: 1374,
+ keywords: ["hooded", "hoodies", "sweaters", "womens", "apparel"],
+ type: "clothing",
+ sizeChartLink: "images/clothing-size-chart.png",
+ },
+ {
+ id: "a93a101d-79ef-4cf3-a6cf-6dbe532a1b4a",
+ image: "images/products/bathroom-rug.jpg",
+ name: "Bathroom Bath Rug Mat 20 x 31 Inch - Grey",
+ rating: {
+ stars: 4.5,
+ count: 119,
+ },
+ priceCents: 1250,
+ keywords: ["bathmat", "bathroom", "home"],
+ },
+ {
+ id: "4f4fbcc2-4e72-45cc-935c-9e13d79cc57f",
+ image: "images/products/women-knit-ballet-flat-black.jpg",
+ name: "Women's Knit Ballet Flat",
+ rating: {
+ stars: 4,
+ count: 326,
+ },
+ priceCents: 2640,
+ keywords: ["shoes", "flats", "womens", "footwear"],
+ },
+ {
+ id: "8b5a2ee1-6055-422a-a666-b34ba28b76d4",
+ image: "images/products/men-golf-polo-t-shirt-blue.jpg",
+ name: "Men's Regular-Fit Quick-Dry Golf Polo Shirt",
+ rating: {
+ stars: 4.5,
+ count: 2556,
+ },
+ priceCents: 1599,
+ keywords: ["tshirts", "shirts", "apparel", "mens"],
+ type: "clothing",
+ sizeChartLink: "images/clothing-size-chart.png",
+ },
+ {
+ id: "b86ddc8b-3501-4b17-9889-a3bad6fb585f",
+ image: "images/products/trash-can-with-foot-pedal-50-liter.jpg",
+ name: "Trash Can with Foot Pedal - Brushed Stainless Steel",
+ rating: {
+ stars: 4.5,
+ count: 2286,
+ },
+ priceCents: 8300,
+ keywords: ["garbage", "bins", "cans", "kitchen"],
+ },
+ {
+ id: "19c6a64a-5463-4d45-9af8-e41140a4100c",
+ image: "images/products/duvet-cover-set-blue-twin.jpg",
+ name: "Duvet Cover Set with Zipper Closure",
+ rating: {
+ stars: 4,
+ count: 456,
+ },
+ priceCents: 2399,
+ keywords: ["bedroom", "bed sheets", "sheets", "covers", "home"],
+ },
+ {
+ id: "d2785924-743d-49b3-8f03-ec258e640503",
+ image: "images/products/women-chunky-beanie-gray.webp",
+ name: "Women's Chunky Cable Beanie - Gray",
+ rating: {
+ stars: 5,
+ count: 83,
+ },
+ priceCents: 1250,
+ keywords: ["hats", "winter hats", "beanies", "tuques", "apparel", "womens"],
+ },
+ {
+ id: "ee1f7c56-f977-40a4-9642-12ba5072e2b0",
+ image: "images/products/men-chino-pants-beige.jpg",
+ name: "Men's Classic-fit Pleated Chino Pants",
+ rating: {
+ stars: 4.5,
+ count: 9017,
+ },
+ priceCents: 2290,
+ keywords: ["pants", "apparel", "mens"],
+ },
+ {
+ id: "1c079479-8586-494f-ab53-219325432536",
+ image: "images/products/men-athletic-shoes-green.jpg",
+ name: "Men's Athletic Sneaker",
+ rating: {
+ stars: 4,
+ count: 229,
+ },
+ priceCents: 3890,
+ keywords: ["shoes", "running shoes", "footwear", "mens"],
+ },
+ {
+ id: "4df68c27-fd59-4a6a-bbd1-e754ddb6d53c",
+ image: "images/products/men-navigator-sunglasses-brown.jpg",
+ name: "Men's Navigator Sunglasses Pilot",
+ rating: {
+ stars: 3.5,
+ count: 42,
+ },
+ priceCents: 1690,
+ keywords: ["sunglasses", "glasses", "accessories", "shades"],
+ },
+ {
+ id: "4e37dd03-3b23-4bc6-9ff8-44e112a92c64",
+ image: "images/products/non-stick-cooking-set-15-pieces.webp",
+ name: "Non-Stick Cookware Set, Pots, Pans and Utensils - 15 Pieces",
+ rating: {
+ stars: 4.5,
+ count: 511,
+ },
+ priceCents: 6797,
+ keywords: ["cooking set", "kitchen"],
+ },
+ {
+ id: "a434b69f-1bc1-482d-9ce7-cd7f4a66ce8d",
+ image: "images/products/vanity-mirror-silver.jpg",
+ name: "Vanity Mirror with Heavy Base - Chrome",
+ rating: {
+ stars: 4.5,
+ count: 130,
+ },
+ priceCents: 1649,
+ keywords: ["bathroom", "washroom", "mirrors", "home"],
+ },
+ {
+ id: "a45cfa0a-66d6-4dc7-9475-e2b01595f7d7",
+ image: "images/products/women-french-terry-fleece-jogger-camo.jpg",
+ name: "Women's Fleece Jogger Sweatpant",
+ rating: {
+ stars: 4.5,
+ count: 248,
+ },
+ priceCents: 2400,
+ keywords: ["pants", "sweatpants", "jogging", "apparel", "womens"],
+ },
+ {
+ id: "d339adf3-e004-4c20-a120-40e8874c66cb",
+ image: "images/products/double-elongated-twist-french-wire-earrings.webp",
+ name: "Double Oval Twist French Wire Earrings - Gold",
+ rating: {
+ stars: 4.5,
+ count: 117,
+ },
+ priceCents: 2400,
+ keywords: ["accessories", "womens"],
+ },
+ {
+ id: "d37a651a-d501-483b-aae6-a9659b0757a0",
+ image: "images/products/round-airtight-food-storage-containers.jpg",
+ name: "Round Airtight Food Storage Containers - 5 Piece",
+ rating: {
+ stars: 4,
+ count: 126,
+ },
+ priceCents: 2899,
+ keywords: ["boxes", "food containers", "kitchen"],
+ },
+ {
+ id: "0d7f9afa-2efe-4fd9-b0fd-ba5663e0a524",
+ image: "images/products/coffeemaker-with-glass-carafe-black.jpg",
+ name: "Coffeemaker with Glass Carafe and Reusable Filter - 25 Oz, Black",
+ rating: {
+ stars: 4.5,
+ count: 1211,
+ },
+ priceCents: 2250,
+ keywords: ["coffeemakers", "kitchen", "appliances"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "02e3a47e-dd68-467e-9f71-8bf6f723fdae",
+ image: "images/products/blackout-curtains-black.jpg",
+ name: "Blackout Curtains Set 42 x 84-Inch - Black, 2 Panels",
+ rating: {
+ stars: 4.5,
+ count: 363,
+ },
+ priceCents: 3099,
+ keywords: ["bedroom", "home"],
+ },
+ {
+ id: "8a53b080-6d40-4a65-ab26-b24ecf700bce",
+ image: "images/products/cotton-bath-towels-teal.webp",
+ name: "100% Cotton Bath Towels - 2 Pack, Light Teal",
+ rating: {
+ stars: 4.5,
+ count: 93,
+ },
+ priceCents: 2110,
+ keywords: ["bathroom", "home", "towels"],
+ },
+ {
+ id: "10ed8504-57db-433c-b0a3-fc71a35c88a1",
+ image: "images/products/knit-athletic-sneakers-pink.webp",
+ name: "Waterproof Knit Athletic Sneakers - Pink",
+ rating: {
+ stars: 4,
+ count: 89,
+ },
+ priceCents: 3390,
+ keywords: ["shoes", "running shoes", "footwear", "womens"],
+ },
+ {
+ id: "77a845b1-16ed-4eac-bdf9-5b591882113d",
+ image: "images/products/countertop-blender-64-oz.jpg",
+ name: "Countertop Blender - 64oz, 1400 Watts",
+ rating: {
+ stars: 4,
+ count: 3,
+ },
+ priceCents: 10747,
+ keywords: ["food blenders", "kitchen", "appliances"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "36c64692-677f-4f58-b5ec-0dc2cf109e27",
+ image: "images/products/floral-mixing-bowl-set.jpg",
+ name: "10-Piece Mixing Bowl Set with Lids - Floral",
+ rating: {
+ stars: 5,
+ count: 679,
+ },
+ priceCents: 3899,
+ keywords: ["mixing bowls", "baking", "cookware", "kitchen"],
+ },
+ {
+ id: "aaa65ef3-8d6f-4eb3-bc9b-a6ea49047d8f",
+ image: "images/products/kitchen-paper-towels-30-pack.jpg",
+ name: "2-Ply Kitchen Paper Towels - 30 Pack",
+ rating: {
+ stars: 4.5,
+ count: 1045,
+ },
+ priceCents: 5799,
+ keywords: ["kitchen", "kitchen towels", "tissues"],
+ },
+ {
+ id: "bc2847e9-5323-403f-b7cf-57fde044a955",
+ image: "images/products/men-cozy-fleece-zip-up-hoodie-red.jpg",
+ name: "Men's Full-Zip Hooded Fleece Sweatshirt",
+ rating: {
+ stars: 4.5,
+ count: 3157,
+ },
+ priceCents: 2400,
+ keywords: ["sweaters", "hoodies", "apparel", "mens"],
+ },
+ {
+ id: "bc2847easd23",
+ image: "images/products/box2_image.jpg",
+ name: "Health & Presonal care",
+ rating: {
+ stars: 4.5,
+ count: 357,
+ },
+ priceCents: 2100,
+ keywords: ["clean", "presonal care", "mens"],
+ },
+ {
+ id: "bc2847e9-5323-103f-b7cf-57fde044a955",
+ image: "images/products/fit.jpg",
+ name: "For your Fitness needs",
+ rating: {
+ stars: 4.5,
+ count: 3157,
+ },
+ priceCents: 22400,
+ keywords: ["sweaters", "hoodies", "apparel", "mens"],
+ },
+ {
+ id: "adf343f-b7cf-57fde044a955",
+ image: "images/products/gitter.jpg",
+ name: "Creat with strip lights",
+ rating: {
+ stars: 4,
+ count: 3157,
+ },
+ priceCents: 1500,
+ keywords: ["light", "rgb", "gitter"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "bc2847e9-5023-403f-b7cf-57fde044a955",
+ image: "images/products/phone.jpg",
+ name: "S22-Ultra SUMSANG",
+ rating: {
+ stars: 5,
+ count: 3157,
+ },
+ priceCents: 52400,
+ keywords: ["sumsang", "phone", "ultra"],
+ type: "appliances",
+ instructionsLink: "images/appliance-instructions.png",
+ warrantyLink: "images/appliance-warranty.png",
+ },
+ {
+ id: "bc2847e9-5323-403f-b7cf-57fde044a955",
+ image: "images/products/box3_image.jpg",
+ name: "Table & Chair",
+ rating: {
+ stars: 3,
+ count: 37,
+ },
+ priceCents: 22400,
+ keywords: ["table", "chair"],
+ },
+].map((productDetails) => {
+ if (productDetails.type === "clothing") {
+ return new Clothing(productDetails);
+ } else if (productDetails.type === "appliances") {
+ return new Appliances(productDetails);
+ }
+ return new Product(productDetails);
+});
+export function getProduct(productId) {
+ let matchingProduct;
+ kaka.forEach((product) => {
+ if (product.id === productId) {
+ matchingProduct = product;
+ }
+ });
+ return matchingProduct;
+}
export let products = [];
export function loadProducts(fun) {
- const xhr = new XMLHttpRequest();
- xhr.addEventListener("load", () => {
- products = JSON.parse(xhr.response).map((productDetails) => {
- if (productDetails.type === "clothing") {
- return new Clothing(productDetails);
- } else if (productDetails.type === "appliances") {
- return new Appliances(productDetails);
- }
- return new Product(productDetails);
+ return new Promise((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.addEventListener("load", () => {
+ products = JSON.parse(xhr.response).map((productDetails) => {
+ if (productDetails.type === "clothing") {
+ return new Clothing(productDetails);
+ } else if (productDetails.type === "appliances") {
+ return new Appliances(productDetails);
+ }
+ return new Product(productDetails);
+ });
+ console.log("load Products");
+ fun();
+ resolve();
});
- console.log("load Products");
- fun();
+ xhr.open("GET", "https://supersimplebackend.dev/products");
+ xhr.send();
});
- xhr.open("GET", "https://supersimplebackend.dev/products");
- xhr.send();
}
From 18213a0611b8c949fa88ba0f374633341ceef784 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Mon, 20 Jan 2025 12:28:25 +0530
Subject: [PATCH 06/31] Refactor checkout.js to use async/await for product
loading and order summary rendering
---
public/js/checkout.js | 55 ++++++++++++++++++++-----------------------
1 file changed, 25 insertions(+), 30 deletions(-)
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 86f5424..2f22012 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,36 +1,31 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
import { loadProducts } from "./products.js";
-//callbacks
-// loadProducts(() => {
-// renderOrderSummary();
-// });
-
-new Promise((resolve, reject) => {
- try {
- loadProducts(() => {
- resolve();
- });
- } catch (error) {
- reject("Failed to load products.");
- }
-})
- .then(() => {
- renderOrderSummary();
- })
- .catch((error) => {
- console.log(error);
- });
-
-// initializeCart();
-// async function initializeCart() {
+// new Promise((resolve, reject) => {
// try {
-// await new Promise((resolve, reject) => {
-// loadProducts(() => {
-// resolve();
-// });
+// loadProducts(() => {
+// resolve();
// });
-// renderOrderSummary();
// } catch (error) {
-// alert(error);
+// reject("Failed to load products.");
// }
-// }
+// })
+// .then(() => {
+// renderOrderSummary();
+// })
+// .catch((error) => {
+// console.log(error);
+// });
+
+ async function initializeCart() {
+ try {
+ await new Promise((resolve, reject) => {
+ loadProducts(() => {
+ resolve();
+ });
+ });
+ renderOrderSummary();
+ } catch (error) {
+ alert(error);
+ }
+}
+initializeCart();
\ No newline at end of file
From b35fdaab5219b90db15bbaff4234e11aef4f9495 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Mon, 20 Jan 2025 12:54:01 +0530
Subject: [PATCH 07/31] Convert loadProducts from XHR to fetch
---
public/js/amazon.js | 18 ++++--------------
public/js/checkout.js | 27 +++++----------------------
public/js/products.js | 23 +++++++++++------------
3 files changed, 20 insertions(+), 48 deletions(-)
diff --git a/public/js/amazon.js b/public/js/amazon.js
index 40548ec..83d0c5a 100644
--- a/public/js/amazon.js
+++ b/public/js/amazon.js
@@ -1,18 +1,8 @@
import { cart } from "/js/cart-class.js";
-import { products, loadProducts } from "/js/products.js";
-initializeProducts();
-async function initializeProducts() {
- try {
- await new Promise((resolve, reject) => {
- loadProducts(() => {
- resolve();
- });
- });
- renderProductsGrid();
- } catch (error) {
- alert(error);
- }
-}
+import { products, loadProductsFetch } from "/js/products.js";
+loadProductsFetch().then(() => {
+ renderProductsGrid();
+});
export function renderProductsGrid() {
let productsHTML = "";
products.forEach((product) => {
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 2f22012..12d31c7 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,27 +1,10 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
-import { loadProducts } from "./products.js";
-// new Promise((resolve, reject) => {
-// try {
-// loadProducts(() => {
-// resolve();
-// });
-// } catch (error) {
-// reject("Failed to load products.");
-// }
-// })
-// .then(() => {
-// renderOrderSummary();
-// })
-// .catch((error) => {
-// console.log(error);
-// });
-
- async function initializeCart() {
- try {
+import { loadProductsFetch } from "./products.js";
+async function initializeCart() {
+ try {
await new Promise((resolve, reject) => {
- loadProducts(() => {
- resolve();
- });
+ loadProductsFetch();
+ resolve();
});
renderOrderSummary();
} catch (error) {
diff --git a/public/js/products.js b/public/js/products.js
index c73979d..9f93060 100644
--- a/public/js/products.js
+++ b/public/js/products.js
@@ -702,11 +702,12 @@ export function getProduct(productId) {
return matchingProduct;
}
export let products = [];
-export function loadProducts(fun) {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.addEventListener("load", () => {
- products = JSON.parse(xhr.response).map((productDetails) => {
+
+export function loadProductsFetch() {
+ const Promise = fetch("https://supersimplebackend.dev/products")
+ .then((response) => response.json())
+ .then((data) => {
+ products = data.map((productDetails) => {
if (productDetails.type === "clothing") {
return new Clothing(productDetails);
} else if (productDetails.type === "appliances") {
@@ -714,12 +715,10 @@ export function loadProducts(fun) {
}
return new Product(productDetails);
});
- console.log("load Products");
- fun();
- resolve();
+ console.log("load Products");
+ })
+ .catch((error) => {
+ console.error("Error fetching data:", error);
});
- xhr.open("GET", "https://supersimplebackend.dev/products");
- xhr.send();
- });
+ return Promise;
}
-
From d50ed18fde1920ee7106e6088f10b50d97511e54 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Mon, 20 Jan 2025 13:06:13 +0530
Subject: [PATCH 08/31] Convert from promises to async await
---
public/js/checkout.js | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/public/js/checkout.js b/public/js/checkout.js
index 12d31c7..2ffc6bb 100644
--- a/public/js/checkout.js
+++ b/public/js/checkout.js
@@ -1,14 +1,11 @@
import { renderOrderSummary } from "./checkout/orderSummary.js";
import { loadProductsFetch } from "./products.js";
-async function initializeCart() {
+initializeCheckout();
+async function initializeCheckout() {
try {
- await new Promise((resolve, reject) => {
- loadProductsFetch();
- resolve();
- });
+ await loadProductsFetch();
renderOrderSummary();
} catch (error) {
alert(error);
}
-}
-initializeCart();
\ No newline at end of file
+}
\ No newline at end of file
From db8c3ce572b6854c26cab403a17f7de3f0160d92 Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Mon, 20 Jan 2025 13:14:38 +0530
Subject: [PATCH 09/31] Learn error handling
---
public/js/products.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/public/js/products.js b/public/js/products.js
index 9f93060..d2653b9 100644
--- a/public/js/products.js
+++ b/public/js/products.js
@@ -718,7 +718,7 @@ export function loadProductsFetch() {
console.log("load Products");
})
.catch((error) => {
- console.error("Error fetching data:", error);
+ alert("Error fetching data:", error);
});
return Promise;
}
From 41c3d1856cc8dd12c01e5493751996ceb9947f8c Mon Sep 17 00:00:00 2001
From: vikrant-vikrant <123059176+vikrant-vikrant@users.noreply.github.com>
Date: Mon, 20 Jan 2025 21:01:43 +0530
Subject: [PATCH 10/31] Create order using the backend
---
public/js/checkout/paymentSummery.js | 24 +++++++++++++++++++++++-
public/js/order.js | 10 ++++++++++
views/listings/checkout.ejs | 2 +-
3 files changed, 34 insertions(+), 2 deletions(-)
create mode 100644 public/js/order.js
diff --git a/public/js/checkout/paymentSummery.js b/public/js/checkout/paymentSummery.js
index 12064fa..a188f89 100644
--- a/public/js/checkout/paymentSummery.js
+++ b/public/js/checkout/paymentSummery.js
@@ -2,7 +2,7 @@ import { cart } from "/js/cart-class.js";
import { getProduct } from "/js/products.js";
import { getDeliveryOption } from "/js/deliveryOptions.js";
import { formatCurrency } from "/js/money.js";
-
+import { addOrder } from "../order.js";
export function renderPaymentSummary() {
let productPriceCents = 0;
let shippingPriceCents = 0;
@@ -16,3 +16,25 @@ export function renderPaymentSummary() {
const totalPayment = formatCurrency(productPriceCents + shippingPriceCents);
return { productPriceCents, shippingPrice, totalPayment };
}
+document
+ .querySelector(".js-place-order")
+ .addEventListener("click", async () => {
+ try {
+ const response = await fetch("https://supersimplebackend.dev/orders", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ cart: cart,
+ }),
+ });
+
+ const order = await response.json();
+ addOrder(order);
+ } catch (error) {
+ console.log(`Unexpected error, Try again later`,error);
+ alert(error);
+ }
+ // window.location.href = 'orders.html';
+ });
diff --git a/public/js/order.js b/public/js/order.js
new file mode 100644
index 0000000..dacf3e5
--- /dev/null
+++ b/public/js/order.js
@@ -0,0 +1,10 @@
+
+export const order = JSON.parse(localStorage.getItem('orders')) || [];
+export function addOrder(order){
+ // order.unshift(order);
+ saveToStroage();
+}
+
+function saveToStroage(){
+ localStorage.setItem('orders', JSON.stringify(order));
+}
\ No newline at end of file
diff --git a/views/listings/checkout.ejs b/views/listings/checkout.ejs
index f9c46cd..cd1322b 100644
--- a/views/listings/checkout.ejs
+++ b/views/listings/checkout.ejs
@@ -48,7 +48,7 @@
-