From 28ad0215058b67e925d1bfaa358cf1f36b68a589 Mon Sep 17 00:00:00 2001 From: Dishant Date: Tue, 9 Jul 2024 02:24:24 +0530 Subject: [PATCH] added useEffect to remove error mesage --- backend/index.js | 1 - frontend/src/pages/Send.jsx | 43 ++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/backend/index.js b/backend/index.js index b9fb179..6faaba9 100644 --- a/backend/index.js +++ b/backend/index.js @@ -11,7 +11,6 @@ const port = 8080; app.use(cors()); app.use(express.json()); app.use("/api", rootRouter); -console.log(mongoDBUri); ConnectToDB(mongoDBUri); app.listen(port, () => console.log(`Listening on port ${port}`)); diff --git a/frontend/src/pages/Send.jsx b/frontend/src/pages/Send.jsx index 038444e..44b496d 100644 --- a/frontend/src/pages/Send.jsx +++ b/frontend/src/pages/Send.jsx @@ -1,31 +1,51 @@ -import { useState } from "react"; +import { useState,useEffect } from "react"; import Input from "../components/Input"; import Button from "../components/Button"; import { useNavigate } from "react-router-dom"; import { useSendMoney } from "../hooks"; + export default function Send() { - const [sender, SetSender] = useState(""); - const [receiver, SetReceiver] = useState(""); - const [amount, SetAmount] = useState(""); + const [sender, setSender] = useState(""); + const [receiver, setReceiver] = useState(""); + const [amount, setAmount] = useState(""); + const [inputError, setInputError] = useState(""); const { loading, error, sendMoney } = useSendMoney(); const navigate = useNavigate(); + useEffect(() => { + if (inputError) { + const timer = setTimeout(() => { + setInputError(""); + }, 2000); + + return () => clearTimeout(timer); + } + }, [inputError]); const handleSubmit = async (e) => { e.preventDefault(); + setInputError(""); + if (!sender || !receiver || !amount) { + setInputError("All fields are required."); + return; + } + try { const response = await sendMoney(sender, receiver, amount); if (response) { navigate("/success"); } - } catch (error) {} + } catch (error) { + console.error(error); + } }; return (
-
+
{error && {error}} + {inputError && {inputError}}

Send Money

@@ -36,25 +56,28 @@ export default function Send() { Label="SenderId" Placeholder="Enter your id" name="senderId" + required={true} Value={sender} - onChange={(e) => SetSender(e.target.value)} + onChange={(e) => setSender(e.target.value)} /> SetReceiver(e.target.value)} + onChange={(e) => setReceiver(e.target.value)} /> SetAmount(e.target.value)} + onChange={(e) => setAmount(e.target.value)} />