1 | import React, { useState, useEffect, useContext } from 'react';
|
---|
2 | import { FaArrowLeft, FaUtensils } from "react-icons/fa";
|
---|
3 | import { useNavigate } from 'react-router-dom';
|
---|
4 | import { OrderContext } from './OrderContext';
|
---|
5 | import styles from '../../css/ShoppingCartCss/delivery-details-style.module.css';
|
---|
6 |
|
---|
7 | const DeliveryDetails = () => {
|
---|
8 | const { isOrderInProgress, isOrderFinished, orderStatus, orderId, startOrder, checkOrderStatus } = useContext(OrderContext);
|
---|
9 | const [address, setAddress] = useState('');
|
---|
10 | const [addressNumber, setAddressNumber] = useState('');
|
---|
11 | const [addressFloor, setAddressFloor] = useState('');
|
---|
12 | const [phoneNumber, setPhoneNumber] = useState('');
|
---|
13 | const [isLoading, setIsLoading] = useState(false);
|
---|
14 | const [inputsDisabled, setInputsDisabled] = useState(false);
|
---|
15 | const [showModal, setShowModal] = useState(false);
|
---|
16 | const navigate = useNavigate();
|
---|
17 | const [itemsWithQuantities, setItemsWithQuantities] = useState([]);
|
---|
18 |
|
---|
19 | useEffect(() => {
|
---|
20 | const storedCart = localStorage.getItem('cart');
|
---|
21 | if (storedCart) {
|
---|
22 | setItemsWithQuantities(JSON.parse(storedCart));
|
---|
23 | }
|
---|
24 |
|
---|
25 | const storedFullAddress = localStorage.getItem('address');
|
---|
26 | const storedPhoneNumber = localStorage.getItem('phoneNumber');
|
---|
27 |
|
---|
28 | if (storedFullAddress) {
|
---|
29 | const [addr, number, floor] = storedFullAddress.split(';');
|
---|
30 | setAddress(addr.trim());
|
---|
31 | setAddressNumber(number.trim());
|
---|
32 | setAddressFloor(floor.trim());
|
---|
33 | }
|
---|
34 |
|
---|
35 | if (storedPhoneNumber) {
|
---|
36 | setPhoneNumber(storedPhoneNumber.trim());
|
---|
37 | }
|
---|
38 | }, []);
|
---|
39 |
|
---|
40 | useEffect(() => {
|
---|
41 | if (isOrderInProgress && orderId) {
|
---|
42 | setInputsDisabled(true);
|
---|
43 | }
|
---|
44 | }, [isOrderInProgress, orderId]);
|
---|
45 |
|
---|
46 | useEffect(() => {
|
---|
47 | if (orderId) {
|
---|
48 | const intervalId = setInterval(() => {
|
---|
49 | checkOrderStatus(orderId);
|
---|
50 | }, 1000);
|
---|
51 |
|
---|
52 | return () => clearInterval(intervalId);
|
---|
53 | }
|
---|
54 |
|
---|
55 | }, [orderId, checkOrderStatus]);
|
---|
56 |
|
---|
57 | const handleSubmit = () => {
|
---|
58 | if (orderId || isOrderInProgress) {
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | setIsLoading(true);
|
---|
63 | setShowModal(true);
|
---|
64 |
|
---|
65 | const fullAddress = `${address}; ${addressNumber}; ${addressFloor}`;
|
---|
66 | const order = {
|
---|
67 | address: fullAddress,
|
---|
68 | isFinished: false,
|
---|
69 | };
|
---|
70 |
|
---|
71 | saveOrderAndProducts(order, itemsWithQuantities);
|
---|
72 | };
|
---|
73 |
|
---|
74 | const saveOrderAndProducts = (order, items) => {
|
---|
75 | fetch("http://localhost:8080/api/orders", {
|
---|
76 | method: "POST",
|
---|
77 | headers: {
|
---|
78 | "Content-Type": "application/json",
|
---|
79 | "Authorization": `Bearer ${localStorage.getItem('token')}`
|
---|
80 | },
|
---|
81 | body: JSON.stringify(order)
|
---|
82 | })
|
---|
83 | .then(response => response.json())
|
---|
84 | .then(savedOrder => {
|
---|
85 | if (!savedOrder || !savedOrder.id) {
|
---|
86 | throw new Error("Order was not saved correctly.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | startOrder(savedOrder.id);
|
---|
90 |
|
---|
91 | const productOrders = items.map(item => ({
|
---|
92 | orderId: savedOrder.id,
|
---|
93 | productId: item.id,
|
---|
94 | quantity: item.grams,
|
---|
95 | deliveryPersonId: null,
|
---|
96 | }));
|
---|
97 |
|
---|
98 | return fetch("http://localhost:8080/api/productOrders", {
|
---|
99 | method: "POST",
|
---|
100 | headers: {
|
---|
101 | "Content-Type": "application/json",
|
---|
102 | "Authorization": `Bearer ${localStorage.getItem('token')}`
|
---|
103 | },
|
---|
104 | body: JSON.stringify(productOrders)
|
---|
105 | }).then(() => savedOrder.id);
|
---|
106 | })
|
---|
107 | .then(orderId => {
|
---|
108 | setIsLoading(false);
|
---|
109 | checkOrderStatus(orderId);
|
---|
110 | })
|
---|
111 | .catch(error => {
|
---|
112 | console.error("Error saving order or product orders:", error);
|
---|
113 | setIsLoading(false);
|
---|
114 | });
|
---|
115 | };
|
---|
116 | return (
|
---|
117 | <div className={styles.checkoutContainer}>
|
---|
118 | <div className={styles.checkoutCard}>
|
---|
119 | <h2>Delivery Details</h2>
|
---|
120 | <div className={styles.backArrow} onClick={() => navigate(-1)}>
|
---|
121 | <FaArrowLeft />
|
---|
122 | </div>
|
---|
123 |
|
---|
124 | <div className={styles.inputField}>
|
---|
125 | <label htmlFor="address">Address:</label>
|
---|
126 | <input
|
---|
127 | type="text"
|
---|
128 | id="address"
|
---|
129 | value={address}
|
---|
130 | onChange={(e) => setAddress(e.target.value)}
|
---|
131 | placeholder="Enter your address"
|
---|
132 | disabled={inputsDisabled}
|
---|
133 | required
|
---|
134 | />
|
---|
135 | </div>
|
---|
136 |
|
---|
137 | <div className={styles.inputField}>
|
---|
138 | <label htmlFor="addressNumber">Address Number:</label>
|
---|
139 | <input
|
---|
140 | type="text"
|
---|
141 | id="addressNumber"
|
---|
142 | value={addressNumber}
|
---|
143 | onChange={(e) => setAddressNumber(e.target.value)}
|
---|
144 | placeholder="Enter address number"
|
---|
145 | disabled={inputsDisabled}
|
---|
146 | required
|
---|
147 | />
|
---|
148 | </div>
|
---|
149 |
|
---|
150 | <div className={styles.inputField}>
|
---|
151 | <label htmlFor="addressFloor">Address Floor:</label>
|
---|
152 | <input
|
---|
153 | type="text"
|
---|
154 | id="addressFloor"
|
---|
155 | value={addressFloor}
|
---|
156 | onChange={(e) => setAddressFloor(e.target.value)}
|
---|
157 | placeholder="Enter floor (optional)"
|
---|
158 | disabled={inputsDisabled}
|
---|
159 | />
|
---|
160 | </div>
|
---|
161 |
|
---|
162 | <div className={styles.inputField}>
|
---|
163 | <label htmlFor="phoneNumber">Phone Number:</label>
|
---|
164 | <input
|
---|
165 | type="text"
|
---|
166 | id="phoneNumber"
|
---|
167 | value={phoneNumber}
|
---|
168 | onChange={(e) => setPhoneNumber(e.target.value)}
|
---|
169 | placeholder="Enter your phone number"
|
---|
170 | disabled
|
---|
171 | required
|
---|
172 | />
|
---|
173 | </div>
|
---|
174 |
|
---|
175 | <h3>Ingredients and Quantities</h3>
|
---|
176 | <ul className={styles.ingredientsList}>
|
---|
177 | {itemsWithQuantities.map((item, index) => (
|
---|
178 | <li key={index} className={styles.ingredientItem}>
|
---|
179 | <FaUtensils className={styles.cartItemIcon} />
|
---|
180 | <div className={styles.itemDetails}>
|
---|
181 | <p className={styles.ingredientItemTitle}>{item.name}</p>
|
---|
182 | <p>{item.grams} grams (In Recipe: {item.measurement})</p>
|
---|
183 | </div>
|
---|
184 | </li>
|
---|
185 | ))}
|
---|
186 | </ul>
|
---|
187 |
|
---|
188 |
|
---|
189 | {!isOrderFinished && (
|
---|
190 | <button
|
---|
191 | onClick={handleSubmit}
|
---|
192 | className={styles.proceedButton}
|
---|
193 | disabled={isLoading || isOrderInProgress || itemsWithQuantities.length === 0}
|
---|
194 | >
|
---|
195 | {isLoading ? "Processing..." : "Confirm Delivery"}
|
---|
196 | </button>
|
---|
197 | )}
|
---|
198 |
|
---|
199 | {orderStatus && (
|
---|
200 | <p className={styles.orderStatusMessage}>
|
---|
201 | {orderStatus}
|
---|
202 | </p>
|
---|
203 | )}
|
---|
204 |
|
---|
205 | {showModal && (
|
---|
206 | <div className={styles.modal}>
|
---|
207 | <div className={styles.modalContent}>
|
---|
208 | <button
|
---|
209 | className={styles.closeButton}
|
---|
210 | onClick={() => setShowModal(false)}
|
---|
211 | >
|
---|
212 | ×
|
---|
213 | </button>
|
---|
214 | <h3>Waiting for Delivery</h3>
|
---|
215 | <p>Your order is being processed. Please wait for a delivery person to accept the order.</p>
|
---|
216 | <div className={styles.spinner}></div>
|
---|
217 | </div>
|
---|
218 | </div>
|
---|
219 | )}
|
---|
220 | </div>
|
---|
221 | </div>
|
---|
222 | );
|
---|
223 | };
|
---|
224 |
|
---|
225 | export default DeliveryDetails;
|
---|