All files / src/pages SearchProductPage.tsx

53.84% Statements 21/39
41.66% Branches 5/12
28.57% Functions 4/14
55.26% Lines 21/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                                      2x 10x 10x 10x 10x             10x 10x     10x 6x       6x 6x 6x               10x 2x 2x 2x   2x 2x 2x                           10x                   10x                                                                                                                                                            
/**
 * @file SearchProductPage.tsx
 * @description
 * Product search interface allowing users to find products by name.
 *
 * **Features:**
 * - Real-time product search (minimum 3 characters)
 * - Display matching products in a list
 * - Click to view full product details
 * - Edit product functionality
 * - Help modal with contextual information
 *
 * **Search Flow:**
 * 1. User types product name (3+ characters)
 * 2. API returns matching products
 * 3. Click product to load full details
 * 4. Option to edit or cancel selection
 *
 * @component
 */
 
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import ProductService from '../api/ProductService';
import { useTranslation } from 'react-i18next';
import HelpModal from '../components/HelpModal';
import Header from '../components/Header';
import Footer from '../components/Footer';
import '../styles/tailwindCustom.css';
 
/**
 * Search product page component
 * @component
 * @returns {JSX.Element} Product search interface
 */
const SearchProductPage: React.FC = () => {
  const { t, i18n } = useTranslation(['translation', 'help']);
  const [searchQuery, setSearchQuery] = useState('');
  const [products, setProducts] = useState<{ id: number; name: string }[]>([]);
  const [selectedProduct, setSelectedProduct] = useState<{
    id: number;
    name: string;
    quantity?: number;
    price?: number;
    totalValue?: number;
  } | null>(null);
  const [isHelpOpen, setIsHelpOpen] = useState(false);
  const navigate = useNavigate();
 
  // Re-render on language changes (helps with help modal translations)
  useEffect(() => {
    const handleLanguageChange = () => {
      setIsHelpOpen((prev) => prev);
    };
 
    i18n.on('languageChanged', handleLanguageChange);
    return () => {
      i18n.off('languageChanged', handleLanguageChange);
    };
  }, [i18n]);
 
  /**
   * Handle product search with minimum 3 character requirement
   * Fetches matching products from backend API
   */
  const handleSearch = async (e: React.ChangeEvent<HTMLInputElement>) => {
    const query = e.target.value;
    setSearchQuery(query);
    setSelectedProduct(null);
 
    if (query.length >= 3) {
      try {
        const results = await ProductService.searchProductsByName(query);
        setProducts(results && results.length > 0 ? results : []);
      } catch (error) {
        console.error(t('searchProduct.error.fetch'), error);
        setProducts([]);
      }
    } else E{
      setProducts([]);
    }
  };
 
  /**
   * Load full product details when user selects from search results
   */
  const handleProductClick = async (productId: number) => {
    try {
      const productDetails = await ProductService.getProductById(productId);
      setSelectedProduct(productDetails);
    } catch (error) {
      console.error(t('searchProduct.error.details'), error);
      setSelectedProduct(null);
    }
  };
 
  return (
    <div className="flex flex-col min-h-screen bg-gray-50">
      <Header isLoggedIn={true} onLogout={() => navigate('/login')} />
 
      <div className="absolute top-4 left-1/2 transform -translate-x-1/2">
        <button
          onClick={() => setIsHelpOpen(true)}
          className="dashboard-button button-help"
          key={i18n.language}
        >
          {t('button', { ns: 'help' })}
        </button>
      </div>
 
      <HelpModal isOpen={isHelpOpen} onClose={() => setIsHelpOpen(false)} pageKey="searchProduct" />
 
      <main className="flex flex-col items-center w-full max-w-2xl p-6 mt-6 bg-white shadow-lg rounded mx-auto">
        <h2 className="text-xl font-semibold text-gray-700 mb-4">{t('searchProduct.subtitle')}</h2>
 
        <input
          type="text"
          placeholder={t('searchProduct.placeholder')}
          value={searchQuery}
          onChange={handleSearch}
          className="input-field"
        />
 
        {searchQuery.length >= 3 &&
          (products.length > 0 ? (
            <ul className="w-full mt-4 space-y-2">
              {products.map((product) => (
                <li
                  key={product.id}
                  className="p-2 bg-gray-100 border rounded hover:bg-gray-200 cursor-pointer"
                  onClick={() => handleProductClick(product.id)}
                >
                  {product.name}
                </li>
              ))}
            </ul>
          ) : (
            <p className="text-gray-500 text-center mt-4">{t('searchProduct.noResults')}</p>
          ))}
 
        {selectedProduct && (
          <div className="mt-6 p-4 bg-gray-100 border rounded shadow-md">
            <h3 className="text-lg font-semibold">{selectedProduct.name}</h3>
            <p className="text-gray-700">
              {t('searchProduct.details.quantity')}: {selectedProduct.quantity}
            </p>
            <p className="text-gray-700">
              {t('searchProduct.details.price')}: ${selectedProduct.price?.toFixed(2)}
            </p>
            <p className="text-gray-700">
              {t('searchProduct.details.totalValue')}: ${selectedProduct.totalValue?.toFixed(2)}
            </p>
 
            <div className="mt-4 flex justify-between space-x-4">
              <button className="button-confirmation button-confirmation-no" onClick={() => setSelectedProduct(null)}>
                {t('searchProduct.cancelButton')}
              </button>
              <button
                className="button-confirmation button-confirmation-yes"
                onClick={() => navigate(`/product/${selectedProduct.id}/edit`)}
              >
                {t('searchProduct.editButton', { name: selectedProduct.name })}
              </button>
            </div>
          </div>
        )}
      </main>
 
      <Footer />
    </div>
  );
};
 
export default SearchProductPage;