Refactor
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
|
||||
"use client";
|
||||
|
||||
import { fetchData } from '@/lib/data-service';
|
||||
import { useEffect, useState } from "react";
|
||||
import { useParams } from "next/navigation";
|
||||
import Layout from "@/components/kokonutui/layout";
|
||||
import Layout from "@/components/layout/layout";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
@@ -55,10 +57,34 @@ export default function OrderDetailsPage() {
|
||||
const params = useParams();
|
||||
const orderId = params?.id;
|
||||
|
||||
const fetchProductNames = async (
|
||||
productIds: string[],
|
||||
authToken: string
|
||||
): Promise<Record<string, string>> => {
|
||||
const productNamesMap: Record<string, string> = {};
|
||||
try {
|
||||
const promises = productIds.map((id) =>
|
||||
fetchData(`${process.env.NEXT_PUBLIC_API_URL}/products/${id}`, {
|
||||
method: "GET",
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
})
|
||||
);
|
||||
const responses = await Promise.all(promises);
|
||||
const results = await Promise.all(responses.map((res) => res));
|
||||
|
||||
results.forEach((product, index) => {
|
||||
productNamesMap[productIds[index]] = product.name || "Unknown Product";
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch product names:", err);
|
||||
}
|
||||
return productNamesMap;
|
||||
};
|
||||
|
||||
const handleMarkAsPaid = async () => {
|
||||
try {
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
const response = await fetch(
|
||||
const response = await fetchData(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}`,
|
||||
{
|
||||
method: "PUT",
|
||||
@@ -70,7 +96,7 @@ export default function OrderDetailsPage() {
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
if (response && response.message === "Order status updated successfully") {
|
||||
setIsPaid(true); // Update isPaid state
|
||||
setOrder((prevOrder) => (prevOrder ? { ...prevOrder, status: "paid" } : null)); // Update order status
|
||||
console.log("Order marked as paid successfully.");
|
||||
@@ -92,7 +118,7 @@ export default function OrderDetailsPage() {
|
||||
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
|
||||
const res = await fetch(
|
||||
const res = await fetchData(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}`,
|
||||
{
|
||||
method: "GET",
|
||||
@@ -100,9 +126,9 @@ export default function OrderDetailsPage() {
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) throw new Error("Failed to fetch order details");
|
||||
if (!res) throw new Error("Failed to fetch order details");
|
||||
|
||||
const data: Order = await res.json();
|
||||
const data: Order = await res;
|
||||
setOrder(data);
|
||||
|
||||
const productIds = data.products.map((product) => product.productId);
|
||||
@@ -120,32 +146,8 @@ export default function OrderDetailsPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const fetchProductNames = async (
|
||||
productIds: string[],
|
||||
authToken: string
|
||||
): Promise<Record<string, string>> => {
|
||||
const productNamesMap: Record<string, string> = {};
|
||||
try {
|
||||
const promises = productIds.map((id) =>
|
||||
fetch(`${process.env.NEXT_PUBLIC_API_URL}/products/${id}`, {
|
||||
method: "GET",
|
||||
headers: { Authorization: `Bearer ${authToken}` },
|
||||
})
|
||||
);
|
||||
const responses = await Promise.all(promises);
|
||||
const results = await Promise.all(responses.map((res) => res.json()));
|
||||
|
||||
results.forEach((product, index) => {
|
||||
productNamesMap[productIds[index]] = product.name || "Unknown Product";
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch product names:", err);
|
||||
}
|
||||
return productNamesMap;
|
||||
};
|
||||
|
||||
fetchOrderDetails();
|
||||
}, [orderId]);
|
||||
}, [orderId]);
|
||||
|
||||
const handleAddTracking = async () => {
|
||||
if (!trackingNumber) return;
|
||||
@@ -153,7 +155,7 @@ export default function OrderDetailsPage() {
|
||||
try {
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
|
||||
const res = await fetch(
|
||||
const res = await fetchData(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}/tracking`,
|
||||
{
|
||||
method: "PUT",
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Dashboard from "@/components/kokonutui/dashboard";
|
||||
import Dashboard from "@/components/dashboard/dashboard";
|
||||
import { Package } from "lucide-react";
|
||||
import OrderTable from "@/components/order-table";
|
||||
import OrderTable from "@/components/tables/order-table";
|
||||
|
||||
export default function OrdersPage() {
|
||||
const router = useRouter();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Dashboard from "@/components/kokonutui/dashboard";
|
||||
import Content from "@/components/kokonutui/content";
|
||||
import { fetchServer } from "@/lib/server-utils"
|
||||
import Dashboard from "@/components/dashboard/dashboard";
|
||||
import Content from "@/components/dashboard/content";
|
||||
import { fetchServer } from '@/lib/server-service';
|
||||
|
||||
// ✅ Corrected Vendor Type
|
||||
interface Vendor {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState, useEffect, ChangeEvent } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Layout from "@/components/kokonutui/layout";
|
||||
import Layout from "@/components/layout/layout";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Product } from "@/models/products";
|
||||
import { Plus } from "lucide-react";
|
||||
@@ -11,8 +11,8 @@ import {
|
||||
saveProductData,
|
||||
deleteProductData,
|
||||
} from "@/lib/productData";
|
||||
import { ProductModal } from "@/components/product-modal";
|
||||
import ProductTable from "@/components/product-table";
|
||||
import { ProductModal } from "@/components/modals/product-modal";
|
||||
import ProductTable from "@/components/tables/product-table";
|
||||
|
||||
export default function ProductsPage() {
|
||||
const router = useRouter();
|
||||
@@ -34,14 +34,14 @@ export default function ProductsPage() {
|
||||
// Fetch products and categories
|
||||
useEffect(() => {
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
|
||||
if (!authToken) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
if (!authToken) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchDataAsync = async () => {
|
||||
try {
|
||||
@@ -55,8 +55,16 @@ export default function ProductsPage() {
|
||||
authToken
|
||||
),
|
||||
]);
|
||||
|
||||
setProducts(fetchedProducts);
|
||||
|
||||
console.log("Fetched Products:", fetchedProducts);
|
||||
|
||||
// Ensure all products have tieredPricing
|
||||
const processedProducts = fetchedProducts.map((product: Product) => ({
|
||||
...product,
|
||||
tieredPricing: product.tieredPricing || [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
}));
|
||||
|
||||
setProducts(processedProducts);
|
||||
setCategories(fetchedCategories);
|
||||
} catch (error) {
|
||||
console.error("Error loading data:", error);
|
||||
@@ -150,10 +158,12 @@ export default function ProductsPage() {
|
||||
const handleEditProduct = (product: Product) => {
|
||||
setProductData({
|
||||
...product,
|
||||
tieredPricing: product.tieredPricing.map(tier => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit: tier.pricePerUnit
|
||||
})),
|
||||
tieredPricing: product.tieredPricing
|
||||
? product.tieredPricing.map(tier => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit: tier.pricePerUnit
|
||||
}))
|
||||
: [{ minQuantity: 1, pricePerUnit: 0 }], // Fallback if undefined
|
||||
});
|
||||
setEditing(true);
|
||||
setModalOpen(true);
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
import { useState, useEffect, ChangeEvent } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Layout from "@/components/kokonutui/layout";
|
||||
import Layout from "@/components/layout/layout";
|
||||
import { Edit, Plus, Trash } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ShippingModal } from "@/components/shipping-modal";
|
||||
import { ShippingModal } from "@/components/modals/shipping-modal";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import {
|
||||
fetchShippingMethods,
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
|
||||
import { ShippingMethod, ShippingData } from "@/lib/types";
|
||||
|
||||
import { ShippingTable } from "@/components/shipping-table";
|
||||
import { ShippingTable } from "@/components/tables/shipping-table";
|
||||
|
||||
export default function ShippingPage() {
|
||||
const [shippingMethods, setShippingMethods] = useState<ShippingMethod[]>([]);
|
||||
@@ -54,6 +54,8 @@ export default function ShippingPage() {
|
||||
})
|
||||
);
|
||||
|
||||
console.log("Fetched Shipping Methods:", sanitizedMethods);
|
||||
|
||||
setShippingMethods(sanitizedMethods);
|
||||
} catch (error) {
|
||||
console.error("Error loading shipping options:", error);
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
import { useState, useEffect, ChangeEvent } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Layout from "@/components/kokonutui/layout";
|
||||
import Layout from "@/components/layout/layout";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Save, Send, Key, MessageSquare, Shield } from "lucide-react";
|
||||
import { apiRequest } from "@/lib/storeHelper";
|
||||
import { toast, Toaster } from "sonner";
|
||||
import BroadcastDialog from "@/components/broadcast-dialog";
|
||||
import BroadcastDialog from "@/components/modals/broadcast-dialog";
|
||||
|
||||
// ✅ Define the Storefront Type
|
||||
interface Storefront {
|
||||
|
||||
Reference in New Issue
Block a user