From d62b407ec6f1377ce4e27fd7e1a62c0f43a4c9c9 Mon Sep 17 00:00:00 2001 From: Nafisa Akter Date: Sun, 12 Nov 2023 16:06:12 +0200 Subject: [PATCH 1/3] parcel history --- src/components/ParcelHistory.js | 64 +++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/components/ParcelHistory.js b/src/components/ParcelHistory.js index 03f23ac..1539e40 100644 --- a/src/components/ParcelHistory.js +++ b/src/components/ParcelHistory.js @@ -1,7 +1,67 @@ -import React from "react"; +import React, { useState } from "react"; const ParcelHistory = () => { - return
; + // Simulated data for parcel history + const parcelHistory = [ + { + sender: "John Doe", + recipient: "Alice Smith", + readyForPickupDate: "2023-11-10 14:30", + pickupDate: "2023-11-12 09:15", + status: "Delivered", + location: "Parcel Locker A", + retrievalCode: "12345", + }, + { + sender: "John Doe", + recipient: "Alice Smith", + readyForPickupDate: "2023-11-10 14:30", + pickupDate: "2023-11-12 09:15", + status: "Delivered", + location: "Parcel Locker A", + retrievalCode: "12345", + }, + // Add more parcel history entries as needed + ]; + + return ( +
+

+ Parcel Information and History +

+
+ {parcelHistory.map((parcel, index) => ( +
+

+ Sender: {parcel.sender} +

+

+ Recipient: {parcel.recipient} +

+

+ Ready for Pickup: {parcel.readyForPickupDate} +

+

+ Picked up: {parcel.pickupDate} +

+

+ Status: {parcel.status} +

+ {parcel.status === "Ready for Pickup" && ( +
+

+ Location: {parcel.location} +

+

+ Retrieval Code: {parcel.retrievalCode} +

+
+ )} +
+ ))} +
+
+ ); }; export default ParcelHistory; From bf1aed9cc15e8f6129a9b21eec7db52da95eafa7 Mon Sep 17 00:00:00 2001 From: Nafisa Akter Date: Sat, 18 Nov 2023 16:39:33 +0200 Subject: [PATCH 2/3] worked on sendparcel from main, mergin to sendparcel branch --- src/App.js | 8 +- src/components/parcels/SendParcel.js | 386 ++++++++++++++++++++------- 2 files changed, 293 insertions(+), 101 deletions(-) diff --git a/src/App.js b/src/App.js index 02e5646..dc18d5f 100644 --- a/src/App.js +++ b/src/App.js @@ -35,7 +35,7 @@ const App = () => { const handleLogin = async (credentials) => { try { - const response = await fetch("http://localhost:5005/api/user/signin", { + const response = await fetch("http://localhost:8005/api/user/signin", { method: "POST", headers: { "Content-Type": "application/json", @@ -64,8 +64,8 @@ const App = () => { // Define the onSignup function const onSignup = async (formData) => { try { - const response = await fetch('http://localhost:5005/api/user/signup', { - method: 'POST', + const response = await fetch("http://localhost:8005/api/user/signup", { + method: "POST", headers: { "Content-Type": "application/json", }, @@ -135,7 +135,7 @@ const App = () => { {isAuthenticated && ( <> } /> - } /> + } /> } /> } /> } /> diff --git a/src/components/parcels/SendParcel.js b/src/components/parcels/SendParcel.js index a5ef437..8cd0f2c 100644 --- a/src/components/parcels/SendParcel.js +++ b/src/components/parcels/SendParcel.js @@ -1,116 +1,308 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; const SendParcel = () => { - const [parcelInfo, setParcelInfo] = useState({ + const [parcelData, setParcelData] = useState({ + parcelDescription: "", + parcelWeight: "", + length: "", width: "", height: "", - depth: "", - mass: "", - recipientId: "", - senderId: "", + status: "awaiting pickup", + recipientName: "", + recipientAddress: "", + recipientPhoneNumber: "", + senderName: "", + senderAddress: "", + senderPhoneNumber: "", }); + useEffect(() => { + // This block will run after the state has been updated + console.log("Updated parcelData:", parcelData); + }, [parcelData]); // Dependency array ensures this effect runs when parcelData changes + const handleChange = (e) => { const { name, value } = e.target; - setParcelInfo({ ...parcelInfo, [name]: value }); + setParcelData((prevData) => ({ + ...prevData, + [name]: value, + })); + + // }; - const handleSubmit = (e) => { + const handleSubmit = async (e) => { e.preventDefault(); - // You can send the parcel information to your backend or perform any other action here - console.log(parcelInfo); + // Handle form submission, you can send parcelData to your backend or perform other actions. + console.log(parcelData); + console.log( + "before sending post request", + parcelData.senderName, + parcelData.senderPhoneNumber, + parcelData.recipientName, + parcelData.recipientPhoneNumber + ); + const senderName = parcelData.senderName; + const senderPhoneNumber = parcelData.senderPhoneNumber; + const recipientName = parcelData.recipientName; + const recipientPhoneNumber = parcelData.recipientPhoneNumber; + + try { + const userValidationResponse = await fetch( + "http://localhost:8005/api/validation/validateUsers", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + senderName, + senderPhoneNumber, + recipientName, + recipientPhoneNumber, + }), + } + ); + + if (!userValidationResponse.ok) { + throw new Error("User validation failed"); + } + + // const userValidationData = await userValidationResponse.json(); + // console.log("User validation successful:", userValidationData); + + const data = await userValidationResponse.json(); + console.log("Sender ID:", data.senderId); + console.log("Receiver ID:", data.receiverId); + + //console.error("Response Status:", userValidationResponse.status); + //console.error("Response Text:", await userValidationResponse.text()); + + // Extract senderId and receiverId from the validation data + const { senderId, receiverId } = data; + const parcelDescription = parcelData.parcelDescription; + const parcelWeight = parcelData.parcelWeight; + const parcelDimension = { + length: parcelData.depth, + width: parcelData.width, + height: parcelData.height, + }; + + const status = parcelData.status; + + // Prepare parcel data with additional information + const parcelDataWithValidation = { + parcelDescription, + parcelWeight, + parcelDimension, + status, + sender: senderId, // Use the senderId obtained from validation + receiver: receiverId, // Use the receiverId obtained from validation + }; + console.log(parcelDataWithValidation); + + // Second API request to create a new parcel + const parcelCreationResponse = await fetch( + "http://localhost:8005/api/sendParcel/parcels", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(parcelDataWithValidation), + } + ); + + if (!parcelCreationResponse.ok) { + throw new Error("Failed to create parcel"); + } + + const parcelCreationData = await parcelCreationResponse.json(); + console.log("Parcel created successfully:", parcelCreationData); + + //console.error("Full Response Text:", await parcelCreationResponse.text()); + + // Continue with the form submission or other actions + } catch (error) { + console.error("Error creating parcel:", error.message); + + // Handle error (show an error message to the user, etc.) + } }; return ( -
-
-
- - Send Your Parcel - - - Please fill in the parcel and sender details below - -
-
-
- Width (cm) - -
-
- Height (cm) - -
-
-
-
- Depth (cm) - -
-
- Mass (kg) - -
-
-
-
- Sender ID - -
-
- Recipient ID - -
-
- -
+
+

Send Parcel

+ +
+ {/* Parcel Size and weight */} +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + {/* Recipient Information */} +

Recipient Information

+
+
+ + +
+
+ + +
+
+ + +
-
+ + {/* Sender Information */} +

Sender Information

+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +