-
Notifications
You must be signed in to change notification settings - Fork 32
Durga js fetch api debugging #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
|
|
||
| * { | ||
| margin: 0; | ||
| padding: 0; | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| body { | ||
| font-family: Arial, sans-serif; | ||
| line-height: 1.6; | ||
| } | ||
|
|
||
| header { | ||
| background: #0d0a99; | ||
| color: #fff; | ||
| padding: 20px; | ||
| text-align: center; | ||
| } | ||
|
|
||
| nav ul { | ||
| list-style: none; | ||
| display: flex; | ||
| justify-content: center; | ||
| gap: 20px; | ||
| margin-top: 10px; | ||
| } | ||
|
|
||
| nav a { | ||
| color: #fff; | ||
| text-decoration: none; | ||
| } | ||
|
|
||
| main { | ||
| display: flex; | ||
| padding: 20px; | ||
| gap: 20px; | ||
| } | ||
|
|
||
| article { | ||
| flex: 3; | ||
| background: #2c13a8; | ||
| color: #fff; | ||
| padding: 20px; | ||
| border-radius: 8px; | ||
| } | ||
|
|
||
| aside { | ||
| flex: 1; | ||
| background: #4d119b; | ||
| color: #fff; | ||
| padding: 20px; | ||
| border-radius: 8px; | ||
| } | ||
|
|
||
| footer { | ||
| background: #3f12ba; | ||
| color: #fff; | ||
| text-align: center; | ||
| padding: 15px; | ||
| margin-top: 20px; | ||
| bottom: auto; | ||
| } | ||
|
|
||
| @media (max-width: 768px) { | ||
| main { | ||
| flex-direction: column; | ||
| flex:1; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>CSS Animation & Media Queries</title> | ||
| <style> | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| margin: 0; | ||
| padding: 0; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| height: 100vh; | ||
| background: #f0f0f0; | ||
| } | ||
| .box { | ||
| width: 150px; | ||
| height: 150px; | ||
| background: #3498db; | ||
| border-radius: 10px; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| color: white; | ||
| font-weight: bold; | ||
| animation: bounce 2s infinite; | ||
| } | ||
| @keyframes bounce { | ||
| 0%, 100% { | ||
| transform: translateY(0); | ||
| } | ||
| 50% { | ||
| transform: translateY(-50px); | ||
| } | ||
| } | ||
| @media (max-width: 768px) { | ||
| .box { | ||
| width: 120px; | ||
| height: 120px; | ||
| font-size: 14px; | ||
| background: #e67e22; | ||
| } | ||
| } | ||
|
|
||
| @media (max-width: 480px) { | ||
| .box { | ||
| width: 90px; | ||
| height: 90px; | ||
| font-size: 12px; | ||
| background: #2ecc71; | ||
| } | ||
| } | ||
| </style> | ||
|
Comment on lines
+7
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| </head> | ||
| <body> | ||
| <div class="box">Hello Guys..!</div> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>E-Commerce App</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| </head> | ||
| <body> | ||
| <h1>🛍️ Simple E-Commerce App</h1> | ||
|
|
||
| <div class="search-box"> | ||
| <input type="text" id="searchInput" placeholder="Search for products..." /> | ||
| <button id="searchBtn">Search</button> | ||
| <button id="allBtn">All Products</button> | ||
| </div> | ||
|
|
||
| <div id="productContainer" class="product-container"> | ||
| </div> | ||
|
|
||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||
| <!DOCTYPE html> | ||||||
| <html lang="en"> | ||||||
| <head> | ||||||
| <meta charset="UTF-8"> | ||||||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||
| <title>Product Details</title> | ||||||
| <link rel="stylesheet" href="style.css"> | ||||||
| </head> | ||||||
| <body> | ||||||
| <h1>🛒 Product Details</h1> | ||||||
| <div id="productDetails" class="product-details"> | ||||||
| <!-- Product details will load here --> | ||||||
| </div> | ||||||
|
|
||||||
| <script> | ||||||
| async function loadProductDetails() { | ||||||
| const params = new URLSearchParams(window.location.search); | ||||||
| const id = params.get("id"); | ||||||
|
|
||||||
| try { | ||||||
| const res = await fetch(`https://dummyjson.com/products/${id}`); | ||||||
| if (!res.ok) throw new Error("Failed to fetch product details"); | ||||||
| const product = await res.json(); | ||||||
|
|
||||||
| const container = document.getElementById("productDetails"); | ||||||
| container.innerHTML = ` | ||||||
| <img src="${product.thumbnail}" alt="${product.title}"> | ||||||
| <h2>${product.title}</h2> | ||||||
| <p>${product.description}</p> | ||||||
| <p><strong>Price:</strong> $${product.price}</p> | ||||||
| <p><strong>Brand:</strong> ${product.brand}</p> | ||||||
| <p><strong>Category:</strong> ${product.category}</p> | ||||||
| <button onclick="window.history.back()">⬅️ Go Back</button> | ||||||
| `; | ||||||
|
Comment on lines
+26
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||||||
| } catch (error) { | ||||||
| document.getElementById("productDetails").innerHTML = `<p>Error: ${error.message}</p>`; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| loadProductDetails(); | ||||||
| </script> | ||||||
|
Comment on lines
+15
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| </body> | ||||||
| </html> | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||
| const productContainer = document.getElementById("productContainer"); | ||||||
| const searchInput = document.getElementById("searchInput"); | ||||||
| const searchBtn = document.getElementById("searchBtn"); | ||||||
| const allBtn = document.getElementById("allBtn"); | ||||||
| async function fetchAllProducts() { | ||||||
| try { | ||||||
| const res = await fetch("https://dummyjson.com/products"); | ||||||
| if (!res.ok) throw new Error("Failed to fetch products"); | ||||||
| const data = await res.json(); | ||||||
| displayProducts(data.products); | ||||||
| } catch (error) { | ||||||
| productContainer.innerHTML = `<p>Error: ${error.message}</p>`; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| async function searchProducts(query) { | ||||||
| try { | ||||||
| const res = await fetch(`https://dummyjson.com/products/search?q=${query}`); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The search query should be properly encoded before being included in a URL to handle special characters (e.g.,
Suggested change
|
||||||
| if (!res.ok) throw new Error("Search failed"); | ||||||
| const data = await res.json(); | ||||||
| displayProducts(data.products); | ||||||
| } catch (error) { | ||||||
| productContainer.innerHTML = `<p>Error: ${error.message}</p>`; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function displayProducts(products) { | ||||||
| if (products.length === 0) { | ||||||
| productContainer.innerHTML = `<p>No products found</p>`; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| productContainer.innerHTML = products | ||||||
| .map( | ||||||
| (p) => ` | ||||||
| <div class="product-card"> | ||||||
| <img src="${p.thumbnail}" alt="${p.title}"> | ||||||
| <h3>${p.title}</h3> | ||||||
| <p>$${p.price}</p> | ||||||
| <button onclick="viewProduct(${p.id})">View Details</button> | ||||||
| </div> | ||||||
| ` | ||||||
| ) | ||||||
| .join(""); | ||||||
|
Comment on lines
+33
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||||||
| } | ||||||
|
|
||||||
| function viewProduct(id) { | ||||||
| window.location.href = `product.html?id=${id}`; | ||||||
| } | ||||||
|
|
||||||
| searchBtn.addEventListener("click", () => { | ||||||
| const query = searchInput.value.trim(); | ||||||
| if (query) searchProducts(query); | ||||||
| else alert("Please enter something to search!"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| }); | ||||||
|
|
||||||
| allBtn.addEventListener("click", fetchAllProducts); | ||||||
|
|
||||||
| fetchAllProducts(); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| background: #f7f9fb; | ||
| margin: 20px; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .search-box { | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| input { | ||
| padding: 8px; | ||
| width: 250px; | ||
| border-radius: 5px; | ||
| border: 1px solid #ccc; | ||
| } | ||
|
|
||
| button { | ||
| padding: 8px 15px; | ||
| margin: 5px; | ||
| border: none; | ||
| background: #007bff; | ||
| color: white; | ||
| border-radius: 5px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| button:hover { | ||
| background: #0056b3; | ||
| } | ||
|
|
||
| .product-container { | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| justify-content: center; | ||
| gap: 15px; | ||
| } | ||
|
|
||
| .product-card { | ||
| background: white; | ||
| border-radius: 10px; | ||
| width: 200px; | ||
| padding: 15px; | ||
| box-shadow: 0 2px 5px rgba(0,0,0,0.1); | ||
| transition: 0.3s; | ||
| } | ||
|
|
||
| .product-card:hover { | ||
| transform: translateY(-5px); | ||
| } | ||
|
|
||
| .product-card img { | ||
| width: 100%; | ||
| border-radius: 8px; | ||
| } | ||
|
|
||
| .product-details { | ||
| max-width: 600px; | ||
| margin: 0 auto; | ||
| text-align: left; | ||
| background: white; | ||
| padding: 20px; | ||
| border-radius: 10px; | ||
| } | ||
|
|
||
| .product-details img { | ||
| width: 100%; | ||
| border-radius: 8px; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
flex: 1property here has no effect.mainis a flex container, but it is not a flex item of another flex container (its parent,body, does not havedisplay: flexby default). You can safely remove this line.