[16237c4] | 1 | import React, { useContext, useEffect, useReducer, useState } from "react";
|
---|
| 2 | import Container from "react-bootstrap/Container";
|
---|
| 3 | import { Helmet } from "react-helmet-async";
|
---|
| 4 | import Form from "react-bootstrap/Form";
|
---|
| 5 | import Button from "react-bootstrap/Button";
|
---|
| 6 | import { useNavigate, useParams } from "react-router-dom";
|
---|
| 7 | import { Store } from "../Store";
|
---|
| 8 | import axios from "axios";
|
---|
| 9 | import { getError } from "../components/utils";
|
---|
| 10 | import { toast } from "react-toastify";
|
---|
| 11 |
|
---|
| 12 | const 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 |
|
---|
| 25 | function 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 paymentHandler = async (event) => {
|
---|
| 44 | event.preventDefault();
|
---|
| 45 | const form = event.currentTarget;
|
---|
| 46 | if (form.checkValidity() === false) {
|
---|
| 47 | event.preventDefault();
|
---|
| 48 | event.stopPropagation();
|
---|
| 49 | }
|
---|
| 50 | setValidated(true);
|
---|
| 51 | if (form.checkValidity() === true) {
|
---|
| 52 | try {
|
---|
| 53 | dispatch({ type: "CREATE_REQUEST" });
|
---|
| 54 | const { data } = await axios.post(
|
---|
| 55 | "/api/orders",
|
---|
| 56 | {
|
---|
| 57 | orderItems: cart.cartItems,
|
---|
| 58 | shippingAddress: cart.shippingAddress,
|
---|
| 59 | paymentMethod: cart.paymentMethod,
|
---|
| 60 | itemsPrice: cart.itemsPrice,
|
---|
| 61 | shippingPrice: cart.shippingPrice,
|
---|
| 62 | totalPrice: cart.totalPrice,
|
---|
| 63 | isPaid: true,
|
---|
| 64 | paidAt: Date.now(),
|
---|
| 65 | },
|
---|
| 66 | {
|
---|
| 67 | headers: {
|
---|
| 68 | authorization: `Bearer ${userInfo.token}`,
|
---|
| 69 | },
|
---|
| 70 | }
|
---|
| 71 | );
|
---|
| 72 | ctxDispatch({ type: "CART_CLEAR" });
|
---|
| 73 | dispatch({ type: "CREATE_SUCCESS" });
|
---|
| 74 | localStorage.removeItem("cartItems");
|
---|
| 75 | navigate(`/order/${data.order._id}`);
|
---|
| 76 | } catch (err) {
|
---|
| 77 | dispatch({ type: "CREATE_FAIL" });
|
---|
| 78 | toast.error(getError(err));
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | return (
|
---|
| 84 | <div className="pageContainer">
|
---|
| 85 | <Container className="pageContainer shipPC">
|
---|
| 86 | <Helmet>
|
---|
| 87 | <title>Плати нарачка</title>
|
---|
| 88 | </Helmet>
|
---|
| 89 | <Form
|
---|
| 90 | noValidate
|
---|
| 91 | validated={validated}
|
---|
| 92 | onSubmit={paymentHandler}
|
---|
| 93 | className="formCointainer"
|
---|
| 94 | >
|
---|
| 95 | <Form.Group id="nameInput">
|
---|
| 96 | <Form.Label>Име и Презиме</Form.Label>
|
---|
| 97 | <Form.Control
|
---|
| 98 | type="text"
|
---|
| 99 | //value={holderName}
|
---|
| 100 | // onChange={(e) => setHolderName(e.target.value)}
|
---|
| 101 | required
|
---|
| 102 | ></Form.Control>
|
---|
| 103 | </Form.Group>
|
---|
| 104 | <Form.Group>
|
---|
| 105 | <Form.Label>Број на картичка</Form.Label>
|
---|
| 106 | <Form.Control
|
---|
| 107 | type="text"
|
---|
| 108 | //value={cardNumber}
|
---|
| 109 | //onChange={(e) => setCardNumber(e.target.value)}
|
---|
| 110 | required
|
---|
| 111 | ></Form.Control>
|
---|
| 112 | </Form.Group>
|
---|
| 113 | <Form.Group>
|
---|
| 114 | <Form.Label>CVV2/CVC2</Form.Label>
|
---|
| 115 | <Form.Control
|
---|
| 116 | type="text"
|
---|
| 117 | //value={cvv}
|
---|
| 118 | //onChange={(e) => setCvv(e.target.value)}
|
---|
| 119 | required
|
---|
| 120 | ></Form.Control>
|
---|
| 121 | </Form.Group>
|
---|
| 122 | <Form.Group>
|
---|
| 123 | <Form.Label>Рок на важност</Form.Label>
|
---|
| 124 | <div style={{ display: "flex" }}>
|
---|
| 125 | <Form.Select style={{ width: "47%", margin: "auto" }}>
|
---|
| 126 | <option value="01">01</option>
|
---|
| 127 | <option value="02">02</option>
|
---|
| 128 | <option value="03">03</option>
|
---|
| 129 | <option value="04">04</option>
|
---|
| 130 | <option value="05">05</option>
|
---|
| 131 | <option value="06">06</option>
|
---|
| 132 | <option value="07">07</option>
|
---|
| 133 | <option value="08">08</option>
|
---|
| 134 | <option value="09">09</option>
|
---|
| 135 | <option value="10">10</option>
|
---|
| 136 | <option value="11">11</option>
|
---|
| 137 | <option value="12">12</option>
|
---|
| 138 | </Form.Select>
|
---|
| 139 | <Form.Select style={{ width: "47%", margin: "auto" }}>
|
---|
| 140 | <option value="01">22</option>
|
---|
| 141 | <option value="02">23</option>
|
---|
| 142 | <option value="03">24</option>
|
---|
| 143 | <option value="04">25</option>
|
---|
| 144 | <option value="05">26</option>
|
---|
| 145 | </Form.Select>
|
---|
| 146 | </div>
|
---|
| 147 | </Form.Group>
|
---|
| 148 |
|
---|
| 149 | <div className="submitBtnContainer">
|
---|
| 150 | <Button variant="danger" size="lg" type="submit">
|
---|
| 151 | Плати
|
---|
| 152 | </Button>
|
---|
| 153 | </div>
|
---|
| 154 | </Form>
|
---|
| 155 | </Container>
|
---|
| 156 | </div>
|
---|
| 157 | );
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | export default CardPaymentScreen;
|
---|