From 27f45973489a272fe61080d66d3d8b8607164c6c Mon Sep 17 00:00:00 2001 From: Md Asif Date: Tue, 24 Dec 2024 01:03:46 +0530 Subject: [PATCH] refactor: simplify notification state management across components --- src/pages/ChargeEdit.jsx | 18 +++----- src/pages/CheckInOutLog.jsx | 15 +++---- src/pages/KeySwap.jsx | 68 ++++++++----------------------- src/pages/LockerStatus.jsx | 24 ++++------- src/pages/LockersRegistration.jsx | 5 +-- 5 files changed, 40 insertions(+), 90 deletions(-) diff --git a/src/pages/ChargeEdit.jsx b/src/pages/ChargeEdit.jsx index bc65275..404d80b 100644 --- a/src/pages/ChargeEdit.jsx +++ b/src/pages/ChargeEdit.jsx @@ -20,11 +20,7 @@ function ChargeEdit() { penaltyAmountEdit: false, }); - const [notification, setNotification] = useState({ - visible: false, - message: "", - type: "", - }); + const [notification, setNotification] = useState({ message: "", type: "" }); const { setIsLoading } = useLoading(); const { t } = useTranslation(); @@ -36,7 +32,10 @@ function ChargeEdit() { const fetchCharges = async () => { try { setIsLoading(true); - const response = await lockerService.getCharges(productCode, interestCategory); + const response = await lockerService.getCharges( + productCode, + interestCategory + ); if (response.status === 200) { const { rent, penalty } = response.data; setChargeDetails({ @@ -47,7 +46,6 @@ function ChargeEdit() { }); } else { setNotification({ - visible: true, message: response.data.message, type: "error", }); @@ -55,7 +53,6 @@ function ChargeEdit() { } catch (error) { console.error(error); setNotification({ - visible: true, message: error.message, type: "error", }); @@ -114,13 +111,11 @@ function ChargeEdit() { ); if (response.status === 200) { setNotification({ - visible: true, message: response.data.message, type: "success", }); } else { setNotification({ - visible: true, message: response.data.message, type: "error", }); @@ -128,7 +123,6 @@ function ChargeEdit() { } catch (error) { console.error(error); setNotification({ - visible: true, message: error.message, type: "error", }); @@ -167,7 +161,7 @@ function ChargeEdit() { return (
- {notification.visible && } + {notification.message !== "" && }
{notification.type === "success" && ( diff --git a/src/pages/CheckInOutLog.jsx b/src/pages/CheckInOutLog.jsx index 3d673a8..ecb28b1 100644 --- a/src/pages/CheckInOutLog.jsx +++ b/src/pages/CheckInOutLog.jsx @@ -10,10 +10,7 @@ import { useLoading } from "../hooks/useLoading"; function CheckInOutLog() { const [time, setTime] = useState(null); const [checkType, setCheckType] = useState(""); - const [notification, setNotification] = useState({ - message: "", - type: "", - }); + const [notification, setNotification] = useState({ message: "", type: "" }); const location = useLocation(); const showToast = useToast(); @@ -29,7 +26,11 @@ function CheckInOutLog() { // Add your logic here try { setIsLoading(true); - const response = await lockerService.checkInOut(accountNumber, time, checkType); + const response = await lockerService.checkInOut( + accountNumber, + time, + checkType + ); if (response.status === 200) { setNotification({ message: response.data.message, type: "success" }); } else { @@ -42,7 +43,7 @@ function CheckInOutLog() { } finally { setIsLoading(false); } - } + }; return (
@@ -80,7 +81,7 @@ function CheckInOutLog() {
-
); diff --git a/src/pages/KeySwap.jsx b/src/pages/KeySwap.jsx index 2add86b..f7ac568 100644 --- a/src/pages/KeySwap.jsx +++ b/src/pages/KeySwap.jsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import { motion, AnimatePresence } from "motion/react"; +import { AnimatePresence } from "motion/react"; import { useTranslation } from "react-i18next"; import { useToast } from "../hooks/useToast"; import { useLoading } from "../hooks/useLoading"; @@ -10,17 +10,13 @@ import Button from "../components/Button"; import { lockerService } from "../services/locker.service"; import clsx from "clsx"; import FormBox from "../components/FormBox"; -import { Copy } from "lucide-react"; +import Notification from "../components/Notification"; function KeySwap() { const { t } = useTranslation(); const showToast = useToast(); const { isLoading, setIsLoading } = useLoading(); - const [notification, setNotification] = useState({ - visible: false, - message: "", - type: "", - }); + const [notification, setNotification] = useState({ message: "", type: "" }); const [keySwapDetails, setKeySwapDetails] = useState({ cabinetId: "", lockerId: "", @@ -117,20 +113,17 @@ function KeySwap() { ); if (response.status === 200) { setNotification({ - visible: true, message: response.data.message, type: "success", }); } else { setNotification({ - visible: true, message: response.data.message, type: "error", }); } } catch (error) { setNotification({ - visible: true, message: error.message, type: "error", }); @@ -169,51 +162,22 @@ function KeySwap() { return (
- - {notification.visible && ( - - {notification.message.split(":").map((msg, index) => { - return index === 1 ? ( - - {msg} - - ) : ( - {msg} - ); - })} - - + + {notification.message !== "" && } + +
+ {notification.type === "success" && ( +
)} - -
- {notification.type === "success" && ( -
- )} - -
- {formFields.map(renderField)} + +
+ {formFields.map(renderField)} +
+
-
-
- ) + ); } export default KeySwap; diff --git a/src/pages/LockerStatus.jsx b/src/pages/LockerStatus.jsx index cab2580..41990b4 100644 --- a/src/pages/LockerStatus.jsx +++ b/src/pages/LockerStatus.jsx @@ -24,11 +24,7 @@ function LockerStatus() { statusValid: true, }); const { isLoading, setIsLoading } = useLoading(); - const [notification, setNotification] = useState({ - visible: false, - message: "", - type: "", - }); + const [notification, setNotification] = useState({ message: "", type: "" }); const formFields = [ { @@ -89,13 +85,11 @@ function LockerStatus() { lockerDetails.status ); setNotification({ - visible: true, message: response.data.message, type: "success", }); } catch (error) { setNotification({ - visible: true, message: error.message, type: "error", }); @@ -135,18 +129,18 @@ function LockerStatus() { return (
- {notification.visible && } + {notification.message !== "" && }
- {notification.type === "success" && ( + {notification.type === "success" && (
)} - -
- {formFields.map(renderField)} -
-
); diff --git a/src/pages/LockersRegistration.jsx b/src/pages/LockersRegistration.jsx index afc274d..8f72406 100644 --- a/src/pages/LockersRegistration.jsx +++ b/src/pages/LockersRegistration.jsx @@ -17,7 +17,6 @@ function LockersRegistration() { const cabinetId = location.state?.cabinetId; const [submitting, setSubmitting] = useState(false); const [notification, setNotification] = useState({ - visible: false, message: "", type: "", }); @@ -109,14 +108,12 @@ function LockersRegistration() { lockerValues ); setNotification({ - visible: true, message: `Cabinet creation successful. Cabinet ID: ${response.data.cabinetId}`, type: "success", }); } catch (error) { console.error(error); setNotification({ - visible: true, message: `Error registering lockers. ${error.message}`, type: "error", }); @@ -207,7 +204,7 @@ function LockersRegistration() { return (
- {notification.visible && } + {notification.message !== "" && }
{notification.type === "success" && (