This commit is contained in:
g
2025-02-07 14:09:21 +00:00
parent 717451ac9f
commit 6158f232db
6 changed files with 111 additions and 47 deletions

View File

@@ -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<Record<string, string>>({});
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);

View File

@@ -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 (
<Dashboard>
<div className="space-y-6">
@@ -13,9 +30,8 @@ export default function OrdersPage() {
</h1>
</div>
{/* ✅ Order Table Component */}
<OrderTable />
</div>
</Dashboard>
);
}
}

View File

@@ -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`,

View File

@@ -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<ShippingMethod[]>([]);
@@ -35,18 +28,32 @@ export default function ShippingPage() {
const [modalOpen, setModalOpen] = useState<boolean>(false);
const [editing, setEditing] = useState<boolean>(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 */}
<ShippingTable
shippingMethods={shippingMethods}
loading={loading}
onEditShipping={handleEditShipping}
onDeleteShipping={handleDeleteShipping}
/>
shippingMethods={shippingMethods}
loading={loading}
onEditShipping={handleEditShipping}
onDeleteShipping={handleDeleteShipping}
/>
</div>
{/* Shipping Modal */}

View File

@@ -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<HTMLInputElement | HTMLTextAreaElement>) => {
const handleInputChange = (
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
setStorefront({ ...storefront, [e.target.name]: e.target.value });
};
@@ -134,12 +146,20 @@ export default function StorefrontPage() {
{/* Buttons */}
<div className="sticky bottom-6 mt-8 flex justify-between">
<Button onClick={() => setBroadcastOpen(true)} className="gap-2 bg-emerald-600 hover:bg-emerald-700 text-white">
<Button
onClick={() => setBroadcastOpen(true)}
className="gap-2 bg-emerald-600 hover:bg-emerald-700 text-white"
>
<Send className="h-5 w-5" /> Broadcast Message
</Button>
<Button onClick={saveStorefront} disabled={saving} className="gap-2 bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-700 hover:to-indigo-700 text-white">
<Save className="h-5 w-5" /> {saving ? "Saving..." : "Save Configuration"}
<Button
onClick={saveStorefront}
disabled={saving}
className="gap-2 bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-700 hover:to-indigo-700 text-white"
>
<Save className="h-5 w-5" />{" "}
{saving ? "Saving..." : "Save Configuration"}
</Button>
</div>
</div>