'use client'
import React, { useState } from 'react';
import Link from 'next/link'; // Not directly used here, but kept for consistency
import Layout from '@/component/Layout'; // Corrected import path (use '@/components/Layout')
import ProductCard from '@/component/ProductCard'; // Corrected import path
import productsData from '@/component/data/produect'; // Corrected import path and file name (should be 'products.json')
import type { Product } from '@/types'; // Import Product type
// Function to generate a random ID (simple implementation for demo purposes)
const generateRandomId = (): number => {
return Math.floor(Math.random() * 1000000); // Generates a random number between 0 and 999999
};
// Dashboard component (renamed from HomePage for clarity based on your export)
const Dashboard: React.FC = () => {
// State to manage the list of products, initialized with data from JSON
const [products, setProducts] = useState<Product[]>(productsData);
const [newProduct, setNewProduct] = useState({
id: 0, // Temporary ID, will be overwritten with random ID
name: '',
price: 0,
image: '',
description: '',
});
// Handle input changes for the new product form
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setNewProduct((prev) => ({
...prev,
[name]: name === 'price' ? parseFloat(value) || 0 : value, // Parse price as a number
}));
};
// Handle form submission to add a new product with a random ID
const handleAddProduct = (e: React.FormEvent) => {
e.preventDefault();
const randomId = generateRandomId(); // Generate a random ID
const newProductWithId: Product = { ...newProduct, id: randomId }; // Add random ID to the new product
setProducts((prevProducts) => [...prevProducts, newProductWithId]); // Add to state
setNewProduct({ id: 0, name: '', price: 0, image: '', description: '' }); // Reset form
};
return (
{/* Section for adding a new product */}
Add New Product
Product Name
Price
Image URL
Description
Add Product
{/* Product Grid */}
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
{products.map((product: Product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
</Layout>
);
};
export default Dashboard; // Corrected export name (use Dashboard as per your query)
this code if create produect than
import productsData from '@/component/data/produect'; // Corrected import path and file name (should be 'products.json')
this file data updata and data updata than display custom message dispay and /// this over all process like save data in obj than run one function this time emtpy and retun true and only this function return true than next process