File tree Expand file tree Collapse file tree 7 files changed +92
-9
lines changed Expand file tree Collapse file tree 7 files changed +92
-9
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,20 @@ import { Product } from '../types/product';
2
2
import { CartTypes } from '../types/cart' ;
3
3
import { createAction } from '../types/helpers' ;
4
4
5
- export type CartActionTypes = ReturnType < typeof addToCart >
5
+ export type CartActionTypes = ReturnType < typeof addToCart > | ReturnType < typeof removeFromCart > ;
6
6
7
7
// TODO: update cart in local storage with a cart service
8
8
9
9
export const addToCart = ( product : Product ) => {
10
- // NOTE: this will be removed in the next PR
11
- console . log ( `product added: ${ JSON . stringify ( product ) } ` ) ;
12
10
return createAction ( {
13
11
type : CartTypes . AddToCart ,
14
12
product,
15
13
} ) ;
16
14
} ;
15
+
16
+ export const removeFromCart = ( index : number ) => {
17
+ return createAction ( {
18
+ type : CartTypes . RemoveFromCart ,
19
+ index,
20
+ } ) ;
21
+ } ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
- import { Container } from 'react-bootstrap' ;
2
+ import { Button } from 'react-bootstrap' ;
3
+ import { Link } from 'react-router-dom' ;
4
+ import {
5
+ Container ,
6
+ Col ,
7
+ Row ,
8
+ } from 'react-bootstrap' ;
9
+ import CartItems from './CartItems' ;
10
+ import { CartViewModifier } from '../types/cart' ;
3
11
4
12
const Cart = ( ) => {
5
- return (
13
+ return (
6
14
< Container >
7
- < h1 > Cart</ h1 >
15
+ < Row >
16
+ < Col >
17
+ < span style = { { float :'right' } } > < Link to = "/" > Keep shoppe-ing</ Link > </ span >
18
+ < h4 > Your Cart</ h4 >
19
+ </ Col >
20
+ </ Row >
21
+ < Row >
22
+ < Col >
23
+ < CartItems cartView = { CartViewModifier . DELETE } />
24
+ < Link to = "/checkout" style = { { float :'right' , padding :'1rem 0 0 0' } } >
25
+ < Button variant = "primary" > Continue to Checkout</ Button >
26
+ </ Link >
27
+ </ Col >
28
+ </ Row >
8
29
</ Container >
9
- ) ;
30
+ )
10
31
} ;
11
32
12
33
export default Cart ;
Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ import { Button } from 'react-bootstrap';
3
3
import { useSelector } from 'react-redux' ;
4
4
import { Link } from 'react-router-dom' ;
5
5
import { AppState } from '../store' ;
6
- import { Product } from '../types/product' ;
7
6
8
7
const CartButton = ( ) => {
9
8
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import { useSelector , useDispatch } from 'react-redux'
3
+ import { ListGroup } from 'react-bootstrap' ;
4
+ import { AppDispatch } from '../actions' ;
5
+ import { AppState } from "../store" ;
6
+ import { CartViewModifier } from '../types/cart' ;
7
+ import { removeFromCart } from '../actions/cart' ;
8
+
9
+ type Props = {
10
+ cartView ?: CartViewModifier
11
+ }
12
+
13
+ const CartItems : React . FC < Props > = ( { cartView = CartViewModifier . NONE } ) => {
14
+ const products = useSelector ( ( state : AppState ) => state . cartState . products ) ;
15
+ const dispatch = useDispatch < AppDispatch > ( ) ;
16
+ const cartTotal = products . reduce ( ( total , prod ) => total + prod . price , 0 ) ;
17
+ return (
18
+ < ListGroup >
19
+ { products . map ( ( product , i ) => (
20
+ < ListGroup . Item key = { product . id } >
21
+ { product . title }
22
+ < span className = 'cart-price' > ${ product . price } < span className = 'text-muted' > { product . unit } </ span >
23
+ { cartView === CartViewModifier . DELETE &&
24
+ < span className = 'fa fa-times text-danger remove-item' onClick = { ( ) => dispatch ( removeFromCart ( i ) ) } > </ span >
25
+ }
26
+ </ span >
27
+ </ ListGroup . Item >
28
+ ) ) }
29
+ < ListGroup . Item >
30
+ < strong > Total</ strong > < span className = 'cart-price' > < strong > ${ cartTotal . toFixed ( 2 ) } </ strong > </ span >
31
+ </ ListGroup . Item >
32
+ </ ListGroup >
33
+ ) ;
34
+ } ;
35
+
36
+ export default CartItems ;
Original file line number Diff line number Diff line change @@ -29,4 +29,14 @@ a.header-link {
29
29
background-repeat : no-repeat;
30
30
background-position : center;
31
31
background-size : cover;
32
+ }
33
+
34
+ .cart-price {
35
+ float : right;
36
+ }
37
+
38
+ .remove-item {
39
+ display : inline-block;
40
+ padding-left : 1rem ;
41
+ cursor : pointer;
32
42
}
Original file line number Diff line number Diff line change @@ -3,10 +3,16 @@ import { AppActions } from '../../actions';
3
3
import { CartTypes } from '../../types/cart' ;
4
4
import { Product } from '../../types/product' ;
5
5
6
+ const sortByTitle = ( prod1 : Product , prod2 : Product ) => {
7
+ return prod1 . title . localeCompare ( prod2 . title ) ;
8
+ }
9
+
6
10
const productsReducer = ( state : Product [ ] = [ ] , action : AppActions ) : Product [ ] => {
7
11
switch ( action . type ) {
8
12
case CartTypes . AddToCart :
9
- return [ ...state , action . product ] ;
13
+ return [ ...state , action . product ] . sort ( sortByTitle ) ;
14
+ case CartTypes . RemoveFromCart :
15
+ return state . filter ( ( _ , index ) => index !== action . index )
10
16
default :
11
17
return state ;
12
18
}
Original file line number Diff line number Diff line change 1
1
export enum CartTypes {
2
2
AddToCart = 'ADD_TO_CART' ,
3
+ RemoveFromCart = 'REMOVE_FROM_CART' ,
3
4
} ;
5
+
6
+ export enum CartViewModifier {
7
+ NONE ,
8
+ DELETE ,
9
+ } ;
You can’t perform that action at this time.
0 commit comments