All files / src/app/admin/products/new page.tsx

0% Statements 0/72
100% Branches 0/0
0% Functions 0/1
0% Lines 0/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
"use client";

import { useRouter } from "next/navigation";
import { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import { ProductFormSkeleton } from "@/components/ui/Skeleton";

const ProductForm = dynamic(
  () => import("@/components/features/admin/ProductForm").then((mod) => ({ default: mod.ProductForm })),
  {
    loading: () => <ProductFormSkeleton />,
    ssr: false, // Admin doesn't need SSR
  }
);

export default function NewProductPage() {
  const router = useRouter();
  const [categories, setCategories] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchCategories = async () => {
      try {
        const response = await fetch("/api/admin/categories");
        const result = await response.json();
        if (result.success) {
          setCategories(result.data);
        }
      } finally {
        setLoading(false);
      }
    };
    fetchCategories();
  }, []);

  const handleSubmit = async (data: {
    title: string;
    description: string;
    price: string | number;
    discountedPrice: string | number;
    stock: number;
    categoryId: string | number;
    sku: string;
    images?: Array<{ id: number; url: string; thumbnailUrl: string; order: number }>;
  }) => {
    const response = await fetch("/api/admin/products", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(data)});

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error || "Failed to create product");
    }

    const result = await response.json();
    if (result.success) {
      router.push(`/admin/products/${result.data.id}`);
    }
  };

  if (loading) return <div style={{ padding: "20px" }}>Loading...</div>;

  return (
    <div style={{ padding: "30px 0" }}>
      <div style={{ maxWidth: "900px", margin: "0 auto", paddingLeft: "30px", paddingRight: "30px" }}>
        <h1 style={{ marginBottom: "30px" }}>Create New Product</h1>
        <ProductForm categories={categories} onSubmit={handleSubmit} />
      </div>
    </div>
  );
}