Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 4

4 months ago admin Reactjs

In the fourth part of this tutorial, we will fetch and display all the products on the home page, and add the navbar menu so we can move between pages.


Add the home component

Inside the home component, we fetch all the products from the backend and send them to the product list component.

                                                    
                                                                                                                
import React, { useEffect, useState } from 'react'
import axios from 'axios'
import ProductList from './products/ProductList'

export default function Home() {
    const[products, setProducts] = useState([])

    useEffect(() => {
        const fetchAllProducts = async() => {
            try {
                const response = await axios.get('http://127.0.0.1:8000/api/products')
                setProducts(response.data.data)
            } catch (error) {
                console.log(error)
            }
        }
        fetchAllProducts()
    }, [])

    return (
        <ProductList products={products} />
    )
}


Add the product list component

Inside the product list component, we receive the products,  loop through, and send each product to the product list item component.

                                                        
                                                                                                                        
import React from 'react'
import ProductListItem from './ProductListItem'

export default function ProductList({products}) {
  return (
    <div className="row my-4">
        {
            products.map(product => <ProductListItem 
                product={product} key={product.id }/>)
        }
    </div>
  )
}


Add the product list item component

Inside the product list item component, we receive the product, display the details, and add the add-to-cart functionality.

                                                        
                                                                                                                        
import React from 'react'
import { useDispatch } from 'react-redux'
import { addToCart } from '../../redux/slices/cartSlice'

export default function ProductListItem({product}) {
    const dispatch = useDispatch()

    return (
        <div className='col-md-4 mb-2'>
            <div className="card h-100">
                <img src={product.product_image} alt="Product Image" 
                    className='card-img-top' />
                <div className="card-body">
                    <h5 className="card-title">
                        {product.product_name} 
                    </h5>
                    <p className="card-text">
                        {product.product_desc} 
                    </p>
                    <p>
                        <span className="fw-bold text-danger">
                            ${product.product_price}    
                        </span> 
                    </p>
                    <button className="btn btn-dark"
                        onClick={() => dispatch(addToCart(product))}>
                        <i className="bi bi-cart-check"></i> add to cart
                    </button>
                </div>
            </div>
        </div>
    )
}


Add the header component

Inside the Header Component, we have the navigation menu.

                                                        
                                                                                                                        
import React from 'react'
import { useSelector } from 'react-redux'
import { Link } from 'react-router-dom'

export default function Header() {
    const { cartItems } = useSelector(state => state.cart)
    return (
        <nav className="navbar navbar-expand-lg bg-body-tertiary">
            <div className="container-fluid">
                <Link className="navbar-brand" to="/">
                    <i className="bi bi-cart h1"></i>
                </Link>
                <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                </button>
                <div className="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul className="navbar-nav mx-auto mb-2 mb-lg-0">
                        <li className="nav-item">
                            <Link className="nav-link" to="/">
                                <i className="bi bi-house"></i> Home
                            </Link>
                        </li>
                        <li className="nav-item">
                            <Link className="nav-link" to="/cart">
                                <i className="bi bi-cart-check"></i> Cart ({cartItems.length})
                            </Link>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    )
}

Related Tuorials

Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 5

In the last part of this tutorial, we will display the cart items, add the ability to increment/decr...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 3

In the third part of this tutorial, we will start coding the front end, first, we will install the p...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 2

In the second part of this tutorial, we will create the product and payment controllers, and later w...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 1

In this tutorial, we will create a shopping cart using React js Laravel 11 and Stripe payment gatewa...


How to Use Rich Text Editor in React js

In this lesson, we will see how to use rich text editor in React JS, let's assume that we have a com...


How to Download a File from the Server Using Laravel and React js

In this tutorial, we will see how to download a file from the server using Laravel and React js, let...


How to Add a Class on Hover in React js

In this lesson, we will see how to add a class on hover in React js, let's assume that we have a boo...


Drag and Drop Image and File Upload Using React and Laravel

In this tutorial, we will see how to upload files using drag and drop in React js and Laravel, first...


API Authentication Using Laravel Sanctum and React js Part 3

In the third part of this tutorial, we will register and log in the user, get the access token, and...


API Authentication Using Laravel Sanctum and React js Part 2

In the second part of this tutorial, we will start handling the frontend first, we will create the r...