From 2ddd7b68d883bfa7c77ae2c087cc0dc90f70ca2b Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sat, 20 Jul 2024 03:45:53 +0530 Subject: [PATCH] fixed --- backend/Schemas/Products.schema.js | 4 + backend/Schemas/User.Schema.js | 1 + backend/index.js | 5 +- src/components/PForm.js | 131 +++++++++++++++++++++-------- 4 files changed, 103 insertions(+), 38 deletions(-) diff --git a/backend/Schemas/Products.schema.js b/backend/Schemas/Products.schema.js index d8a2ffe..8565efb 100644 --- a/backend/Schemas/Products.schema.js +++ b/backend/Schemas/Products.schema.js @@ -28,6 +28,10 @@ const ProductSchema = new mongoose.Schema({ ref: "User", required: true, }, + notificationPeriod: { + type: Number, + required: true, + }, }); const Product = mongoose.model("Product", ProductSchema); diff --git a/backend/Schemas/User.Schema.js b/backend/Schemas/User.Schema.js index 9dfa46d..a2302fa 100644 --- a/backend/Schemas/User.Schema.js +++ b/backend/Schemas/User.Schema.js @@ -32,6 +32,7 @@ const UserSchema = new mongoose.Schema({ resetPasswordExpires: { type: Date, }, + }); const User = mongoose.model("User", UserSchema); diff --git a/backend/index.js b/backend/index.js index b3c7f7d..b1daba1 100644 --- a/backend/index.js +++ b/backend/index.js @@ -229,10 +229,10 @@ app.get("/products/:barcode", async (req, res) => { }); app.post("/add-product", authenticateUser, async (req, res) => { - const { product_name, barcode, mfd, expiry_date, product_info } = req.body; + const { product_name, barcode, mfd, expiry_date, product_info ,notificationPeriod } = req.body; const userId = req.user.userId; - if (!product_name || !barcode || !mfd || !expiry_date || !product_info) { + if (!product_name || !barcode || !mfd || !expiry_date || !product_info || !notificationPeriod) { return res.status(400).send({ message: "All fields are required" }); } @@ -257,6 +257,7 @@ app.post("/add-product", authenticateUser, async (req, res) => { expiry_date: new Date(expiry_date), product_info, addedBy: userId, + notificationPeriod, }); try { diff --git a/src/components/PForm.js b/src/components/PForm.js index 8c036be..e4c6f7f 100644 --- a/src/components/PForm.js +++ b/src/components/PForm.js @@ -15,20 +15,61 @@ const PForm = () => { product_info: "", }); const [loading, setLoading] = useState(false); + const [notificationMessage, setNotificationMessage] = useState(""); + const [notificationTimeout, setNotificationTimeout] = useState(null); + const [canRedirect, setCanRedirect] = useState(false); useEffect(() => { toast.info("Add your product here."); - }, []); + return () => { + if (notificationTimeout) { + clearTimeout(notificationTimeout); + } + }; + }, [notificationTimeout]); const handleChange = (e) => { setProductData({ ...productData, [e.target.name]: e.target.value }); }; + const handleNotification = () => { + const { mfd, expiry_date } = productData; + if (!mfd || !expiry_date ) { + return toast.error("Please set both Manufacturing and Expiry dates."); + } + + const mfdDate = new Date(mfd); + const expiryDate = new Date(expiry_date); + const diffDays = (expiryDate - mfdDate) / (1000 * 60 * 60 * 24); + + let notifyDays; + if (diffDays > 3) { + notifyDays = 3; + } else { + notifyDays = 1; + } + + if (!notificationTimeout) { + alert(`Set notification for ${notifyDays} days`); + const timeoutId = setTimeout(() => { + setNotificationMessage(""); + setCanRedirect(true); + setNotificationTimeout(null); + }, 50000); + setNotificationTimeout(timeoutId); + } else { + alert(`Set notification for ${notifyDays} days`); + } + + if (canRedirect && notificationMessage === "") { + window.location.href = "/user/notifications"; + } + }; + const handleStore = async (e) => { e.preventDefault(); setLoading(true); - - // Ensure date fields are in yyyy-mm-dd format + const formattedProductData = { ...productData, mfd: productData.mfd @@ -38,33 +79,49 @@ const PForm = () => { ? new Date(productData.expiry_date).toISOString().split("T")[0] : "", }; - + + const { mfd, expiry_date } = formattedProductData; + const mfdDate = new Date(mfd); + const expiryDate = new Date(expiry_date); + const diffDays = (expiryDate - mfdDate) / (1000 * 60 * 60 * 24); + + let notifyDays; + if (diffDays > 3) { + notifyDays = 5; + } else { + notifyDays = 1; + } + + const productDataWithNotification = { + ...formattedProductData, + notificationPeriod: notifyDays, + }; + if (formattedProductData.expiry_date <= formattedProductData.mfd) { setLoading(false); return toast.error( "Manufacturing date cannot be greater than or equal to expiry date." ); } - + try { - // Get the token from localStorage const token = localStorage.getItem("token"); - + if (!token) { setLoading(false); return toast.error("Authorization token is missing."); } - - const response = await axios.post( + + await axios.post( "https://smartserver-production.up.railway.app/add-product", - formattedProductData, + productDataWithNotification, { headers: { Authorization: `Bearer ${token}`, }, } ); - + toast.success("Product added successfully!"); setProductData({ product_name: "", @@ -75,7 +132,6 @@ const PForm = () => { }); } catch (error) { if (error.response) { - // Server responded with a status other than 2xx if (error.response.status === 409) { toast.error("Duplicate product. Please check and try again."); } else { @@ -85,22 +141,15 @@ const PForm = () => { }` ); } - - console.error("Response data:", error.response.data); - console.error("Response status:", error.response.status); - console.error("Response headers:", error.response.headers); } else if (error.request) { - // Request was made but no response was received toast.error("No response from server."); } else { - // Something else caused the error toast.error(`Error: ${error.message}`); } } finally { setLoading(false); } }; - return ( <> @@ -212,23 +261,33 @@ const PForm = () => {
- +
+ + +
+