39 lines
904 B
TypeScript
39 lines
904 B
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
interface Product {
|
|
id: string;
|
|
name: string;
|
|
price: string;
|
|
imageSrc: string;
|
|
}
|
|
|
|
export function useProduct(productId: string) {
|
|
const [product, setProduct] = useState<Product | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchProduct = async () => {
|
|
try {
|
|
setLoading(true);
|
|
// Simulate fetch
|
|
const response = await fetch(`/api/products/${productId}`);
|
|
const data = await response.json();
|
|
setProduct(data);
|
|
setError(null);
|
|
} catch (err) {
|
|
setError('Failed to fetch product');
|
|
setProduct(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
if (productId) {
|
|
fetchProduct();
|
|
}
|
|
}, [productId]);
|
|
|
|
return { product, loading, error };
|
|
}
|