@@ -6,78 +6,175 @@ import TableCell from '@material-ui/core/TableCell';
66import TableHead from '@material-ui/core/TableHead' ;
77// tslint:disable:no-magic-numbers
88import TableRow from '@material-ui/core/TableRow' ;
9+ import CheckIcon from '@material-ui/icons/Check' ;
910import DeleteIcon from '@material-ui/icons/Delete' ;
11+ import EditIcon from '@material-ui/icons/Edit' ;
12+ import ShoppingCartIcon from '@material-ui/icons/ShoppingCart' ;
1013import _isNil from 'ramda/src/isNil' ;
11- import React from 'react' ;
14+ import React , { useState } from 'react' ;
1215import { useTranslation } from 'react-i18next' ;
13- import { useHistory } from 'react-router' ;
16+ import { useHistory , useLocation } from 'react-router' ;
1417import courseImagePlaceholder from '../../images/course_400x180.png' ;
18+ import { EnhancedCourse } from '../../redux/discoveryItems/actionCreators' ;
1519import assetsUrl from '../../utils/helpers/assetsUrl' ;
1620import sumBy from '../../utils/helpers/sumBy' ;
1721import useStyles from './styles' ;
1822
19- const CartItems = ( { items, removeItem } : any ) => {
23+ export interface Options {
24+ readonly items : EnhancedCourse [ ] ;
25+ readonly removeItem : ( id : string ) => ( e : any ) => void ;
26+ }
27+
28+ const CartItems = ( { items, removeItem } : Options ) => {
2029 const classes = useStyles ( ) ;
2130 const history = useHistory ( ) ;
2231 const { t } = useTranslation ( ) ;
2332
24- const goTo = ( slug : string ) => ( e : any ) => {
33+ const goTo = ( url : string ) => ( e : any ) => {
2534 e . preventDefault ( ) ;
26- history . push ( `/courses/ ${ slug } ` ) ;
35+ history . push ( url ) ;
2736 } ;
2837
2938 const courses = items . length === 1 ? t ( 'cart.item' ) : t ( 'cart.items' ) ;
3039 const total = sumBy ( 'price' ) ( items ) ;
3140
41+ const { search } = useLocation ( ) ;
42+
43+ const params = new URLSearchParams ( search ) ;
44+
45+ const newItemId = params . get ( 'newItemId' ) ;
46+
47+ const initialState = ! newItemId ;
48+
49+ const [ isEditable , setIsEditable ] = useState ( initialState ) ;
50+
51+ const enableEditing = ( e : any ) => {
52+ e . preventDefault ( ) ;
53+
54+ return setIsEditable ( true ) ;
55+ } ;
56+
57+ const addedItem = items . find ( item => item . id === newItemId ) ;
58+ let addedItemImageUrl ;
59+
60+ if ( addedItem !== undefined ) {
61+ addedItemImageUrl =
62+ addedItem !== undefined && _isNil ( addedItem . imageUrl )
63+ ? courseImagePlaceholder
64+ : assetsUrl ( addedItem . imageUrl ) ;
65+ }
66+
3267 return (
3368 < Paper className = { classes . root } square >
3469 < Table className = { classes . table } aria-label = "table" >
35- < TableHead >
36- < TableRow >
37- < TableCell colSpan = { 3 } > </ TableCell >
38- < TableCell align = "right" > { t ( 'cart.price' ) } </ TableCell >
39- </ TableRow >
40- </ TableHead >
41- < TableBody >
42- { items . map ( ( item : any ) => {
43- const imageUrl = _isNil ( item . imageUrl )
44- ? courseImagePlaceholder
45- : assetsUrl ( item . imageUrl ) ;
46-
47- return (
48- < TableRow key = { item . title } >
49- < TableCell >
50- < img
51- alt = { item . title }
52- src = { imageUrl }
53- className = { classes . itemImage }
54- />
55- </ TableCell >
56- < TableCell colSpan = { 2 } >
57- < a
58- onClick = { goTo ( item . slug ) }
59- style = { { cursor : 'pointer' } }
60- >
61- { item . title }
62- </ a >
63- < Button size = "small" onClick = { removeItem ( item . id ) } >
64- < DeleteIcon />
65- </ Button >
66- </ TableCell >
67- < TableCell align = "right" > { item . price } </ TableCell >
68- </ TableRow >
69- ) ;
70- } ) }
71- < TableRow >
72- < TableCell colSpan = { 3 } />
73- < TableCell align = "right" >
74- { t ( 'cart.total' ) } ({ items . length } { courses } ): £{ total }
75- </ TableCell >
76- </ TableRow >
77- </ TableBody >
70+ { isEditable || addedItem === undefined ? (
71+ < >
72+ { items . length > 0 ? (
73+ < TableHead >
74+ < TableRow >
75+ < TableCell colSpan = { 3 } > </ TableCell >
76+ < TableCell align = "right" > { t ( 'cart.price' ) } </ TableCell >
77+ </ TableRow >
78+ </ TableHead >
79+ ) : null }
80+ < TableBody >
81+ { items . map ( ( item : any ) => {
82+ const imageUrl = _isNil ( item . imageUrl )
83+ ? courseImagePlaceholder
84+ : assetsUrl ( item . imageUrl ) ;
85+
86+ return (
87+ < TableRow key = { item . title } >
88+ < TableCell >
89+ < img
90+ alt = { item . title }
91+ src = { imageUrl }
92+ className = { classes . itemImage }
93+ />
94+ </ TableCell >
95+ < TableCell >
96+ < a
97+ onClick = { goTo ( `/courses/${ item . slug } ` ) }
98+ style = { { cursor : 'pointer' } }
99+ >
100+ < Typography variant = "subtitle1" > { item . title } </ Typography >
101+ </ a >
102+ < p >
103+ { t ( 'cart.instructor' ) } : { item . user . firstName } { ' ' }
104+ { item . user . lastName }
105+ </ p >
106+ </ TableCell >
107+ < TableCell >
108+ < Button size = "small" onClick = { removeItem ( item . id ) } >
109+ < DeleteIcon />
110+ </ Button >
111+ </ TableCell >
112+ < TableCell align = "right" > { item . price } </ TableCell >
113+ </ TableRow >
114+ ) ;
115+ } ) }
116+ { items . length > 0 ? (
117+ < TableRow >
118+ < TableCell colSpan = { 3 } />
119+ < TableCell align = "right" >
120+ { t ( 'cart.total' ) } ({ items . length } { courses } ): £{ total }
121+ </ TableCell >
122+ </ TableRow >
123+ ) : null }
124+
125+ { items . length === 0 ? (
126+ < TableRow >
127+ < TableCell >
128+ < Typography variant = "h6" >
129+ { t ( 'cart.yourCartIsEmpty' ) }
130+ </ Typography >
131+ < Button size = "small" onClick = { goTo ( '/' ) } >
132+ < ShoppingCartIcon /> { t ( 'cart.keepShopping' ) }
133+ </ Button >
134+ </ TableCell >
135+ </ TableRow >
136+ ) : null }
137+ </ TableBody >
138+ </ >
139+ ) : null }
140+ { ! isEditable && addedItem !== undefined ? (
141+ < TableBody >
142+ < TableRow >
143+ < TableCell >
144+ < div className = { classes . addedToBasket } >
145+ < div className = { classes . addedToBasketImageWrapper } >
146+ < img
147+ alt = { addedItem . title }
148+ src = { addedItemImageUrl }
149+ className = { classes . itemImage }
150+ />
151+ </ div >
152+ < div className = { classes . addedToBasketContent } >
153+ < CheckIcon />
154+ < Typography > { t ( 'cart.addedToCart' ) } </ Typography >
155+ </ div >
156+ </ div >
157+ </ TableCell >
158+ < TableCell colSpan = { 2 } >
159+ < a onClick = { goTo ( addedItem . slug ) } style = { { cursor : 'pointer' } } >
160+ { addedItem . title }
161+ </ a >
162+ </ TableCell >
163+ < TableCell align = "right" >
164+ < Button size = "small" onClick = { enableEditing } >
165+ < EditIcon /> { t ( 'cart.editCart' ) }
166+ </ Button >
167+ </ TableCell >
168+ < TableCell align = "right" >
169+ { t ( 'cart.total' ) } ({ items . length } { courses } ): £{ total }
170+ </ TableCell >
171+ </ TableRow >
172+ </ TableBody >
173+ ) : null }
78174 </ Table >
79175 </ Paper >
80176 ) ;
81177} ;
82178
179+ // tslint:disable-next-line:max-file-line-count
83180export default CartItems ;
0 commit comments