forked from basir/next-amazona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment.js
100 lines (99 loc) · 2.94 KB
/
payment.js
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
import Cookies from 'js-cookie';
import { useRouter } from 'next/router';
import React, { useContext, useEffect, useState } from 'react';
import { Store } from '../utils/Store';
import Layout from '../components/Layout';
import CheckoutWizard from '../components/CheckoutWizard';
import useStyles from '../utils/styles';
import {
Button,
FormControl,
FormControlLabel,
List,
ListItem,
Radio,
RadioGroup,
Typography,
} from '@material-ui/core';
import { useSnackbar } from 'notistack';
export default function Payment() {
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
const classes = useStyles();
const router = useRouter();
const [paymentMethod, setPaymentMethod] = useState('');
const { state, dispatch } = useContext(Store);
const {
cart: { shippingAddress },
} = state;
useEffect(() => {
if (!shippingAddress.address) {
router.push('/shipping');
} else {
setPaymentMethod(Cookies.get('paymentMethod') || '');
}
}, []);
const submitHandler = (e) => {
closeSnackbar();
e.preventDefault();
if (!paymentMethod) {
enqueueSnackbar('Payment method is required', { variant: 'error' });
} else {
dispatch({ type: 'SAVE_PAYMENT_METHOD', payload: paymentMethod });
Cookies.set('paymentMethod', paymentMethod);
router.push('/placeorder');
}
};
return (
<Layout title="Payment Method">
<CheckoutWizard activeStep={2}></CheckoutWizard>
<form className={classes.form} onSubmit={submitHandler}>
<Typography component="h1" variant="h1">
Payment Method
</Typography>
<List>
<ListItem>
<FormControl component="fieldset">
<RadioGroup
aria-label="Payment Method"
name="paymentMethod"
value={paymentMethod}
onChange={(e) => setPaymentMethod(e.target.value)}
>
<FormControlLabel
label="PayPal"
value="PayPal"
control={<Radio />}
></FormControlLabel>
<FormControlLabel
label="Stripe"
value="Stripe"
control={<Radio />}
></FormControlLabel>
<FormControlLabel
label="Cash"
value="Cash"
control={<Radio />}
></FormControlLabel>
</RadioGroup>
</FormControl>
</ListItem>
<ListItem>
<Button fullWidth type="submit" variant="contained" color="primary">
Continue
</Button>
</ListItem>
<ListItem>
<Button
fullWidth
type="button"
variant="contained"
onClick={() => router.push('/shipping')}
>
Back
</Button>
</ListItem>
</List>
</form>
</Layout>
);
}