11// tslint:disable:no-magic-numbers
2- import { Badge , Typography } from '@material-ui/core' ;
2+ import {
3+ Badge ,
4+ ListItemAvatar ,
5+ ListItemText ,
6+ MenuItem ,
7+ MenuList ,
8+ Typography ,
9+ } from '@material-ui/core' ;
310import Button from '@material-ui/core/Button' ;
411import Menu from '@material-ui/core/Menu' ;
512import { withStyles } from '@material-ui/core/styles' ;
613import ShoppingCartIcon from '@material-ui/icons/ShoppingCart' ;
14+ import _isNil from 'ramda/src/isNil' ;
715import React from 'react' ;
16+ import { useHistory } from 'react-router' ;
17+ import courseImagePlaceholder from '../../images/course_400x180.png' ;
18+ import assetsUrl from '../../utils/helpers/assetsUrl' ;
819
920const StyledMenu = withStyles ( {
21+ list : {
22+ padding : 0 ,
23+ } ,
1024 paper : {
1125 border : '1px solid #d3d4d5' ,
26+ borderRadius : 0 ,
1227 } ,
1328} ) ( ( props : any ) => (
1429 < Menu
30+ autoFocus = { false }
1531 elevation = { 0 }
1632 getContentAnchorEl = { null }
33+ variant = "menu"
1734 anchorOrigin = { {
1835 horizontal : 'center' ,
1936 vertical : 'bottom' ,
@@ -29,6 +46,7 @@ const StyledMenu = withStyles({
2946const CartDropdown = ( ) => {
3047 // TODO: refactor component
3148 const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
49+ const history = useHistory ( ) ;
3250
3351 const handleClick = ( event : any ) => {
3452 setAnchorEl ( event . currentTarget ) ;
@@ -38,19 +56,47 @@ const CartDropdown = () => {
3856 setAnchorEl ( null ) ;
3957 } ;
4058
59+ const goToCart = ( ) => {
60+ handleClose ( ) ;
61+ history . push ( '/cart' ) ;
62+ } ;
63+
64+ const goToCourse = ( slug : string ) => ( ) => {
65+ handleClose ( ) ;
66+ history . push ( `/courses/${ slug } ` ) ;
67+ } ;
68+
69+ const cartItems : any [ ] = [
70+ {
71+ author : 'Martin Cook' ,
72+ price : '£19.99' ,
73+ slug : 'designing-microservices-architecture' ,
74+ title : 'Designing microservices architecture' ,
75+ } ,
76+ {
77+ author : 'Thomas Tik' ,
78+ price : '£19.99' ,
79+ slug : 'designing-microservices-architecture' ,
80+ title : 'Designing microservices architecture' ,
81+ } ,
82+ {
83+ author : 'Thomas Tik' ,
84+ price : '£19.99' ,
85+ slug : 'designing-microservices-architecture' ,
86+ title : 'Designing microservices architecture' ,
87+ } ,
88+ ] ;
89+
4190 return (
42- < div style = { { display : 'flex' , alignItems : 'center' } } >
91+ < div style = { { display : 'flex' , alignItems : 'center' } } >
4392 < Button
4493 aria-controls = "customized-menu"
4594 aria-haspopup = "true"
46- variant = "text"
4795 color = "inherit"
4896 onClick = { handleClick }
97+ onMouseOver = { handleClick }
4998 >
50- < Badge
51- badgeContent = { 2 }
52- color = "secondary"
53- >
99+ < Badge badgeContent = { cartItems . length || null } color = "secondary" >
54100 < ShoppingCartIcon />
55101 </ Badge >
56102 </ Button >
@@ -61,7 +107,61 @@ const CartDropdown = () => {
61107 open = { Boolean ( anchorEl ) }
62108 onClose = { handleClose }
63109 >
64- < Typography style = { { padding : 10 } } > Learn React from scratch - £17.99</ Typography >
110+ < MenuList
111+ onMouseLeave = { handleClose }
112+ disablePadding
113+ style = { { maxWidth : 400 , width : '100%' } }
114+ >
115+ { cartItems . map ( item => {
116+ const imageUrl = _isNil ( item . imageUrl )
117+ ? courseImagePlaceholder
118+ : assetsUrl ( item . imageUrl ) ;
119+
120+ return (
121+ < MenuItem onClick = { goToCourse ( item . slug ) } >
122+ < ListItemAvatar style = { { marginRight : 10 } } >
123+ < img
124+ src = { imageUrl }
125+ style = { { width : 100 , height : 60 } }
126+ alt = { item . title }
127+ />
128+ </ ListItemAvatar >
129+ < ListItemText
130+ primary = { item . title }
131+ secondary = {
132+ < >
133+ < span > Instructor: { item . author } </ span >
134+ < br />
135+ Price:
136+ < span
137+ style = { {
138+ color : '#000' ,
139+ fontWeight : 'bold' ,
140+ marginLeft : 5 ,
141+ textAlign : 'right' ,
142+ } }
143+ >
144+ { item . price }
145+ </ span >
146+ </ >
147+ }
148+ />
149+ </ MenuItem >
150+ ) ;
151+ } ) }
152+ < li style = { { padding : 10 } } >
153+ < Typography variant = "h6" style = { { marginBottom : 10 } } > Total: £59.97</ Typography >
154+
155+ < Button
156+ variant = "contained"
157+ fullWidth
158+ color = "secondary"
159+ onClick = { goToCart }
160+ >
161+ Go to cart
162+ </ Button >
163+ </ li >
164+ </ MenuList >
65165 </ StyledMenu >
66166 </ div >
67167 ) ;
0 commit comments