diff --git a/src/hooks/useProducts.ts b/src/hooks/useProducts.ts index 53609fa..ef12004 100644 --- a/src/hooks/useProducts.ts +++ b/src/hooks/useProducts.ts @@ -1,39 +1,36 @@ -"use client"; +import { useState, useEffect } from 'react'; -import { useEffect, useState } from "react"; -import { Product, fetchProducts } from "@/lib/api/product"; +interface Product { + id: string; + name: string; + price: string; + imageSrc: string; +} export function useProducts() { - const [products, setProducts] = useState([]); - const [isLoading, setIsLoading] = useState(true); - const [error, setError] = useState(null); + const [products, setProducts] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); - useEffect(() => { - let isMounted = true; + useEffect(() => { + const fetchProducts = async () => { + try { + setLoading(true); + // Simulate fetch + const response = await fetch('/api/products'); + const data = await response.json(); + setProducts(data); + setError(null); + } catch (err) { + setError('Failed to fetch products'); + setProducts([]); + } finally { + setLoading(false); + } + }; - async function loadProducts() { - try { - const data = await fetchProducts(); - if (isMounted) { - setProducts(data); - } - } catch (err) { - if (isMounted) { - setError(err instanceof Error ? err : new Error("Failed to fetch products")); - } - } finally { - if (isMounted) { - setIsLoading(false); - } - } - } + fetchProducts(); + }, []); - loadProducts(); - - return () => { - isMounted = false; - }; - }, []); - - return { products, isLoading, error }; + return { products, loading, error }; }