fix auth
This commit is contained in:
@@ -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 */}
|
||||
|
||||
Reference in New Issue
Block a user