Skip to content

Commit c022008

Browse files
authored
Video-16-Add-To-Cart-Action (basir#16)
1 parent c619a29 commit c022008

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,44 @@ JS AMAZONA
124124
6. style .details and all columns
125125
7. create add to cart button with add-button id
126126
15. Product Screen Action
127+
127128
1. after_render() to add event to the button
128129
2. add event handler for the button
129130
3. redirect user to cart/:product_id
130131
4. implement after_render in index.js
132+
133+
16. Add To Cart Action
134+
1. create CartScreen.js
135+
2. parseRequestUrl
136+
3. getProduct(request.id)
137+
4. addToCart
138+
5. getCartItems
139+
6. cartItems.find
140+
7. if existItem update qty
141+
8. else add item
142+
9. setCartItems
143+
17. Cart Screen UI
144+
1. cartItems = getCartItems()
145+
2. create 2 columns for cart items and cart action
146+
3. cartItems.length === 0 ? cart is empty
147+
4. show item image, name, qty and price
148+
5. cart action
149+
6. Subtotal
150+
7. Proceed to Checkout button
151+
8. Add CSS Style
152+
18. Update and Delete Cart Items
153+
1. add qty select next to each item
154+
2. after_render()
155+
3. add change event to qty select
156+
4. getCartItems() and pass to addToCart()
157+
5. set force to true to addToCart()
158+
6. create rerender() as (component, areaName = 'content')
159+
7. component.render and component.after_render
160+
8. if force is true then rerender()
161+
9. add delete button next to each item
162+
10. add click event to qty button
163+
11. call removeFromCart(deleteButton.id)
164+
12. implement removeFromCart(id)
165+
13. setCartItems( getCartItems().filter)
166+
14. if id === parseRequestUrl().id? redirect to '/cart'
167+
15. else rerender(CartScreen);

frontend/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import HomeScreen from './srceens/HomeScreen';
22
import ProductScreen from './srceens/ProductScreen';
33
import { parseRequestUrl } from './utils';
44
import Error404Screen from './srceens/Error404Screen';
5+
import CartScreen from './srceens/CartScreen';
56

67
const routes = {
78
'/': HomeScreen,
89
'/product/:id': ProductScreen,
10+
'/cart/:id': CartScreen,
11+
'/cart': CartScreen,
912
};
1013
const router = async () => {
1114
const request = parseRequestUrl();

frontend/src/localStorage.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const getCartItems = () => {
2+
const cartItems = localStorage.getItem('cartItems')
3+
? JSON.parse(localStorage.getItem('cartItems'))
4+
: [];
5+
return cartItems;
6+
};
7+
export const setCartItems = (cartItems) => {
8+
localStorage.setItem('cartItems', JSON.stringify(cartItems));
9+
};

frontend/src/srceens/CartScreen.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { parseRequestUrl } from '../utils';
2+
import { getProduct } from '../api';
3+
import { getCartItems, setCartItems } from '../localStorage';
4+
5+
const addToCart = (item, forceUpdate = false) => {
6+
let cartItems = getCartItems();
7+
const existItem = cartItems.find((x) => x.product === item.product);
8+
if (existItem) {
9+
cartItems = cartItems.map((x) =>
10+
x.product === existItem.product ? item : x
11+
);
12+
} else {
13+
cartItems = [...cartItems, item];
14+
}
15+
setCartItems(cartItems);
16+
};
17+
18+
const CartScreen = {
19+
after_render: () => {},
20+
render: async () => {
21+
const request = parseRequestUrl();
22+
if (request.id) {
23+
const product = await getProduct(request.id);
24+
addToCart({
25+
product: product._id,
26+
name: product.name,
27+
image: product.image,
28+
price: product.price,
29+
countInStock: product.countInStock,
30+
qty: 1,
31+
});
32+
}
33+
return `<div>Cart Screen</div>
34+
<div>${getCartItems().length}</div>
35+
`;
36+
},
37+
};
38+
39+
export default CartScreen;

0 commit comments

Comments
 (0)