1
1
import React , { Component } from 'react' ;
2
2
import log from 'loglevel' ;
3
- import { MenuItem , Grid , Card , CardContent } from "@material-ui/core" ;
3
+ import { MenuItem , Grid , Card , CardContent , Divider } from "@material-ui/core" ;
4
4
import ContinueButton from "./continueButton" ;
5
5
import { withStyles } from "@material-ui/core/styles" ;
6
6
import { Field , reduxForm } from "redux-form" ;
@@ -10,6 +10,8 @@ import {stateCodes} from "../../../constants/stateCodes";
10
10
import DeleteIcon from '@material-ui/icons/Delete' ;
11
11
import Button from '@material-ui/core/Button' ;
12
12
import { setShippingAddress } from "../../../actions"
13
+ import IconButton from '@material-ui/core/IconButton' ;
14
+ import Modal from "../../ui/modal" ;
13
15
14
16
const styles = theme => ( {
15
17
root : {
@@ -45,14 +47,20 @@ class ShippingAddressForm extends Component {
45
47
constructor ( props ) {
46
48
super ( props ) ;
47
49
this . state = {
48
- values : null ,
50
+ addressRemovalConfirmation : false ,
49
51
}
50
52
}
51
53
52
54
onSubmitHandler = ( ) => {
53
55
log . info ( `[ShippingAddress] values = ${ JSON . stringify ( this . props . shippingAddressFormStore ) } ` )
54
- this . setState ( { values : this . props . shippingAddressFormStore . values } )
55
- this . props . setShippingAddress ( { values : this . props . shippingAddressFormStore . values , submitted : true } )
56
+ let formValues = this . props . shippingAddressFormStore . values
57
+
58
+ let id = `${ formValues . firstName } -${ formValues . lastName } -${ Math . floor ( Date . now ( ) / 1000 ) } `
59
+
60
+ this . props . setShippingAddress ( {
61
+ values : { ...this . props . shippingAddressFormStore . values , "id" : id } ,
62
+ submitted : true
63
+ } )
56
64
}
57
65
58
66
render ( ) {
@@ -61,17 +69,17 @@ class ShippingAddressForm extends Component {
61
69
62
70
log . info ( `[ShippingAddress] Rendering ShippingAddress Component. firstName = ${ firstName } ` )
63
71
64
- const handleChange = ( event ) => {
65
- log . info ( `event.target.value = ${ event . target . value } ` )
66
- } ;
67
-
68
- const onEditBtnClick = ( ) => {
72
+ const handleEditBtnClick = ( ) => {
73
+ this . props . setShippingAddress ( { submitted : false } )
74
+ }
69
75
76
+ const handleDeleteAddressClick = ( formValues ) => ( ) => {
77
+ this . setState ( { addressRemovalConfirmation : true } )
70
78
}
71
79
72
80
const renderTextField = ( label , name , validationRules ) => {
73
81
return (
74
- < Grid item container lg = { 8 } >
82
+ < Grid item container xs = { 11 } sm = { 8 } >
75
83
< Field
76
84
name = { name }
77
85
label = { label }
@@ -102,7 +110,7 @@ class ShippingAddressForm extends Component {
102
110
const renderShippingForm = ( ) => {
103
111
return (
104
112
< Grid item style = { { width : "100%" , height : 670 } } >
105
- < Grid item xs = { 11 } sm = { 8 } lg = { 12 } >
113
+ < Grid item xs = { 12 } sm = { 12 } >
106
114
< form onSubmit = { handleSubmit ( submitHandler ) } className = { classes . root }
107
115
style = { { width : "inherit" } } >
108
116
{ renderTextField ( "First Name" , "firstName" , requiredRule ) }
@@ -111,7 +119,7 @@ class ShippingAddressForm extends Component {
111
119
[ requiredRule , addressLine1Rule ] ) }
112
120
{ renderTextField ( "Address Line 2 (optional)" , "addressLine2" , null ) }
113
121
114
- < Grid item container lg = { 8 } >
122
+ < Grid item container xs = { 11 } sm = { 8 } >
115
123
< Grid item container xs = { 6 } style = { { paddingRight : 15 } } >
116
124
< Field
117
125
name = "zipCode"
@@ -132,7 +140,6 @@ class ShippingAddressForm extends Component {
132
140
component = { TextField }
133
141
variant = "outlined"
134
142
select
135
- onChange = { handleChange }
136
143
fullWidth
137
144
size = "medium"
138
145
style = { textFieldStyles }
@@ -156,10 +163,11 @@ class ShippingAddressForm extends Component {
156
163
}
157
164
158
165
const renderShippingSummaryAddress = ( values ) => {
166
+
159
167
return (
160
- < Grid item lg = { 5 } style = { { margin : "2rem 2rem 0 2rem" } } >
161
- < Card style = { { height : 200 , fontSize : "1.1rem" } } >
162
- < CardContent style = { { height : 160 } } >
168
+ < Grid item lg = { 4 } style = { { margin : "2rem" } } >
169
+ < Card style = { { height : "fit-content" , fontSize : "1.1rem" } } >
170
+ < CardContent style = { { height : "fit-content" } } >
163
171
164
172
< Grid item style = { { marginBottom : "0.5rem" } } >
165
173
{ `${ values . firstName } ${ values . lastName } ` }
@@ -185,34 +193,86 @@ class ShippingAddressForm extends Component {
185
193
186
194
</ CardContent >
187
195
188
- < Grid container justify = "flex-end" style = { { paddingRight : "0.5rem" , paddingBottom : "1rem" } } >
189
- < DeleteIcon fontSize = "large" />
196
+ < Grid container style = { { padding : "0 0.8rem 1rem 1rem" } } >
197
+ < Grid item xs = { 6 } style = { { alignSelf : "center" } } >
198
+ < Button variant = "outlined" color = "default" fullWidth style = { {
199
+ height : "3rem" ,
200
+ fontSize : "1rem"
201
+ } } onClick = { handleEditBtnClick } >
202
+ Edit
203
+ </ Button >
204
+ </ Grid >
205
+
206
+ < Grid item container xs = { 6 } justify = { "flex-end" } >
207
+ < IconButton onClick = { handleDeleteAddressClick ( values ) } >
208
+ < DeleteIcon fontSize = "large" />
209
+ </ IconButton >
210
+ </ Grid >
211
+
190
212
</ Grid >
191
213
</ Card >
192
-
193
- < Grid container justify = "center" style = { { padding : "2rem 3rem 1rem 3rem" } } >
194
- < Button variant = "contained" color = "secondary" fullWidth style = { {
195
- height : "3rem" ,
196
- fontSize : "1.2rem"
197
- } } onClick = { onEditBtnClick } >
198
- Edit
199
- </ Button >
200
- </ Grid >
201
214
</ Grid >
202
215
)
203
216
}
204
217
205
218
const renderShippingSummaryAddresses = ( ) => {
206
219
return (
207
220
< Grid container justify = "flex-start" style = { { height : "fit-content" , backgroundColor : "#80808033" } } >
208
- { renderShippingSummaryAddress ( this . state . values ) }
221
+ { renderShippingSummaryAddress ( this . props . shippingAddress . values ) }
222
+ </ Grid >
223
+ )
224
+ }
225
+
226
+ const renderAddressRemovalConfirmation = ( ) => {
227
+ return (
228
+ < Grid container direction = "column" >
229
+ < Grid item container direction = "column" style = { { margin : "1rem" } } >
230
+ < Grid item
231
+ style = { { color : "#3e4152" , fontSize : 14 , fontWeight : "bolder" , paddingBottom : "1rem" } } >
232
+ Remove Address
233
+ </ Grid >
234
+ < Grid item style = { { color : "#696b79" , fontSize : 14 , fontWeight : 200 } } >
235
+ Are you sure you want to remove selected address?
236
+ </ Grid >
237
+ </ Grid >
238
+ < Grid item >
239
+ < Divider style = { { width : 300 , height : 1 } } />
240
+ </ Grid >
241
+ < Grid item container alignItems = "center" style = { { textAlignLast : "center" } } >
242
+ < Grid item xs = { 6 } onClick = { removeModalClickHandler } style = { {
243
+ color : "red" , fontWeight : "bold" , cursor : "pointer"
244
+ } } >
245
+ REMOVE
246
+ </ Grid >
247
+ < Divider orientation = "vertical" style = { { width : 0 , height : 45 } } />
248
+ < Grid item xs = { 5 } onClick = { closeModalClickHandler }
249
+ style = { { fontWeight : "bold" , cursor : "pointer" , paddingLeft : "1.3rem" } } >
250
+ CANCEL
251
+ </ Grid >
252
+ </ Grid >
209
253
</ Grid >
210
254
)
211
255
}
212
256
257
+ const closeModalClickHandler = ( ) => {
258
+ this . setState ( { addressRemovalConfirmation : false } )
259
+ }
260
+
261
+ const removeModalClickHandler = ( ) => {
262
+ this . props . reset ( 'shippingAddressForm' )
263
+ this . props . setShippingAddress ( { values : null , submitted : false } )
264
+ this . setState ( { addressRemovalConfirmation : false } )
265
+ }
266
+
267
+ log . info ( `this.state.addressRemovalConfirmation = ${ this . state . addressRemovalConfirmation } ` )
213
268
return (
214
269
< >
215
- { this . state . values ? renderShippingSummaryAddresses ( ) : renderShippingForm ( ) }
270
+ { this . props . shippingAddress . submitted
271
+ ? renderShippingSummaryAddresses ( ) : renderShippingForm ( ) }
272
+ { this . state . addressRemovalConfirmation ?
273
+ < Modal renderWarningComponent = { renderAddressRemovalConfirmation ( ) }
274
+ modalWidth = "300px"
275
+ closeHandler = { closeModalClickHandler } /> : null }
216
276
</ >
217
277
)
218
278
}
@@ -221,7 +281,9 @@ class ShippingAddressForm extends Component {
221
281
const mapStateToProps = ( state ) => {
222
282
return ( {
223
283
shippingAddressFormStore : state . form . shippingAddressForm ?
224
- state . form . shippingAddressForm : null
284
+ state . form . shippingAddressForm : null ,
285
+ shippingAddress : state . shippingAddressReducer
286
+
225
287
} )
226
288
}
227
289
0 commit comments