-
Notifications
You must be signed in to change notification settings - Fork 0
Driverjar team2 #14
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
Merged
Merged
Driverjar team2 #14
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
158d709
Fix: resolves the cart localStorage overwrite issue.
b6cddfb
index created
53553a2
.mjs created
82ba68f
Add getParam function to retrieve URL query parameters
itusebastian 1e45d8d
Add product ID retrieval from URL in product.js
itusebastian 962b9ea
Update product link to use query parameter for product ID in index.html
itusebastian 364b8c9
Add test block to log extracted product ID and fetched product data
itusebastian 474aa86
Update product links to use query parameters for product IDs in index…
itusebastian 6ab625d
Implement ProductDetails class with methods for fetching and displayi…
itusebastian a233412
Refactor product.js to streamline imports and initialize ProductDetails
itusebastian e3d96dc
Refactor ProductDetails class to improve method documentation and enh…
itusebastian eb93723
Remove product details section from index.html
itusebastian c0f003e
fix(cart): restore quantity increment logic on duplicate item add
itusebastian b76f814
refactor(ProductDetails): clean up addToCart method by removing redun…
itusebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { setLocalStorage, getLocalStorage } from './utils.mjs'; | ||
|
|
||
| export default class ProductDetails { | ||
| constructor(productId, dataSource) { | ||
| this.productId = productId; | ||
| this.dataSource = dataSource; | ||
| this.product = {}; // Will hold the product object once fetched | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the product details page by fetching data, | ||
| * rendering the HTML, and attaching the cart event listener. | ||
| */ | ||
| async init() { | ||
| // 1. Use the datasource to get the details for the current product. | ||
| this.product = await this.dataSource.findProductById(this.productId); | ||
|
|
||
| // 2. Render the HTML details onto the page | ||
| this.renderProductDetails('main.product-detail'); | ||
|
|
||
| // 3. Add a listener to the Add to Cart button using .bind(this) | ||
| // so 'this' inside addToCart refers to this class instance. | ||
| document | ||
| .getElementById('addToCart') | ||
| .addEventListener('click', this.addToCart.bind(this)); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the currently viewed product to the localStorage cart. | ||
| */ | ||
| addToCart() { | ||
| // 1. Retrieve existing cart or initialize an empty array if it doesn't exist | ||
| const cartItems = getLocalStorage('so-cart') || []; | ||
|
|
||
| // 2. Check if the product already exists in the cart by ID | ||
| const existingItem = cartItems.find(item => item.Id === this.product.Id); | ||
|
|
||
| if (existingItem) { | ||
| // If it exists, increment the quantity | ||
| existingItem.Quantity = (existingItem.Quantity || 1) + 1; | ||
| } else { | ||
| // If it's a new item, initialize quantity to 1 and push it | ||
| this.product.Quantity = 1; | ||
| cartItems.push(this.product); | ||
| } | ||
|
|
||
| // 3. Save the updated array back to localStorage | ||
| setLocalStorage('so-cart', cartItems); | ||
|
|
||
| // Visual feedback for the user | ||
| alert(`${this.product.NameWithoutBrand} added to cart!`); | ||
| } | ||
|
|
||
| /** | ||
| * Generates the HTML structure for the product details and injects it into the DOM. | ||
| * @param {string} selector - The CSS selector of the container element (e.g., 'main.product-detail') | ||
| */ | ||
| renderProductDetails(selector) { | ||
| const element = document.querySelector(selector); | ||
| if (!element) return; | ||
|
|
||
| // Generate the internal HTML based on the product data structures | ||
| element.innerHTML = ` | ||
| <h3>${this.product.Brand.Name}</h3> | ||
| <h2 class="divider">${this.product.NameWithoutBrand}</h2> | ||
| <img | ||
| class="divider" | ||
| src="${this.product.Image}" | ||
| alt="${this.product.NameWithoutBrand}" | ||
| /> | ||
| <p class="product-card__price">$${this.product.FinalPrice}</p> | ||
| <p class="product__color">${this.product.Colors[0].ColorName}</p> | ||
| <p class="product__description"> | ||
| ${this.product.DescriptionHtmlSimple} | ||
| </p> | ||
| <div class="product-detail__add"> | ||
| <button id="addToCart" data-id="${this.product.Id}">Add to Cart</button> | ||
| </div> | ||
| `; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,9 @@ | ||
| import { setLocalStorage, getLocalStorage } from './utils.mjs'; | ||
| import { getParam } from './utils.mjs'; | ||
| import ProductData from './ProductData.mjs'; | ||
| import ProductDetails from './ProductDetails.mjs'; | ||
|
|
||
| const productId = getParam('product'); | ||
| const dataSource = new ProductData('tents'); | ||
|
|
||
| // Retrieves the existing cart array, checks for duplicates, and updates local storage | ||
| function addProductToCart(product) { | ||
| const currentCart = getLocalStorage('so-cart') || []; | ||
| const existingItem = currentCart.find(item => item.Id === product.Id); | ||
|
|
||
| if (existingItem) { | ||
| existingItem.Quantity = (existingItem.Quantity || 1) + 1; | ||
| } else { | ||
| product.Quantity = 1; | ||
| currentCart.push(product); | ||
| } | ||
|
|
||
| setLocalStorage('so-cart', currentCart); | ||
| } | ||
|
|
||
| // add to cart button event handler | ||
| async function addToCartHandler(e) { | ||
| const product = await dataSource.findProductById(e.target.dataset.id); | ||
| addProductToCart(product); | ||
| } | ||
|
|
||
| // add listener to Add to Cart button | ||
| document | ||
| .getElementById('addToCart') | ||
| .addEventListener('click', addToCartHandler); | ||
| const product = new ProductDetails(productId, dataSource); | ||
| product.init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <!doctype html> | ||
|
|
||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
|
|
||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
|
|
||
| <title>Sleep Outside | Cedar Ridge Rimrock 2-person tent</title> | ||
|
|
||
| <link rel="stylesheet" href="../css/style.css" /> | ||
|
|
||
| <script src="../js/product.js" type="module"></script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <header class="divider"> | ||
| <div class="logo"> | ||
| <img src="/images/noun_Tent_2517.svg" alt="tent image for logo" /> | ||
| <a href="../index.html"> Sleep<span class="highlight">Outside</span></a> | ||
| </div> | ||
|
|
||
| <div class="cart"> | ||
| <a href="../cart/index.html"> | ||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> | ||
| <path | ||
| d="M18.9 32.6c1.1 2.4 2.5 3.3 5.4 3.3 1.6 0 3.6-0.3 5.9-0.6 3.2-0.5 6.9-1 11.2-1 2.1 0 4.3 0.1 6.4 0.3 2.1 0.1 4.2 0.3 6.1 0.3 3.2 0 5.2-0.4 5.9-1.2 2.7-2.7 2.8-8.8 2.9-14.6 0.1-6.7 0.2-14.5 4.6-18.7 -0.5 0-1 0-1.6 0 -14.2 0-37.5 0-41.1 0C15.6 6.2 14.9 23.6 18.9 32.6z" | ||
| /> | ||
|
|
||
| <path | ||
| d="M90.1 29.7c1-3.3 1.5-7.3 1.5-11.2 0-9-2.7-18.8-8.6-18.8 -0.1 0-0.2 0-0.3 0L77.8-0.1c-0.3 0.2-0.8 0.3-1.1 0.4 0 0 0 0 0 0 -0.2 0-0.3 0-0.4 0 -4.5 0.1-7 1.8-8.4 4.9l8.9-0.1c-1.6 3.6-2.4 8.7-2.4 13.5 0 4.9 0.8 9.9 2.5 13.6l-12.3 0c-0.2 0.4-0.4 0.8-0.6 1.2 -0.2 0.4-0.4 0.7-0.6 1.1 -0.1 0.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-2.5-0.4-5.9-0.4 -4.9 0-8.6 0.4-9.5 0.7 -0.3 0.5-0.5 1.9-0.5 3.3C52.5 68.1 56 69 58.8 68.9z" | ||
| /> | ||
|
|
||
| <path | ||
| d="M24.3 68.4c2.9-0.1 6.4-0.9 8.3-1.4 0.1-0.8 0.3-2.8-0.7-3.5 -0.5-0.2-2.5-0.4-5.9-0.4 -4.9 0-8.6 0.4-9.5 0.7 -0.3 0.5-0.5 1.9-0.5 3.3C18 67.7 21.5 68.6 24.3 68.4z" | ||
| /> | ||
|
|
||
| <path | ||
| d="M60.1 71.4v3.3h-5.2v-3.4c-1.7-0.3-3.3-0.7-4.6-1 -0.9 6.8-1.1 13.3-0.3 14.5 0.4 0.3 2.9 1.1 8 1.1h0c5 0 8.8-0.7 9.7-1.3 0.8-1.3 0.6-7.7-0.4-14.4C65.5 70.5 62.7 71.1 60.1 71.4z" | ||
| /> | ||
|
|
||
| <!-- <text x="0" y="115" fill="#000000" font-size="5px" font-weight="bold" font-family="'Helvetica Neue', Helvetica, Arial-Unicode, Arial, Sans-serif">Created by Natalia Woodroffe</text> | ||
| <text x="0" y="120" fill="#000000" font-size="5px" font-weight="bold" font-family="'Helvetica Neue', Helvetica, Arial-Unicode, Arial, Sans-serif">from the Noun Project</text> --> | ||
| </svg> | ||
| </a> | ||
| </div> | ||
| </header> | ||
|
|
||
| <main class="product-detail divider"> | ||
| </main> | ||
|
|
||
| <footer>©NOT a real business</footer> | ||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.