@@ -6,78 +6,175 @@ import TableCell from '@material-ui/core/TableCell';
6
6
import TableHead from '@material-ui/core/TableHead' ;
7
7
// tslint:disable:no-magic-numbers
8
8
import TableRow from '@material-ui/core/TableRow' ;
9
+ import CheckIcon from '@material-ui/icons/Check' ;
9
10
import DeleteIcon from '@material-ui/icons/Delete' ;
11
+ import EditIcon from '@material-ui/icons/Edit' ;
12
+ import ShoppingCartIcon from '@material-ui/icons/ShoppingCart' ;
10
13
import _isNil from 'ramda/src/isNil' ;
11
- import React from 'react' ;
14
+ import React , { useState } from 'react' ;
12
15
import { useTranslation } from 'react-i18next' ;
13
- import { useHistory } from 'react-router' ;
16
+ import { useHistory , useLocation } from 'react-router' ;
14
17
import courseImagePlaceholder from '../../images/course_400x180.png' ;
18
+ import { EnhancedCourse } from '../../redux/discoveryItems/actionCreators' ;
15
19
import assetsUrl from '../../utils/helpers/assetsUrl' ;
16
20
import sumBy from '../../utils/helpers/sumBy' ;
17
21
import useStyles from './styles' ;
18
22
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 ) => {
20
29
const classes = useStyles ( ) ;
21
30
const history = useHistory ( ) ;
22
31
const { t } = useTranslation ( ) ;
23
32
24
- const goTo = ( slug : string ) => ( e : any ) => {
33
+ const goTo = ( url : string ) => ( e : any ) => {
25
34
e . preventDefault ( ) ;
26
- history . push ( `/courses/ ${ slug } ` ) ;
35
+ history . push ( url ) ;
27
36
} ;
28
37
29
38
const courses = items . length === 1 ? t ( 'cart.item' ) : t ( 'cart.items' ) ;
30
39
const total = sumBy ( 'price' ) ( items ) ;
31
40
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
+
32
67
return (
33
68
< Paper className = { classes . root } square >
34
69
< 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 }
78
174
</ Table >
79
175
</ Paper >
80
176
) ;
81
177
} ;
82
178
179
+ // tslint:disable-next-line:max-file-line-count
83
180
export default CartItems ;
0 commit comments