diff --git a/app/dashboard/orders/[id]/page.tsx b/app/dashboard/orders/[id]/page.tsx index f205cd2..8d968e7 100644 --- a/app/dashboard/orders/[id]/page.tsx +++ b/app/dashboard/orders/[id]/page.tsx @@ -25,6 +25,7 @@ import { } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Clipboard, Truck, Package } from "lucide-react"; +import { useRouter } from "next/navigation"; interface Order { orderId: string; @@ -49,6 +50,8 @@ export default function OrderDetailsPage() { const [productNames, setProductNames] = useState>({}); const [isPaid, setIsPaid] = useState(false); + + const router = useRouter(); const params = useParams(); const orderId = params?.id; @@ -110,6 +113,7 @@ export default function OrderDetailsPage() { setIsPaid(true); } } catch (err: any) { + router.push("/dashboard/orders"); setError(err.message); } finally { setLoading(false); diff --git a/app/dashboard/orders/page.tsx b/app/dashboard/orders/page.tsx index c121649..98148a9 100644 --- a/app/dashboard/orders/page.tsx +++ b/app/dashboard/orders/page.tsx @@ -1,8 +1,25 @@ +"use client"; + +import { useEffect } from "react"; +import { useRouter } from "next/navigation"; import Dashboard from "@/components/kokonutui/dashboard"; import { Package } from "lucide-react"; -import OrderTable from "@/components/order-table" +import OrderTable from "@/components/order-table"; export default function OrdersPage() { + const router = useRouter(); + + useEffect(() => { + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + router.push("/login"); + } + }, [router]); + return (
@@ -13,9 +30,8 @@ export default function OrdersPage() {
- {/* ✅ Order Table Component */}
); -} +} \ No newline at end of file diff --git a/app/dashboard/products/page.tsx b/app/dashboard/products/page.tsx index e6a6b52..8df8d75 100644 --- a/app/dashboard/products/page.tsx +++ b/app/dashboard/products/page.tsx @@ -33,10 +33,18 @@ export default function ProductsPage() { // Fetch products and categories useEffect(() => { + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + router.push("/login"); + return; + } + const fetchDataAsync = async () => { try { - const authToken = document.cookie.split("Authorization=")[1]; - const [fetchedProducts, fetchedCategories] = await Promise.all([ fetchProductData( `${process.env.NEXT_PUBLIC_API_URL}/products`, diff --git a/app/dashboard/shipping/page.tsx b/app/dashboard/shipping/page.tsx index 5766524..5c6b7a8 100644 --- a/app/dashboard/shipping/page.tsx +++ b/app/dashboard/shipping/page.tsx @@ -1,18 +1,11 @@ "use client"; import { useState, useEffect, ChangeEvent } from "react"; +import { useRouter } from "next/navigation"; import Layout from "@/components/kokonutui/layout"; import { Edit, Plus, Trash } from "lucide-react"; import { Button } from "@/components/ui/button"; import { ShippingModal } from "@/components/shipping-modal"; -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table"; import { Skeleton } from "@/components/ui/skeleton"; import { fetchShippingMethods, @@ -23,7 +16,7 @@ import { import { ShippingMethod, ShippingData } from "@/lib/types"; -import { ShippingTable } from "@/components/shipping-table" +import { ShippingTable } from "@/components/shipping-table"; export default function ShippingPage() { const [shippingMethods, setShippingMethods] = useState([]); @@ -35,18 +28,32 @@ export default function ShippingPage() { const [modalOpen, setModalOpen] = useState(false); const [editing, setEditing] = useState(false); + const router = useRouter(); + useEffect(() => { const fetchShippingMethodsData = async () => { try { - const authToken = document.cookie.split("Authorization=")[1]; - const fetchedMethods: ShippingMethod[] = await fetchShippingMethods(authToken); - - // Ensure `_id` is always a string - const sanitizedMethods: ShippingMethod[] = fetchedMethods.map((method) => ({ - ...method, - _id: method._id ?? "", // Default to empty string if undefined - })); - + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + router.push("/login"); + return; + } + + const fetchedMethods: ShippingMethod[] = await fetchShippingMethods( + authToken + ); + + const sanitizedMethods: ShippingMethod[] = fetchedMethods.map( + (method) => ({ + ...method, + _id: method._id ?? "", + }) + ); + setShippingMethods(sanitizedMethods); } catch (error) { console.error("Error loading shipping options:", error); @@ -54,17 +61,20 @@ export default function ShippingPage() { setLoading(false); } }; - + fetchShippingMethodsData(); }, []); const handleAddShipping = async () => { if (!newShipping.name || !newShipping.price) return; - + try { const authToken = document.cookie.split("Authorization=")[1]; - const updatedMethods: ShippingMethod[] = await addShippingMethod(authToken, newShipping); - + const updatedMethods: ShippingMethod[] = await addShippingMethod( + authToken, + newShipping + ); + setShippingMethods(updatedMethods); setNewShipping({ name: "", price: 0 }); // No `_id` needed for new entry setModalOpen(false); @@ -72,16 +82,22 @@ export default function ShippingPage() { console.error("Error adding shipping method:", error); } }; - + const handleUpdateShipping = async () => { if (!newShipping.name || !newShipping.price || !newShipping._id) return; // Ensure `_id` exists - + try { const authToken = document.cookie.split("Authorization=")[1]; - const updatedShipping: ShippingMethod = await updateShippingMethod(authToken, newShipping._id, newShipping); - + const updatedShipping: ShippingMethod = await updateShippingMethod( + authToken, + newShipping._id, + newShipping + ); + setShippingMethods((prevMethods) => - prevMethods.map((method) => (method._id === updatedShipping._id ? updatedShipping : method)) + prevMethods.map((method) => + method._id === updatedShipping._id ? updatedShipping : method + ) ); setNewShipping({ name: "", price: 0 }); setEditing(false); @@ -131,11 +147,11 @@ export default function ShippingPage() { {/* Shipping Methods Table */} + shippingMethods={shippingMethods} + loading={loading} + onEditShipping={handleEditShipping} + onDeleteShipping={handleDeleteShipping} + /> {/* Shipping Modal */} diff --git a/app/dashboard/storefront/page.tsx b/app/dashboard/storefront/page.tsx index ca052ab..446ec23 100644 --- a/app/dashboard/storefront/page.tsx +++ b/app/dashboard/storefront/page.tsx @@ -32,6 +32,16 @@ export default function StorefrontPage() { // ✅ Fetch Storefront Data useEffect(() => { + const authToken = document.cookie + .split("; ") + .find((row) => row.startsWith("Authorization=")) + ?.split("=")[1]; + + if (!authToken) { + router.push("/login"); + return; + } + const fetchStorefront = async () => { try { setLoading(true); @@ -48,7 +58,9 @@ export default function StorefrontPage() { }, []); // ✅ Handle Form Input Changes - const handleInputChange = (e: ChangeEvent) => { + const handleInputChange = ( + e: ChangeEvent + ) => { setStorefront({ ...storefront, [e.target.name]: e.target.value }); }; @@ -134,12 +146,20 @@ export default function StorefrontPage() { {/* Buttons */}
- -
diff --git a/components/order-table.tsx b/components/order-table.tsx index 7ba94ec..6084e34 100644 --- a/components/order-table.tsx +++ b/components/order-table.tsx @@ -3,7 +3,7 @@ import { useState, useEffect } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; -import { Package, Eye } from "lucide-react"; +import { Eye } from "lucide-react"; import Link from "next/link"; import { fetchWithAuthClient } from "@/lib/client-utils"; // ✅ Import client-safe API helper import { toast } from "sonner"; @@ -17,10 +17,9 @@ interface Order { } export default function OrderTable() { - const [orders, setOrders] = useState([]); + const [orders, setOrders] = useState(null); const [loading, setLoading] = useState(true); - // ✅ Fetch Orders on Load useEffect(() => { const fetchOrders = async () => { try { @@ -28,6 +27,7 @@ export default function OrderTable() { setOrders(data); } catch (error) { toast.error("Failed to fetch orders."); + console.error("Error fetching orders:", error); } finally { setLoading(false); } @@ -59,7 +59,7 @@ export default function OrderTable() { Loading... )) - ) : orders.length > 0 ? ( + ) : orders && orders.length > 0 ? ( orders.map((order) => ( {order._id.slice(-6)} @@ -77,7 +77,7 @@ export default function OrderTable() { )) ) : ( - + No orders found. @@ -86,4 +86,4 @@ export default function OrderTable() { ); -} +} \ No newline at end of file