From 0c64b5be790391dcf29e3213da707e6409019f60 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Fri, 14 Mar 2025 17:03:54 +0000 Subject: [PATCH] WOOHOO --- app/dashboard/shipping/page.tsx | 80 +++++++++++++++++++++------- components/layout/sidebar.tsx | 19 ++++++- components/modals/shipping-modal.tsx | 72 ++++++++++++++----------- lib/auth-utils.ts | 55 +++++++++++++++++++ lib/shippingHelper.ts | 8 ++- 5 files changed, 183 insertions(+), 51 deletions(-) create mode 100644 lib/auth-utils.ts diff --git a/app/dashboard/shipping/page.tsx b/app/dashboard/shipping/page.tsx index 64cd736..b09372d 100644 --- a/app/dashboard/shipping/page.tsx +++ b/app/dashboard/shipping/page.tsx @@ -27,12 +27,18 @@ export default function ShippingPage() { const [loading, setLoading] = useState(true); const [modalOpen, setModalOpen] = useState(false); const [editing, setEditing] = useState(false); + const [refreshTrigger, setRefreshTrigger] = useState(0); const router = useRouter(); + const refreshShippingMethods = () => { + setRefreshTrigger(prev => prev + 1); + }; + useEffect(() => { const fetchShippingMethodsData = async () => { try { + setLoading(true); const authToken = document.cookie .split("; ") .find((row) => row.startsWith("Authorization=")) @@ -65,23 +71,41 @@ export default function ShippingPage() { }; fetchShippingMethodsData(); - }, []); + }, [refreshTrigger]); // Add refreshTrigger as a dependency const handleAddShipping = async () => { if (!newShipping.name || !newShipping.price) return; try { - const authToken = document.cookie.split("Authorization=")[1]; - const updatedMethods: ShippingMethod[] = await addShippingMethod( + setLoading(true); + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + console.error("No auth token found"); + return; + } + + await addShippingMethod( authToken, newShipping ); - setShippingMethods(updatedMethods); - setNewShipping({ name: "", price: 0 }); // No `_id` needed for new entry + // Close modal and reset form before refreshing to avoid UI delays setModalOpen(false); + setNewShipping({ name: "", price: 0 }); + + // Refresh the list after adding + refreshShippingMethods(); + + console.log("Shipping method added successfully"); } catch (error) { console.error("Error adding shipping method:", error); + alert("Failed to add shipping method. Please try again."); + } finally { + setLoading(false); } }; @@ -89,23 +113,37 @@ export default function ShippingPage() { if (!newShipping.name || !newShipping.price || !newShipping._id) return; // Ensure `_id` exists try { - const authToken = document.cookie.split("Authorization=")[1]; - const updatedShipping: ShippingMethod = await updateShippingMethod( + setLoading(true); + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + console.error("No auth token found"); + return; + } + + await updateShippingMethod( authToken, newShipping._id, newShipping ); - setShippingMethods((prevMethods) => - prevMethods.map((method) => - method._id === updatedShipping._id ? updatedShipping : method - ) - ); + // Close modal and reset form before refreshing to avoid UI delays + setModalOpen(false); setNewShipping({ name: "", price: 0 }); setEditing(false); - setModalOpen(false); + + // Refresh the list after updating + refreshShippingMethods(); + + console.log("Shipping method updated successfully"); } catch (error) { console.error("Error updating shipping method:", error); + alert("Failed to update shipping method. Please try again."); + } finally { + setLoading(false); } }; @@ -114,9 +152,7 @@ export default function ShippingPage() { const authToken = document.cookie.split("Authorization=")[1]; const response = await deleteShippingMethod(authToken, _id); if (response.success) { - setShippingMethods((prevMethods) => - prevMethods.filter((method) => method._id !== _id) - ); + refreshShippingMethods(); // Refresh the list after deleting } else { console.error("Deletion was not successful."); } @@ -141,7 +177,11 @@ export default function ShippingPage() {

Manage Shipping Options

- @@ -159,7 +199,11 @@ export default function ShippingPage() { {/* Shipping Modal */} setModalOpen(false)} + onClose={() => { + setNewShipping({ name: "", price: 0 }); + setEditing(false); + setModalOpen(false); + }} onSave={editing ? handleUpdateShipping : handleAddShipping} shippingData={newShipping} editing={editing} diff --git a/components/layout/sidebar.tsx b/components/layout/sidebar.tsx index aa89dc4..f7cb6bd 100644 --- a/components/layout/sidebar.tsx +++ b/components/layout/sidebar.tsx @@ -7,11 +7,28 @@ import { ShoppingCart, LogOut } from "lucide-react" import { NavItem } from "./nav-item" import { Button } from "@/components/ui/button" import { sidebarConfig } from "@/config/sidebar" +import { logoutUser } from "@/lib/auth-utils" +import { toast } from "sonner" const Sidebar: React.FC = () => { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false) const router = useRouter() + const handleLogout = async () => { + try { + // Show toast notification for better user experience + toast.success("Logging out..."); + + // Perform the logout + await logoutUser(); + + // The logoutUser function will handle the redirect + } catch (error) { + console.error("Error during logout:", error); + toast.error("Failed to logout. Please try again."); + } + }; + return ( <>