Skip to content
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

Individual item name and price are overwritten on each new addition. #5

Closed
rees8 opened this issue Oct 1, 2024 · 8 comments
Closed
Assignees

Comments

@rees8
Copy link
Contributor

rees8 commented Oct 1, 2024

Problem: On adding a new item, the item details are being overwritten each time in the menu.js file.
Solution: We can make use of an array to store each item as it is being added into the cart so as to not lose the item list.

@Anjaliavv51 I would like to work on this, would be grateful if you could assign it to me.

@1-SubhamSingh
Copy link

Hey there ,
I am interested to fix this , can you assign me this task.

@rees8
Copy link
Contributor Author

rees8 commented Oct 1, 2024

@1-SubhamSingh I am also a contributor, this was actually an issue found by me and I wanted to ask the project owner to assign this to me.

@1-SubhamSingh
Copy link

@rees8 Ohh !! fine

@Anjaliavv51
Copy link
Owner

@rees8 please add snippet of code in comment.

@rees8
Copy link
Contributor Author

rees8 commented Oct 1, 2024

in menu.js:
Original code:
if (event.target.classList.contains('butt')) {
var item = event.target.closest('.items');
var itemName = item.querySelector('h3').textContent;
var itemPrice = item.querySelector('p').textContent;
alert("Item added to cart successfully");

            localStorage.setItem('itemName', itemName);
            localStorage.setItem('itemPrice', itemPrice);

            window.location.href = "cart.html";
        }

Modified version:
if (event.target.classList.contains('butt')) {
var item = event.target.closest('.items');
var itemName = item.querySelector('h3').textContent;
var itemPrice = item.querySelector('p').textContent;

            alert("Item added to cart successfully");

            // Retrieve existing cart items from localStorage
            var cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];

            // Add new item to the cart array
            var newItem = { name: itemName, price: itemPrice };
            cartItems.push(newItem);

            // Save updated cart back to localStorage
            localStorage.setItem('cartItems', JSON.stringify(cartItems));

            window.location.href = "cart.html";
        }

The ultimate functionality remains the same as the cart.js file is managing the process, but this small change should streamline the item addition process and is also better from the code readability point of view.

@Anjaliavv51
Copy link
Owner

@rees8 go ahead

@rees8
Copy link
Contributor Author

rees8 commented Oct 1, 2024

Thank you. A small request though: Could you also add the hacktoberfest label to this if it is applicable? It was mentioned in the meet to remind the project owner if the tag was missing.

@Anjaliavv51
Copy link
Owner

OKAY
@rees8 please check the pin issue #11 before starting to contribute.

Anjaliavv51 added a commit that referenced this issue Oct 1, 2024
Modified the existing code to store the saved items in an array to
streamline the process @Anjaliavv51
#5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants