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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x | /**
* @file DeleteProductPage.tsx
* @description
* Product deletion interface with two-step confirmation.
*
* **Features:**
* - Search products by name (3+ characters)
* - Click to select from search results
* - Two-step confirmation process
* - Real-time search suggestions
* - Success/error messaging
* - Help modal support
*
* **Deletion Workflow:**
* 1. Search and select product
* 2. First confirmation (are you sure?)
* 3. Second confirmation (final check)
* 4. Delete product from API
*
* @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';
/**
* Delete product page component
* @component
* @returns {JSX.Element} Product deletion interface
*/
const DeleteProductPage: React.FC = () => {
const { t, i18n } = useTranslation(['translation', 'help']);
const navigate = useNavigate();
const [searchQuery, setSearchQuery] = useState('');
const [products, setProducts] = useState<{ id: number; name: string }[]>([]);
const [selectedProduct, setSelectedProduct] = useState<{ id: number; name: string } | null>(null);
const [confirmation, setConfirmation] = useState<'first' | 'second' | null>(null);
const [message, setMessage] = useState('');
const [isHelpOpen, setIsHelpOpen] = useState(false);
// Re-render help button on language changes
useEffect(() => {
const handleLanguageChange = () => {
setIsHelpOpen((prev) => prev);
};
i18n.on('languageChanged', handleLanguageChange);
return () => {
i18n.off('languageChanged', handleLanguageChange);
};
}, [i18n]);
/**
* Search products by name (minimum 3 characters)
*/
const handleSearch = async (e: React.ChangeEvent<HTMLInputElement>) => {
const query = e.target.value;
setSearchQuery(query);
setSelectedProduct(null);
setMessage('');
if (query.length >= 3) {
try {
const results = await ProductService.searchProductsByName(query);
if (!results || results.length === 0) {
setProducts([]);
setMessage(t('deleteProduct.messages.notFound'));
} else {
setProducts(results);
}
} catch (error) {
console.error(t('deleteProduct.error.fetch'), error);
setProducts([]);
setMessage(t('deleteProduct.messages.error'));
}
} else {
setProducts([]);
setMessage('');
}
};
/**
* Select product and open first confirmation dialog
*/
const handleProductClick = (product: { id: number; name: string }) => {
setSelectedProduct(product);
setConfirmation('first');
setMessage('');
};
/**
* Delete product after two-step confirmation
* Remove from products list on success
*/
const handleDelete = async () => {
if (!selectedProduct) return;
try {
await ProductService.deleteProduct(selectedProduct.id);
setMessage(t('deleteProduct.messages.success'));
setProducts((prev) => prev.filter((p) => p.id !== selectedProduct.id));
setSelectedProduct(null);
setConfirmation(null);
} catch (error) {
console.error(t('deleteProduct.error.delete'), error);
setMessage(t('deleteProduct.messages.failed'));
}
};
/**
* Cancel deletion and reset confirmation state
*/
const cancelOperation = () => {
setConfirmation(null);
setMessage(t('deleteProduct.messages.canceled'));
};
return (
<div className="flex flex-col min-h-screen bg-gray-50">
{/* Page header with logout functionality */}
<Header isLoggedIn={true} onLogout={() => navigate('/login')} />
{/* Help button positioned at the top center of the page */}
<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>
{/* Help modal for guidance */}
<HelpModal isOpen={isHelpOpen} onClose={() => setIsHelpOpen(false)} pageKey="deleteProduct" />
{/* Main content section */}
<main className="flex flex-col items-center justify-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('deleteProduct.findProduct')}</h2>
{/* Search input for finding products */}
<input
type="text"
placeholder={t('deleteProduct.searchPlaceholder')}
value={searchQuery}
onChange={handleSearch}
className="input-field"
/>
{/* Display message if applicable */}
{message && <p className="text-gray-500 text-center mt-4">{message}</p>}
{/* List of products matching the search query */}
<ul className="w-full mt-4 space-y-2">
{products.map((product) => (
<li
key={product.id}
className={`p-2 border rounded hover:bg-gray-200 cursor-pointer ${
selectedProduct?.id === product.id ? 'selected-product' : 'bg-gray-100'
}`}
onClick={() => handleProductClick(product)}
>
{product.name}
</li>
))}
</ul>
{confirmation === 'first' && selectedProduct && (
<div className="mt-6 p-4 bg-gray-100 border rounded shadow-md">
<p>{t('deleteProduct.confirmation.first')}</p>
<div className="mt-4 flex justify-between">
<button
className="button-confirmation button-confirmation-yes"
onClick={() => setConfirmation('second')}
>
{t('deleteProduct.confirmYes')}
</button>
<button
className="button-confirmation button-confirmation-no"
onClick={cancelOperation}
>
{t('deleteProduct.confirmNo')}
</button>
</div>
</div>
)}
{confirmation === 'second' && (
<div className="mt-6 p-4 bg-gray-100 border rounded shadow-md">
<p>{t('deleteProduct.confirmation.second')}</p>
<div className="mt-4 flex justify-between">
<button
className="button-confirmation button-confirmation-yes"
onClick={handleDelete}
>
{t('deleteProduct.confirmYes')}
</button>
<button
className="button-confirmation button-confirmation-no"
onClick={cancelOperation}
>
{t('deleteProduct.confirmNo')}
</button>
</div>
</div>
)}
</main>
<Footer />
</div>
);
};
export default DeleteProductPage;
|