-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathPaymentScreen.js
More file actions
186 lines (171 loc) · 6.81 KB
/
PaymentScreen.js
File metadata and controls
186 lines (171 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React, { useEffect, useState } from 'react';
import { Button, Col, Form, InputGroup, ListGroup, Row, Spinner } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { savePaymentMethodIdToLocalStorage } from '../actions/orderActions';
import { getMyPaymentMethodsAction, savePaymentMethodAction } from '../actions/paymentActions';
import CheckoutSteps from '../components/CheckoutSteps';
import Loader from '../components/Loader';
import Message from '../components/Message';
const PaymentScreen = ({ history }) => {
const order = useSelector((state) => state.order);
const { shippingAddressId } = order;
if (!shippingAddressId) {
history.push('/shipping');
}
const [paymentMethodId, setPaymentMethodId] = useState('');
const [cardNumber, setCardNumber] = useState('4111 1111 1111 1111');
const [expirationMonth, setExpirationMonth] = useState('10');
const [expirationYear, setExpirationYear] = useState('23');
const [cvv, setCvv] = useState('123');
const [message, setMessage] = useState(null);
const dispatch = useDispatch();
const paymentMethodSave = useSelector((state) => state.paymentMethodSave);
const { success, loading: saveLoading, error: saveError } = paymentMethodSave;
const paymentMethodListMy = useSelector((state) => state.paymentMethodListMy);
const { paymentMethods, loading: listLoading, error: listError } = paymentMethodListMy;
useEffect(() => {
dispatch(getMyPaymentMethodsAction());
}, [dispatch]);
const proceedToPlaceOrder = () => {
dispatch(savePaymentMethodIdToLocalStorage(paymentMethodId));
history.push('/placeorder');
};
const saveCard = () => {
const cardRequestBody = {
card: {
cardNumber: cardNumber,
expirationMonth: expirationMonth,
expirationYear: expirationYear,
cvv: cvv
}
};
dispatch(savePaymentMethodAction(cardRequestBody));
};
return (
<>
<CheckoutSteps step1 step2 step3 />
<Row className='mx-5 justify-content-md-center'>
<h1 className='mx-5 justify-content-md-center'>Payment Method</h1>
</Row>
<hr></hr>
{(saveError || listError) && <Message variant='danger'>{JSON.stringify(saveError) || JSON.stringify(listError)}</Message>}
{message && <Message variant='danger'>{message}</Message>}
<Row>
<Col xs={12} md={6}>
{listLoading ? (
<Loader></Loader>
) : (
<>
<h2>Select Payment Method</h2>
{paymentMethods.map((a) => (
<>
<ListGroup.Item key={a.paymentMethodId} variant='flush'>
<InputGroup>
<Col md={1}>
<Form.Check
type='radio'
id={a.paymentMethodId}
value={paymentMethodId}
name='paymentMethod'
checked={a.paymentMethodId === paymentMethodId}
onChange={(e) => {
console.log(a.paymentMethodId);
setPaymentMethodId(a.paymentMethodId);
}}
></Form.Check>
</Col>
<Col>
<div
className='p-2'
style={{
whiteSpace: 'pre-wrap',
backgroundColor: '#eeeeee',
color: '#0e0e0e'
}}
onClick={(e) => {
console.log(a.paymentMethodId);
setPaymentMethodId(a.paymentMethodId);
}}
>
<p className='m-0' style={{ textTransform: 'uppercase' }}>
{a.cardType}
</p>
<p className='m-0'>
**** **** **** {a.cardLast4Digits} - {a.cardExpirationMonth} / {a.cardExpirationYear}
</p>
</div>
</Col>
</InputGroup>
</ListGroup.Item>
</>
))}
</>
)}
</Col>
<Col xs={12} md={6}>
<h2>
<p>Add New Card</p>
</h2>
<Row className='mx-5 justify-content-md-center'>
<Col>
<Form>
<Form.Group controlId='cardNumber'>
<Form.Label>Card Number</Form.Label>
<Form.Control
type='text'
placeholder='Card Number'
value={cardNumber}
required
onChange={(e) => setCardNumber(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group controlId='expirationMonth'>
<Form.Label>Expiration Month</Form.Label>
<Form.Control
type='text'
placeholder='Exp Month'
value={expirationMonth}
required
onChange={(e) => setExpirationMonth(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group controlId='expirationYear'>
<Form.Label>Expiration Year</Form.Label>
<Form.Control
type='text'
placeholder='Exp Year'
value={expirationYear}
required
onChange={(e) => setExpirationYear(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group controlId='cvv'>
<Form.Label>Cvv</Form.Label>
<Form.Control
type='password'
placeholder='Cvv'
value={cvv}
required
onChange={(e) => setCvv(e.target.value)}
></Form.Control>
</Form.Group>
</Form>
</Col>
</Row>
<Row className='mx-5 justify-content-md-center'>
<Button type='submit' variant='primary' onClick={saveCard} disabled={saveLoading}>
{saveLoading ? <Spinner as='span' animation='border' size='sm' role='status' aria-hidden='true' /> : <>Add Card</>}
</Button>
</Row>
</Col>
</Row>
<hr></hr>
<Row className='mx-5 justify-content-md-center'>
<Button type='submit' variant='primary' onClick={proceedToPlaceOrder} className='mt-3' disabled={!paymentMethodId}>
Proceed to PlaceOrder
</Button>
</Row>
</>
);
};
export default PaymentScreen;