source: frontend/src/screens/CardPaymentScreen.js

Last change on this file was 113029b, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 20 months ago

Prototype

  • Property mode set to 100644
File size: 5.6 KB
Line 
1import React, { useContext, useEffect, useReducer, useState } from "react";
2import Container from "react-bootstrap/Container";
3import { Helmet } from "react-helmet-async";
4import Form from "react-bootstrap/Form";
5import Button from "react-bootstrap/Button";
6import { useNavigate, useParams } from "react-router-dom";
7import { Store } from "../Store";
8import axios from "axios";
9import { getError } from "../components/utils";
10import { toast } from "react-toastify";
11
12const reducer = (state, action) => {
13 switch (action.type) {
14 case "CREATE_REQUEST":
15 return { ...state, loading: true };
16 case "CREATE_SUCCESS":
17 return { ...state, loading: false };
18 case "CREATE_FAIL":
19 return { ...state, loading: false };
20 default:
21 return state;
22 }
23};
24
25function CardPaymentScreen() {
26 const [validated, setValidated] = useState(false);
27 const navigate = useNavigate();
28
29 const [{ loading }, dispatch] = useReducer(reducer, {
30 loading: false,
31 });
32
33 const { state, dispatch: ctxDispatch } = useContext(Store);
34 const { cart, userInfo } = state;
35
36 const round2 = (num) => Math.round(num * 100 + Number.EPSILON) / 100;
37 cart.itemsPrice = round2(
38 cart.cartItems.reduce((a, c) => a + c.quantity * c.price, 0)
39 );
40 cart.shippingPrice = cart.itemsPrice > 1500 ? round2(0) : round2(150);
41 cart.totalPrice = cart.itemsPrice + cart.shippingPrice;
42
43 const [cardNumber, setCardNumber] = useState("");
44 const handleChange = (event) => {
45 const result = event.target.value.replace(/[^0-9]/gi, "");
46 setCardNumber(result);
47 };
48 const [ccvNumber, setCcvNumber] = useState("");
49 const handleChangeCCV = (event) => {
50 const result = event.target.value.replace(/[^0-9]/gi, "");
51 console.log(event.currentTarget.validity.valid);
52
53 setCcvNumber(result);
54 };
55 const paymentHandler = async (event) => {
56 event.preventDefault();
57 const form = event.currentTarget;
58 if (form.checkValidity() === false) {
59 event.preventDefault();
60 event.stopPropagation();
61 }
62 if (cardNumber.length !== 16) {
63 event.preventDefault();
64 event.stopPropagation();
65 }
66 setValidated(true);
67 if (form.checkValidity() === true) {
68 try {
69 dispatch({ type: "CREATE_REQUEST" });
70 const { data } = await axios.post(
71 "/api/orders",
72 {
73 orderItems: cart.cartItems,
74 shippingAddress: cart.shippingAddress,
75 paymentMethod: cart.paymentMethod,
76 itemsPrice: cart.itemsPrice,
77 shippingPrice: cart.shippingPrice,
78 totalPrice: cart.totalPrice,
79 isPaid: true,
80 paidAt: Date.now(),
81 isConfirmed: true,
82 contactNumber: userInfo.contact,
83 },
84 {
85 headers: {
86 authorization: `Bearer ${userInfo.token}`,
87 },
88 }
89 );
90 ctxDispatch({ type: "CART_CLEAR" });
91 dispatch({ type: "CREATE_SUCCESS" });
92 localStorage.removeItem("cartItems");
93 navigate(`/order/${data.order._id}`);
94 } catch (err) {
95 dispatch({ type: "CREATE_FAIL" });
96 toast.error(getError(err));
97 }
98 }
99 };
100
101 return (
102 <div className="pageContainer">
103 <Container className="pageContainer shipPC">
104 <Helmet>
105 <title>Плати нарачка</title>
106 </Helmet>
107 <Form
108 noValidate
109 validated={validated}
110 onSubmit={paymentHandler}
111 className="formCointainer"
112 >
113 <Form.Group id="nameInput">
114 <Form.Label>Име и Презиме</Form.Label>
115 <Form.Control type="text" required></Form.Control>
116 </Form.Group>
117 <Form.Group>
118 <Form.Label>Број на картичка</Form.Label>
119 <Form.Control
120 type="text"
121 minLength="16"
122 maxLength="16"
123 value={cardNumber}
124 onChange={handleChange}
125 required
126 ></Form.Control>
127 </Form.Group>
128 <Form.Group>
129 <Form.Label>CVV2/CVC2</Form.Label>
130 <Form.Control
131 type="text"
132 minLength={3}
133 maxLength={3}
134 value={ccvNumber}
135 onChange={handleChangeCCV}
136 required
137 ></Form.Control>
138 </Form.Group>
139 <Form.Group>
140 <Form.Label>Рок на важност</Form.Label>
141 <div style={{ display: "flex" }}>
142 <Form.Select style={{ width: "47%", margin: "auto" }}>
143 <option value="01">01</option>
144 <option value="02">02</option>
145 <option value="03">03</option>
146 <option value="04">04</option>
147 <option value="05">05</option>
148 <option value="06">06</option>
149 <option value="07">07</option>
150 <option value="08">08</option>
151 <option value="09">09</option>
152 <option value="10">10</option>
153 <option value="11">11</option>
154 <option value="12">12</option>
155 </Form.Select>
156 <Form.Select style={{ width: "47%", margin: "auto" }}>
157 <option value="02">23</option>
158 <option value="03">24</option>
159 <option value="04">25</option>
160 <option value="05">26</option>
161 </Form.Select>
162 </div>
163 </Form.Group>
164
165 <div className="submitBtnContainer">
166 <Button variant="danger" size="lg" type="submit">
167 Плати
168 </Button>
169 </div>
170 </Form>
171 </Container>
172 </div>
173 );
174}
175
176export default CardPaymentScreen;
Note: See TracBrowser for help on using the repository browser.