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 | 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 2x 2x | /**
* @file AddProductPage.tsx
* @description
* Admin interface for adding new products to the inventory.
*
* **Features:**
* - Input fields for product name, quantity, and price
* - Two-step confirmation (preview + confirmation)
* - API integration for product creation
* - Success/error messaging
* - Help modal with guidance
*
* **Workflow:**
* 1. Fill in product name, quantity, and price
* 2. Click "Add" to confirm details
* 3. Submit confirmation dialog
* 4. API saves product or shows error
*
* @component
*/
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import ProductService from '../api/ProductService';
import { useTranslation } from 'react-i18next';
import Header from '../components/Header';
import HelpModal from '../components/HelpModal';
import Footer from '../components/Footer';
import '../styles/addProduct.css';
import '../styles/tailwindCustom.css';
/**
* Add product page component
* @component
* @returns {JSX.Element} Product creation form
*/
const AddProductPage: React.FC = () => {
const { t, i18n } = useTranslation(['translation', 'help']);
const [name, setName] = useState('');
const [quantity, setQuantity] = useState('');
const [price, setPrice] = useState('');
const [confirmation, setConfirmation] = useState(false);
const [message, setMessage] = useState('');
const [isHelpOpen, setIsHelpOpen] = useState(false);
const navigate = useNavigate();
/**
* Reset form fields to initial state
*/
const resetFields = () => {
setName('');
setQuantity('');
setPrice('');
};
/**
* Submit new product to API with type conversion
* Convert string inputs to appropriate numeric types
*/
const handleAddProduct = async () => {
setMessage('');
try {
await ProductService.addProduct({
name,
quantity: parseInt(quantity, 10),
price: parseFloat(price),
});
setMessage(t('addProduct.successMessage'));
resetFields();
} catch (error) {
setMessage(t('addProduct.errorMessage'));
console.error(t('addProduct.errorLog'), error);
}
setConfirmation(false);
};
/**
* Cancel operation and reset form state
*/
const handleCancel = () => {
resetFields();
setMessage(t('addProduct.cancelMessage'));
setConfirmation(false);
};
return (
<div className="flex flex-col min-h-screen bg-gray-100">
<Header isLoggedIn={true} onLogout={() => navigate('/login')} />
<div className="absolute top-4 left-1/2 transform -translate-x-1/2">
<button
onClick={() => setIsHelpOpen(true)}
className="button-secondary"
key={i18n.language}
>
{t('button', { ns: 'help' })}
</button>
</div>
<HelpModal isOpen={isHelpOpen} onClose={() => setIsHelpOpen(false)} pageKey="addProduct" />
<main className="flex flex-col items-center justify-center flex-grow">
<div className="w-96 box-shadow mt-6">
<h2 className="text-lg font-semibold mb-4">{t('addProduct.detailsTitle')}</h2>
<div className="mb-4">
<label htmlFor="name" className="block font-medium mb-2">
{t('addProduct.nameLabel')}
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="input-field"
/>
</div>
<div className="mb-4">
<label htmlFor="quantity" className="block font-medium mb-2">
{t('addProduct.quantityLabel')}
</label>
<input
type="number"
id="quantity"
value={quantity}
onChange={(e) => setQuantity(e.target.value)}
className="input-field"
/>
</div>
<div className="mb-4">
<label htmlFor="price" className="block font-medium mb-2">
{t('addProduct.priceLabel')}
</label>
<input
type="number"
id="price"
value={price}
onChange={(e) => setPrice(e.target.value)}
className="input-field"
/>
</div>
<div className="flex justify-between mt-6">
<button className="button-confirmation-no" onClick={handleCancel}>
{t('addProduct.cancelButton')}
</button>
<button
className="button-confirmation-yes"
onClick={() => setConfirmation(true)}
>
{t('addProduct.addButton')}
</button>
</div>
{confirmation && (
<div className="confirmation-box mt-4">
<p>{t('addProduct.confirmationMessage')}</p>
<div className="flex justify-between mt-2">
<button
className="button-confirmation-yes"
onClick={handleAddProduct}
>
{t('addProduct.confirmYes')}
</button>
<button className="button-confirmation-no" onClick={handleCancel}>
{t('addProduct.confirmNo')}
</button>
</div>
</div>
)}
{message && (
<p className="mt-4 text-center text-blue-500 font-semibold">{message}</p>
)}
</div>
</main>
<Footer />
</div>
);
};
export default AddProductPage;
|