# Products API Integration This document describes the integration of the Products API from `https://trantran.zenstores.com.vn/api/Products` into the React POS application. ## Overview The integration includes: - Environment configuration for API base URL - Axios-based API service layer - Redux actions and reducers for state management - Updated ProductList component with API integration - Error handling and loading states ## Files Created/Modified ### New Files Created: 1. **`.env`** - Environment variables ``` REACT_APP_API_BASE_URL=https://trantran.zenstores.com.vn/api/ ``` 2. **`src/services/api.js`** - Main API configuration with axios - Base axios instance with interceptors - Request/response logging - Error handling - Authentication token support 3. **`src/services/productsApi.js`** - Products API service - getAllProducts() - getProductById(id) - createProduct(productData) - updateProduct(id, productData) - deleteProduct(id) - searchProducts(query) - getCategories() - getBrands() - Bulk operations 4. **`src/core/redux/actions/productActions.js`** - Redux actions - Async actions for all CRUD operations - Loading states management - Error handling 5. **`src/core/redux/reducers/productReducer.js`** - Redux reducer - State management for products - Loading and error states - Search functionality 6. **`src/components/ApiTest.jsx`** - API testing component - Test API connectivity - Debug API responses ### Modified Files: 1. **`src/core/redux/reducer.jsx`** - Updated to use combineReducers - Added new product reducer - Maintained backward compatibility with legacy reducer 2. **`src/feature-module/inventory/productlist.jsx`** - Enhanced with API integration - Fetches data from API on component mount - Fallback to legacy data if API fails - Loading states and error handling - Real-time search functionality - Delete confirmation with API calls ## Usage ### Environment Setup 1. Ensure the `.env` file is in the project root with: ``` REACT_APP_API_BASE_URL=https://trantran.zenstores.com.vn/api/ ``` 2. Restart the development server after adding environment variables. ### Using the API Service ```javascript import { productsApi } from '../services/productsApi'; // Get all products const products = await productsApi.getAllProducts(); // Get product by ID const product = await productsApi.getProductById(123); // Create new product const newProduct = await productsApi.createProduct({ name: 'Product Name', price: 99.99, // ... other fields }); // Update product const updatedProduct = await productsApi.updateProduct(123, { name: 'Updated Name', price: 149.99 }); // Delete product await productsApi.deleteProduct(123); ``` ### Using Redux Actions ```javascript import { useDispatch, useSelector } from 'react-redux'; import { fetchProducts, createProduct, deleteProduct } from '../core/redux/actions/productActions'; const MyComponent = () => { const dispatch = useDispatch(); const { products, loading, error } = useSelector(state => state.products); // Fetch products useEffect(() => { dispatch(fetchProducts()); }, [dispatch]); // Create product const handleCreate = async (productData) => { try { await dispatch(createProduct(productData)); // Success handling } catch (error) { // Error handling } }; // Delete product const handleDelete = async (productId) => { try { await dispatch(deleteProduct(productId)); // Success handling } catch (error) { // Error handling } }; return (