@@ -13,9 +13,11 @@ import { withStyles } from '@material-ui/core/styles';
1313import ShoppingCartIcon from '@material-ui/icons/ShoppingCart' ;
1414import _isNil from 'ramda/src/isNil' ;
1515import React from 'react' ;
16+ import { useTranslation } from 'react-i18next' ;
1617import { useHistory } from 'react-router' ;
1718import courseImagePlaceholder from '../../images/course_400x180.png' ;
1819import assetsUrl from '../../utils/helpers/assetsUrl' ;
20+ import sumBy from '../../utils/helpers/sumBy' ;
1921
2022const StyledMenu = withStyles ( {
2123 list : {
@@ -46,6 +48,7 @@ const StyledMenu = withStyles({
4648const CartDropdown = ( ) => {
4749 // TODO: refactor component
4850 const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
51+ const { t } = useTranslation ( ) ;
4952 const history = useHistory ( ) ;
5053
5154 const handleClick = ( event : any ) => {
@@ -56,7 +59,8 @@ const CartDropdown = () => {
5659 setAnchorEl ( null ) ;
5760 } ;
5861
59- const goToCart = ( ) => {
62+ const goToCart = ( e : any ) => {
63+ e . preventDefault ( ) ;
6064 handleClose ( ) ;
6165 history . push ( '/cart' ) ;
6266 } ;
@@ -66,37 +70,43 @@ const CartDropdown = () => {
6670 history . push ( `/courses/${ slug } ` ) ;
6771 } ;
6872
69- const cartItems : any [ ] = [
73+ const items : any [ ] = [
7074 {
7175 author : 'Martin Cook' ,
72- price : '£19.99' ,
76+ id : 1 ,
77+ price : 19.99 ,
7378 slug : 'designing-microservices-architecture' ,
7479 title : 'Designing microservices architecture' ,
7580 } ,
7681 {
7782 author : 'Thomas Tik' ,
78- price : '£19.99' ,
83+ id : 2 ,
84+ price : 19.99 ,
7985 slug : 'designing-microservices-architecture' ,
8086 title : 'Designing microservices architecture' ,
8187 } ,
8288 {
8389 author : 'Thomas Tik' ,
84- price : '£19.99' ,
90+ id : 3 ,
91+ price : 19.99 ,
8592 slug : 'designing-microservices-architecture' ,
8693 title : 'Designing microservices architecture' ,
8794 } ,
8895 ] ;
8996
97+ const total = sumBy ( 'price' ) ( items ) ;
98+
9099 return (
91100 < div style = { { display : 'flex' , alignItems : 'center' } } >
92101 < Button
93102 aria-controls = "customized-menu"
94103 aria-haspopup = "true"
95104 color = "inherit"
96- onClick = { handleClick }
97- onMouseOver = { handleClick }
105+ variant = "outlined"
106+ onClick = { goToCart }
107+ onMouseEnter = { handleClick }
98108 >
99- < Badge badgeContent = { cartItems . length || null } color = "secondary" >
109+ < Badge badgeContent = { items . length || null } color = "secondary" >
100110 < ShoppingCartIcon />
101111 </ Badge >
102112 </ Button >
@@ -112,13 +122,13 @@ const CartDropdown = () => {
112122 disablePadding
113123 style = { { maxWidth : 400 , width : '100%' } }
114124 >
115- { cartItems . map ( item => {
125+ { items . map ( item => {
116126 const imageUrl = _isNil ( item . imageUrl )
117127 ? courseImagePlaceholder
118128 : assetsUrl ( item . imageUrl ) ;
119129
120130 return (
121- < MenuItem onClick = { goToCourse ( item . slug ) } >
131+ < MenuItem onClick = { goToCourse ( item . slug ) } key = { item . id } >
122132 < ListItemAvatar style = { { marginRight : 10 } } >
123133 < img
124134 src = { imageUrl }
@@ -130,9 +140,11 @@ const CartDropdown = () => {
130140 primary = { item . title }
131141 secondary = {
132142 < >
133- < span > Instructor: { item . author } </ span >
143+ < span >
144+ { t ( 'cart.instructor' ) } : { item . author }
145+ </ span >
134146 < br />
135- Price :
147+ { t ( 'cart.price' ) } :
136148 < span
137149 style = { {
138150 color : '#000' ,
@@ -141,7 +153,7 @@ const CartDropdown = () => {
141153 textAlign : 'right' ,
142154 } }
143155 >
144- { item . price }
156+ £ { item . price }
145157 </ span >
146158 </ >
147159 }
@@ -150,15 +162,17 @@ const CartDropdown = () => {
150162 ) ;
151163 } ) }
152164 < li style = { { padding : 10 } } >
153- < Typography variant = "h6" style = { { marginBottom : 10 } } > Total: £59.97</ Typography >
165+ < Typography variant = "h6" style = { { marginBottom : 10 } } >
166+ { t ( 'cart.total' ) } : £{ total }
167+ </ Typography >
154168
155169 < Button
156170 variant = "contained"
157171 fullWidth
158172 color = "secondary"
159173 onClick = { goToCart }
160174 >
161- Go to cart
175+ { t ( ' cart.goToCart' ) }
162176 </ Button >
163177 </ li >
164178 </ MenuList >
0 commit comments