feat(i18n): add bilingual support (Català / Castellano)
Run Tests on Branches / Detect Changes (push) Successful in 11s
Run Tests on Branches / Backend Tests (push) Has been skipped
Run Tests on Branches / Frontend Tests (push) Successful in 1m44s
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Has been skipped
Run Tests on Branches / Detect Changes (push) Successful in 11s
Run Tests on Branches / Backend Tests (push) Has been skipped
Run Tests on Branches / Frontend Tests (push) Successful in 1m44s
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Has been skipped
Add lightweight i18n system using React Context + useTranslation hook.
No external dependencies. Language persisted in localStorage('ff-lang').
New files:
- src/i18n/locales/ca.js (~410 translation keys)
- src/i18n/locales/es.js (~410 translation keys)
- src/i18n/LanguageContext.jsx (Provider + t() function)
- src/i18n/useTranslation.js (hook)
- src/i18n/index.js (barrel export)
Modified 22 files:
- main.jsx: wrap App with LanguageProvider
- App.test.jsx: add LanguageProvider wrapper for tests
- 13 user components: BottomNav, HomeView, LoginModal, SearchView,
ProfileView, AlertsView, ScannerView, PharmacyList, MedicineResults,
ProductResults, SavedNotifications, ErrorBoundary, SearchBar
- 6 admin components: AdminView, LoginForm, PharmacyManagement,
MedicineManagement, PharmacyMedicineLink, PharmacyProductLink
ProfileView includes language selector (globe icon + modal CA/ES)
placed next to the Theme selector.
No routes changed. No API calls affected. No navigation changes.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import React, { useState, useEffect, useMemo, useRef } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import './AdminComponents.css';
|
||||
|
||||
const MAX_PHARMACY_RESULTS = 25;
|
||||
@@ -8,6 +9,7 @@ function normalize(s) {
|
||||
}
|
||||
|
||||
function PharmacyProductLink() {
|
||||
const { t } = useTranslation();
|
||||
const [pharmacies, setPharmacies] = useState([]);
|
||||
const [productSearch, setProductSearch] = useState('');
|
||||
const [productResults, setProductResults] = useState([]);
|
||||
@@ -130,7 +132,7 @@ function PharmacyProductLink() {
|
||||
e.preventDefault();
|
||||
|
||||
if (!selectedProduct) {
|
||||
alert('Por favor, selecciona un producto primero');
|
||||
alert(t('admin.linkProduct.selectProductFirst'));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,16 +158,16 @@ function PharmacyProductLink() {
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('Error al vincular producto a farmacia');
|
||||
if (!response.ok) throw new Error(t('admin.linkProduct.linkError'));
|
||||
|
||||
resetForm();
|
||||
if (selectedPharmacy) {
|
||||
fetchPharmacyProducts(selectedPharmacy.id);
|
||||
}
|
||||
alert('¡Producto vinculado a la farmacia correctamente!');
|
||||
alert(t('admin.linkProduct.linkSuccess'));
|
||||
} catch (error) {
|
||||
console.error('Error linking product:', error);
|
||||
alert('Error al vincular producto a farmacia');
|
||||
alert(t('admin.linkProduct.linkError'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,18 +180,18 @@ function PharmacyProductLink() {
|
||||
body: JSON.stringify({ price, stock })
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('Error al actualizar');
|
||||
if (!response.ok) throw new Error(t('admin.linkProduct.updateError'));
|
||||
|
||||
fetchPharmacyProducts(selectedPharmacy.id);
|
||||
alert('¡Actualizado correctamente!');
|
||||
alert(t('admin.linkProduct.updateSuccess'));
|
||||
} catch (error) {
|
||||
console.error('Error updating:', error);
|
||||
alert('Error al actualizar');
|
||||
alert(t('admin.linkProduct.updateError'));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id) => {
|
||||
if (!confirm('¿Eliminar este producto de la farmacia?')) return;
|
||||
if (!confirm(t('admin.linkProduct.deleteConfirm'))) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/admin/pharmacy-products/${id}`, {
|
||||
@@ -197,13 +199,13 @@ function PharmacyProductLink() {
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('Error al eliminar');
|
||||
if (!response.ok) throw new Error(t('admin.linkProduct.deleteError'));
|
||||
|
||||
fetchPharmacyProducts(selectedPharmacy.id);
|
||||
alert('¡Producto eliminado de la farmacia!');
|
||||
alert(t('admin.linkProduct.deleteSuccess'));
|
||||
} catch (error) {
|
||||
console.error('Error deleting:', error);
|
||||
alert('Error al eliminar producto');
|
||||
alert(t('admin.linkProduct.deleteError'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -266,11 +268,11 @@ function PharmacyProductLink() {
|
||||
|
||||
return (
|
||||
<div className="admin-section">
|
||||
<h2>Vincular Producto a Farmacia</h2>
|
||||
<h2>{t('admin.linkProduct.title')}</h2>
|
||||
|
||||
<form className="admin-form" onSubmit={handleSubmit}>
|
||||
<div className="form-group">
|
||||
<label>Farmacia *</label>
|
||||
<label>{t('admin.linkProduct.pharmacy')}</label>
|
||||
<input
|
||||
ref={pharmacyInputRef}
|
||||
type="text"
|
||||
@@ -285,7 +287,7 @@ function PharmacyProductLink() {
|
||||
}}
|
||||
onFocus={() => setPharmacyDropdownOpen(true)}
|
||||
onBlur={() => setTimeout(() => setPharmacyDropdownOpen(false), 150)}
|
||||
placeholder={pharmacies.length ? `Buscar entre ${pharmacies.length} farmacias por nombre o dirección…` : 'Cargando farmacias…'}
|
||||
placeholder={pharmacies.length ? `${t('admin.linkProduct.searchPharmacy')} ${pharmacies.length} ${t('admin.linkProduct.pharmacies')}` : t('admin.linkProduct.loadingPharmacies')}
|
||||
autoComplete="off"
|
||||
required={!selectedPharmacy}
|
||||
/>
|
||||
@@ -293,7 +295,7 @@ function PharmacyProductLink() {
|
||||
<div className="medicine-search-results">
|
||||
{filteredPharmacies.length === 0 ? (
|
||||
<div className="search-result-item search-result-item--empty">
|
||||
<span>No hay farmacias que coincidan con "{pharmacyQuery}"</span>
|
||||
<span>{t('admin.linkProduct.noPharmacies')} "{pharmacyQuery}"</span>
|
||||
</div>
|
||||
) : (
|
||||
filteredPharmacies.map((pharmacy) => (
|
||||
@@ -314,14 +316,14 @@ function PharmacyProductLink() {
|
||||
<p>✅ Selected: <strong>{selectedPharmacy.name}</strong></p>
|
||||
<p className="medicine-details">{selectedPharmacy.address}</p>
|
||||
<button type="button" className="btn-small" onClick={clearPharmacy}>
|
||||
Cambiar farmacia
|
||||
{t('admin.linkProduct.changePharmacy')}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>Buscar Producto (CIMA / Parafarmacia) *</label>
|
||||
<label>{t('admin.linkProduct.product')}</label>
|
||||
<input
|
||||
type="text"
|
||||
value={productSearch}
|
||||
@@ -329,10 +331,10 @@ function PharmacyProductLink() {
|
||||
setProductSearch(e.target.value);
|
||||
setSelectedProduct(null);
|
||||
}}
|
||||
placeholder="Escribe para buscar medicamentos o productos de parafarmacia..."
|
||||
placeholder={t('admin.linkProduct.searchProduct')}
|
||||
required
|
||||
/>
|
||||
{searching && <p className="loading-text">Buscando...</p>}
|
||||
{searching && <p className="loading-text">{t('admin.linkProduct.searching')}</p>}
|
||||
|
||||
{productResults.length > 0 && !selectedProduct && (
|
||||
<div className="medicine-search-results">
|
||||
@@ -356,9 +358,9 @@ function PharmacyProductLink() {
|
||||
<div className="selected-medicine-info">
|
||||
<p>✅ Selected: <strong>{selectedProduct.product_name || selectedProduct.name}</strong></p>
|
||||
<p className="medicine-details">
|
||||
{selectedProduct.brand && `Marca: ${selectedProduct.brand} • `}
|
||||
{selectedProduct.brands && `Marca: ${selectedProduct.brands} • `}
|
||||
{selectedProduct.price != null && `Precio: ${selectedProduct.price}€ • `}
|
||||
{selectedProduct.brand && `${t('admin.linkProduct.brand')} ${selectedProduct.brand} • `}
|
||||
{selectedProduct.brands && `${t('admin.linkProduct.brand')} ${selectedProduct.brands} • `}
|
||||
{selectedProduct.price != null && `${t('admin.linkProduct.price')} ${selectedProduct.price}€ • `}
|
||||
{getSourceBadge(selectedProduct.source || 'parapharmacy')}
|
||||
{' '}
|
||||
{selectedProduct._id || selectedProduct.id || selectedProduct.nregistro}
|
||||
@@ -371,7 +373,7 @@ function PharmacyProductLink() {
|
||||
setProductSearch('');
|
||||
}}
|
||||
>
|
||||
Cambiar producto
|
||||
{t('admin.linkProduct.changeProduct')}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
@@ -379,7 +381,7 @@ function PharmacyProductLink() {
|
||||
|
||||
<div className="form-row">
|
||||
<div className="form-group">
|
||||
<label>Precio (€)</label>
|
||||
<label>{t('admin.linkProduct.priceInput')}</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
@@ -390,7 +392,7 @@ function PharmacyProductLink() {
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>Stock</label>
|
||||
<label>{t('admin.linkProduct.stock')}</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.stock}
|
||||
@@ -402,21 +404,21 @@ function PharmacyProductLink() {
|
||||
|
||||
<div className="form-actions">
|
||||
<button type="submit" className="btn-primary">
|
||||
Vincular Producto
|
||||
{t('admin.linkProduct.linkBtn')}
|
||||
</button>
|
||||
<button type="button" className="btn-secondary" onClick={resetForm}>
|
||||
Reiniciar
|
||||
{t('admin.linkProduct.reset')}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{selectedPharmacy && (
|
||||
<div className="pharmacy-medicines-section">
|
||||
<h3>Productos en {selectedPharmacy.name}</h3>
|
||||
<h3>{t('admin.linkProduct.productsIn')} {selectedPharmacy.name}</h3>
|
||||
{loading ? (
|
||||
<div className="loading">Cargando...</div>
|
||||
<div className="loading">{t('admin.linkProduct.loading')}</div>
|
||||
) : pharmacyProducts.length === 0 ? (
|
||||
<p className="empty-state">Aún no hay productos vinculados a esta farmacia.</p>
|
||||
<p className="empty-state">{t('admin.linkProduct.empty')}</p>
|
||||
) : (
|
||||
<div className="admin-list">
|
||||
{pharmacyProducts.map((pp) => (
|
||||
@@ -427,7 +429,7 @@ function PharmacyProductLink() {
|
||||
{getSourceBadge(pp.product_source)}
|
||||
</h4>
|
||||
<p>
|
||||
<strong>Precio:</strong> {pp.price ? `€${parseFloat(pp.price).toFixed(2)}` : 'No definido'} •
|
||||
<strong>{t('admin.linkProduct.priceLabel')}</strong> {pp.price ? `€${parseFloat(pp.price).toFixed(2)}` : t('admin.linkProduct.undefined')} •
|
||||
<strong> Stock:</strong> {pp.stock || 0}
|
||||
</p>
|
||||
</div>
|
||||
@@ -435,17 +437,17 @@ function PharmacyProductLink() {
|
||||
<button
|
||||
className="btn-edit"
|
||||
onClick={() => {
|
||||
const newPrice = prompt('Introduce nuevo precio:', pp.price || '');
|
||||
const newStock = prompt('Introduce nuevo stock:', pp.stock || '0');
|
||||
const newPrice = prompt(t('admin.linkProduct.newPrice'), pp.price || '');
|
||||
const newStock = prompt(t('admin.linkProduct.newStock'), pp.stock || '0');
|
||||
if (newPrice !== null && newStock !== null) {
|
||||
handleUpdate(pp.id, newPrice ? parseFloat(newPrice) : null, parseInt(newStock) || 0);
|
||||
}
|
||||
}}
|
||||
>
|
||||
Actualizar
|
||||
{t('admin.linkProduct.update')}
|
||||
</button>
|
||||
<button className="btn-delete" onClick={() => handleDelete(pp.id)}>
|
||||
Eliminar
|
||||
{t('admin.linkProduct.delete')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user