From 0d094d06604096e3d5ffeb3ff70ca92443f73b90 Mon Sep 17 00:00:00 2001 From: A-Hazzard Date: Mon, 4 Nov 2024 00:47:42 -0400 Subject: [PATCH] Hid client withdrawals and invoices url from admin navbar & panel --- components/InvoiceManagement.tsx | 74 +++++++++++++------------- components/Navbar.tsx | 91 ++++++++++++++++---------------- components/Panel.tsx | 86 +++++++++++++++--------------- 3 files changed, 125 insertions(+), 126 deletions(-) diff --git a/components/InvoiceManagement.tsx b/components/InvoiceManagement.tsx index 10a400d..ce7080a 100644 --- a/components/InvoiceManagement.tsx +++ b/components/InvoiceManagement.tsx @@ -80,50 +80,50 @@ const InvoiceManagement = () => { amountRange: "", }); const [sortConfig, setSortConfig] = useState({ field: '', direction: 'asc' }); - const [unsubscribe, setUnsubscribe] = useState<(() => void) | null>(null); useEffect(() => { - subscribeToInvoices(); + let unsubscribe: (() => void) | undefined; + + const setupSubscription = () => { + setLoading(true); + try { + const invoicesCollection = collection(db, "invoices"); + unsubscribe = onSnapshot(invoicesCollection, (snapshot) => { + const invoicesData: Invoice[] = snapshot.docs.map((doc) => { + const data = doc.data() as DocumentData; + return { + id: doc.id, + invoiceNumber: data.invoiceNumber, + description: data.description, + amount: parseFloat(data.amount) || 0, + date: data.date, + status: data.status, + userId: data.userId, + userEmail: data.userEmail, + createdAt: data.createdAt, + username: data.username, + country: data.country, + userName: data.username, + }; + }); + setInvoices(invoicesData); + setLoading(false); + }); + } catch (err) { + console.error("Error subscribing to invoices:", err); + setLoading(false); + } + }; + + setupSubscription(); + + // Cleanup subscription on unmount return () => { - // Cleanup subscription on component unmount if (unsubscribe) { unsubscribe(); } }; - }, [unsubscribe]); - - const subscribeToInvoices = () => { - setLoading(true); - try { - const invoicesCollection = collection(db, "invoices"); - const unsub = onSnapshot(invoicesCollection, (snapshot) => { - const invoicesData: Invoice[] = snapshot.docs.map((doc) => { - const data = doc.data() as DocumentData; - return { - id: doc.id, - invoiceNumber: data.invoiceNumber, - description: data.description, - amount: parseFloat(data.amount) || 0, - date: data.date, - status: data.status, - userId: data.userId, - userEmail: data.userEmail, - createdAt: data.createdAt, - username: data.username, - country: data.country, - userName: data.username, - }; - }); - setInvoices(invoicesData); - setLoading(false); - }); - - setUnsubscribe(() => unsub); - } catch (err) { - console.error("Error subscribing to invoices:", err); - setLoading(false); - } - }; + }, []); const handleAddInvoice = async () => { // Validation diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 42ad235..ce9bb26 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -79,33 +79,17 @@ const Navbar = () => { className="absolute top-4 right-4 cursor-pointer text-light" />