Home.jsx 3.21 KB
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { productService } from '../services/api/productService';
import ProductCard from '../components/product/ProductCard';
import LoadingSpinner from '../components/common/LoadingSpinner';
import './home.css';

const Home = () => {
  const [featuredProducts, setFeaturedProducts] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState('');

  useEffect(() => {
    loadFeaturedProducts();
  }, []);

  const loadFeaturedProducts = async () => {
    try {
      setLoading(true);
      const response = await productService.getFeaturedProducts();
      setFeaturedProducts(response.products || []);
    } catch (error) {
      setError('Failed to load featured products');
      console.error('Error loading featured products:', error);
    } finally {
      setLoading(false);
    }
  };

  if (loading) {
    return <LoadingSpinner />;
  }

  return (
    <div className="home-page">
      {/* Hero Section */}
      <section className="hero-section">
        <div className="container">
          <div className="hero-content">
            <h1>Welcome to E-Commerce</h1>
            <p>Discover amazing products at great prices</p>
            <Link to="/products" className="btn btn-primary btn-large">
              Shop Now
            </Link>
          </div>
        </div>
      </section>

      {/* Featured Products */}
      <section className="featured-products-section">
        <div className="container">
          <h2>Featured Products</h2>
          {error && <div className="error-message">{error}</div>}
          
          <div className="products-grid">
            {featuredProducts.map(product => (
              <ProductCard key={product.id} product={product} />
            ))}
          </div>

          {featuredProducts.length === 0 && !error && (
            <div className="no-products">
              <p>No featured products available at the moment.</p>
              <Link to="/products" className="btn btn-secondary">
                Browse All Products
              </Link>
            </div>
          )}
        </div>
      </section>

      {/* Features Section */}
      <section className="features-section">
        <div className="container">
          <div className="features-grid">
            <div className="feature">
              <div className="feature-icon">๐Ÿšš</div>
              <h3>Free Shipping</h3>
              <p>Free shipping on orders over $50</p>
            </div>
            <div className="feature">
              <div className="feature-icon">๐Ÿ”’</div>
              <h3>Secure Payment</h3>
              <p>Your payment information is safe with us</p>
            </div>
            <div className="feature">
              <div className="feature-icon">โ†ฉ๏ธ</div>
              <h3>Easy Returns</h3>
              <p>30-day return policy for all items</p>
            </div>
            <div className="feature">
              <div className="feature-icon">๐Ÿ“ž</div>
              <h3>24/7 Support</h3>
              <p>We're here to help you anytime</p>
            </div>
          </div>
        </div>
      </section>
    </div>
  );
};

export default Home;