132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
"use client"
|
|
|
|
import { useState, useEffect } from "react"
|
|
import type { Product } from "@/types/business"
|
|
|
|
// Mock products data
|
|
const mockProducts: Product[] = [
|
|
{
|
|
id: "1",
|
|
name: "Wireless Headphones",
|
|
category: "Audio",
|
|
description: "High-quality wireless headphones with noise cancellation",
|
|
buyPrice: 45.0,
|
|
sellPrice: 89.99,
|
|
stock: 25,
|
|
minStock: 5,
|
|
supplier: "Audio Tech Co",
|
|
sku: "WH-001",
|
|
createdAt: "2024-01-01",
|
|
updatedAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "Phone Cases",
|
|
category: "Accessories",
|
|
description: "Protective cases for various phone models",
|
|
buyPrice: 3.5,
|
|
sellPrice: 12.99,
|
|
stock: 150,
|
|
minStock: 20,
|
|
supplier: "Mobile Accessories Ltd",
|
|
sku: "PC-002",
|
|
createdAt: "2024-01-01",
|
|
updatedAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "Power Banks",
|
|
category: "Electronics",
|
|
description: "10000mAh portable power banks",
|
|
buyPrice: 18.0,
|
|
sellPrice: 34.99,
|
|
stock: 3,
|
|
minStock: 10,
|
|
supplier: "Power Solutions Inc",
|
|
sku: "PB-003",
|
|
createdAt: "2024-01-01",
|
|
updatedAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "4",
|
|
name: "USB Cables",
|
|
category: "Accessories",
|
|
description: "USB-C charging cables 6ft length",
|
|
buyPrice: 2.25,
|
|
sellPrice: 8.99,
|
|
stock: 0,
|
|
minStock: 15,
|
|
supplier: "Cable World",
|
|
sku: "UC-004",
|
|
createdAt: "2024-01-01",
|
|
updatedAt: "2024-01-01",
|
|
},
|
|
]
|
|
|
|
export function useProducts() {
|
|
const [products, setProducts] = useState<Product[]>([])
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
useEffect(() => {
|
|
// Simulate loading from localStorage or API
|
|
const savedProducts = localStorage.getItem("wholesale-products")
|
|
if (savedProducts) {
|
|
setProducts(JSON.parse(savedProducts))
|
|
} else {
|
|
setProducts(mockProducts)
|
|
localStorage.setItem("wholesale-products", JSON.stringify(mockProducts))
|
|
}
|
|
setLoading(false)
|
|
}, [])
|
|
|
|
const addProduct = (productData: Omit<Product, "id" | "createdAt" | "updatedAt">) => {
|
|
const newProduct: Product = {
|
|
...productData,
|
|
id: Date.now().toString(),
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
}
|
|
|
|
const updatedProducts = [...products, newProduct]
|
|
setProducts(updatedProducts)
|
|
localStorage.setItem("wholesale-products", JSON.stringify(updatedProducts))
|
|
}
|
|
|
|
const updateProduct = (productId: string, productData: Omit<Product, "id" | "createdAt" | "updatedAt">) => {
|
|
const updatedProducts = products.map((product) =>
|
|
product.id === productId ? { ...product, ...productData, updatedAt: new Date().toISOString() } : product,
|
|
)
|
|
setProducts(updatedProducts)
|
|
localStorage.setItem("wholesale-products", JSON.stringify(updatedProducts))
|
|
}
|
|
|
|
const deleteProduct = (productId: string) => {
|
|
const updatedProducts = products.filter((product) => product.id !== productId)
|
|
setProducts(updatedProducts)
|
|
localStorage.setItem("wholesale-products", JSON.stringify(updatedProducts))
|
|
}
|
|
|
|
const getProductById = (productId: string) => {
|
|
return products.find((product) => product.id === productId)
|
|
}
|
|
|
|
const getLowStockProducts = () => {
|
|
return products.filter((product) => product.stock <= product.minStock)
|
|
}
|
|
|
|
const getOutOfStockProducts = () => {
|
|
return products.filter((product) => product.stock === 0)
|
|
}
|
|
|
|
return {
|
|
products,
|
|
loading,
|
|
addProduct,
|
|
updateProduct,
|
|
deleteProduct,
|
|
getProductById,
|
|
getLowStockProducts,
|
|
getOutOfStockProducts,
|
|
}
|
|
}
|