@@ -11,40 +11,54 @@ import _isNil from 'ramda/src/isNil';
1111import _propEq from 'ramda/src/propEq' ;
1212import React from 'react' ;
1313import { useTranslation } from 'react-i18next' ;
14- import { Link } from 'react-router-dom' ;
14+ import { useDispatch , useSelector } from 'react-redux' ;
15+ import { Link , useHistory } from 'react-router-dom' ;
1516// tslint:disable:no-import-side-effect
1617import 'slick-carousel/slick/slick-theme.css' ;
1718import 'slick-carousel/slick/slick.css' ;
1819// TODO: check slow rendering issue
1920// import CourseRating from '../../atoms/CourseRating';
2021import courseImagePlaceholder from '../../images/course_400x180.png' ;
22+ import { addCartItem } from '../../redux/cart/actionCreators' ;
2123import { EnhancedCourse } from '../../redux/discoveryItems/actionCreators' ;
24+ import { State } from '../../redux/rootReducer' ;
2225import assetsUrl from '../../utils/helpers/assetsUrl' ;
26+ import useStyles from './styles' ;
2327
24- const Slide = ( {
25- course,
26- classes,
27- } : {
28- classes : any ;
29- course : EnhancedCourse ;
30- } ) => {
28+ const Slide = ( { course } : { course : EnhancedCourse } ) => {
3129 const { t } = useTranslation ( ) ;
30+ const { items } = useSelector ( ( state : State ) => state . cart ) ;
31+ const classes = useStyles ( ) ;
3232 // TODO: get real rating and price
3333 // const courseRating = Number((Math.random() * 5).toFixed(1));
3434 // tslint:disable-next-line:no-magic-numbers
3535 const coursePrice = Number ( Math . random ( ) * 10 + 9 ) . toFixed ( 2 ) ;
3636
37- const imageUrl = ! _isNil ( course . imageUrl ) ? assetsUrl ( course . imageUrl ) : courseImagePlaceholder ;
37+ const imageUrl = ! _isNil ( course . imageUrl )
38+ ? assetsUrl ( course . imageUrl )
39+ : courseImagePlaceholder ;
40+
41+ const dispatch = useDispatch ( ) ;
42+ const history = useHistory ( ) ;
43+
44+ const addItem = ( item : EnhancedCourse ) => ( e : any ) => {
45+ e . preventDefault ( ) ;
46+ dispatch ( addCartItem ( item ) ) ;
47+ history . push ( { pathname : '/cart' , search : `?newItemId=${ item . id } ` } ) ;
48+ } ;
49+
50+ const hasAddedToCart =
51+ items . find ( item => item . id === course . id ) !== undefined ;
3852
3953 return (
40- < Card className = { classes . card } >
41- < Link className = { classes . courseLink } to = { `/courses/ ${ course . slug } ` } >
42- < CardMedia
43- className = { classes . cardMedia }
44- image = { imageUrl }
45- title = { course . title }
46- / >
47- < CardContent className = { classes . cardContent } >
54+ < Card >
55+ < CardMedia
56+ className = { classes . cardMedia }
57+ image = { imageUrl }
58+ title = { course . title }
59+ />
60+ < CardContent className = { classes . cardContent } >
61+ < Link className = { classes . courseLink } to = { `/courses/ ${ course . slug } ` } >
4862 < Typography
4963 variant = "subtitle1"
5064 component = "div"
@@ -65,19 +79,29 @@ const Slide = ({
6579 { /* <CourseRating value={courseRating} /> */ }
6680
6781 < div className = { classes . cardPrice } > { `£${ coursePrice } ` } </ div >
68- </ CardContent >
69- < CardActions >
82+ </ Link >
83+ </ CardContent >
84+ < CardActions className = { classes . cardActions } >
85+ { ! hasAddedToCart ? (
7086 < Button
7187 size = "small"
7288 color = "primary"
73- style = { { textTransform : 'capitalize' } }
89+ onClick = { addItem ( course ) }
90+ className = { classes . cartAction }
7491 >
7592 { t ( 'cart.addToCart' ) }
7693 </ Button >
77- </ CardActions >
78- </ Link >
94+ ) : null }
95+
96+ { hasAddedToCart ? (
97+ < Typography className = { classes . cartAction } >
98+ { t ( 'cart.addedToCart' ) }
99+ </ Typography >
100+ ) : null }
101+ </ CardActions >
79102 </ Card >
80103 ) ;
81104} ;
82105
83- export default Slide ;
106+ // tslint:disable-next-line:max-file-line-count
107+ export default Slide ;
0 commit comments