Hilmi-Engineering / hilmi-marketplace

0 stars 0 forks source link

Centralize API Calls and Implement Boilerplate Backend Integrations #1

Open wendulem opened 5 months ago

wendulem commented 5 months ago

Overview

To improve maintainability and scalability, we need to refactor all direct axios calls scattered throughout our components into a single api.js service file. This file will handle all interactions with the backend and the Shopify API, ensuring a clean separation of concerns and making the API logic easier to manage and update.

Tasks

1. Create a Central API Service File

Objective: Centralize all external API calls into a single service module.

Details:

2. Define API Service Functions

Objective: Define and implement functions within api.js that make HTTP requests to the appropriate backend endpoints.

Details:

3. Integrate API Service with Components

Objective: Replace existing axios calls in components with calls to the functions defined in api.js.

Details:

4. Ensure Error Handling and Loading States are Managed

Objective: Enhance user experience by managing loading states and errors comprehensively.

Details:

Expected Outcome

Implementation Details

Here's an example of what the api.js might look like:


import axios from 'axios';

const BASE_URL = 'http://localhost:5000/api';  // Adjust based on actual backend URL

export const fetchProducts = async (params) => {
  try {
    const response = await axios.get(`${BASE_URL}/products`, { params });
    return response.data;
  } catch (error) {
    console.error('Error fetching products:', error);
    throw error;
  }
};

export const fetchProductById = async (productId) => {
  try {
    const response = await axios.get(`${BASE_URL}/products/${productId}`);
    return response.data;
  } catch (error) {
    console.error('Error fetching product details:', error);
    throw error;
  }
};

export const fetchVendorById = async (vendorId) => {
  try {
    const response = await axios.get(`${BASE_URL}/vendors/${vendorId}`);
    return response.data;
  } catch (error) {
    console.error('Error fetching vendor details:', error);
    throw error;
  }
};