Fake Store API Mini Project

📘 Project: Product Listing Application (React + API Integration)


🧩 Problem Statement

Build a React application that fetches product data from a public API and displays it in a responsive card layout.

Students must integrate an external REST API using Axios and manage application state using React Hooks (useState, useEffect).

The goal of this project is to understand:

  • API integration in React
  • State management using React Hooks
  • Rendering dynamic UI using .map()
  • Handling loading and error states
  • Creating reusable UI components

📦 Functional Requirements

1. Fetch Products from API

API Endpoint:

https://fakestoreapi.com/products

Requirements:

  • Fetch data using Axios
  • API call must be implemented inside useEffect
  • Store response data using useState

2. Display Products in Card Format

Each product card must display:

  • Product Image
  • Product Title
  • Product Price
  • Product Category
  • “View Details” Button (UI only, no navigation required)

Design Requirements:

  • Cards must be responsive
  • Use CSS Grid or Flexbox for layout
  • Clean and properly aligned UI

3. Loading State

  • Display “Loading…” while the API request is in progress

4. Error Handling

  • Display an appropriate error message if the API request fails

🧠 Non-Functional Requirements

  • Clean component structure
  • Proper key usage while mapping products
  • No console errors
  • Proper separation of JSX and CSS
  • Readable and well-formatted code

🏗 Technical Constraints

Students must use:

  • React Functional Components
  • useState
  • useEffect
  • Axios
  • CSS (No UI frameworks unless explicitly permitted)

🎯 Learning Objectives

After completing this assignment, students should be able to:

  • Integrate third-party APIs into a React application
  • Understand component lifecycle using hooks
  • Manage asynchronous data
  • Render dynamic UI components
  • Handle real-world UI states (loading and error)

🚀 Bonus (Optional Enhancements)

Students may optionally implement:

  • Search functionality
  • Category filtering
  • Pagination
  • Sorting by price
  • Product detail page using React Router

Interceptor

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import axios from 'axios';
// Add a request interceptor
axios.interceptors.request.use(
  config => {
    console.log("inside interceptor request", config)

    const token = "TOKEN FROM INTERCEPTORS"
    if (token) {
      console.log("INSIDE request use")
      config.headers['Authorization'] = 'Bearer ' + token
    }
    
    return config
  },
  error => {
    Promise.reject(error)
  }
)


axios.interceptors.response.use(
  config => {
    console.log("inside interceptor response", config)
    return config
  },
  error => {
    Promise.reject(error)
  }
)



const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

reportWebVitals();

App.js

import logo from './logo.svg';
import './App.css';
import { useEffect } from 'react';
import axios from 'axios';

function App() {

  useEffect(()=> {

    axios({
      method: 'get',
      url: 'http://localhost:1234/webservice.php'
    })
      .then(function (response) {
        console.log(response)
      });
    
  }, [])

  return (
    <>
    This is App component
    </>
  );
}

export default App;

webservice.php

<?php

header("Access-Control-Allow-Origin: *");

// echo "<pre>";
// print_r($_SERVER);

echo json_encode(["test" => "Hello world...."]);

Routing

npm i react-router-dom 
npx create-react-app routingapp

App.js

import Home from './components/Home';
import Blogs from './components/Blogs';
import Contact from './components/Contact';
import NoPage from './components/NoPage';
import { BrowserRouter, Outlet, Route, Routes } from 'react-router-dom';
import Layout from './components/Layout';

function App() {
  return (
    <>
    <h1>HELLO WORLD</h1>
    <BrowserRouter>
      <Routes>
        <Route path="/admin" element={<Layout />}>
          <Route index element={<Home />} />
          <Route path="contact" element={<Contact />} />
          <Route path="*" element={<NoPage />} />
        </Route>
        <Route path="/user" element={<Layout />}>
          <Route index element={<Home />} />
          <Route path="contact" element={<Contact />} />
          <Route path="blogs" element={<Blogs />} />
          <Route path="test" element={<div>This is test component</div>} />
          <Route path="*" element={<NoPage />} />    
        </Route>
      </Routes>
    </BrowserRouter>
    </>

  );
}

export default App;

Layout.js

import { Outlet, Link } from "react-router-dom";

export default function Layout() {
  return (
    <>
      <nav>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/blogs">Blogs</Link>
          </li>
          <li>
            <Link to="/contact">Contact</Link>
          </li>
        </ul>
      </nav>
      <Outlet />
    </>
  )
}

Outlet is the block where router components get loaded