Files
FarmaFinder/apps/frontend/src/components/ProductResults.jsx
T
Antoni Nuñez Romeu 63b25e3f4f fix: use _id for MongoDB products in frontend
- SearchView now uses p._id || p.id for product navigation
- ProductResults uses product._id || product.id for React key
- Fixes 'undefined' error when clicking parapharmacy products
2026-07-16 16:18:22 +02:00

126 lines
3.5 KiB
React

import React from 'react';
import './ProductResults.css';
const categoryLabels = {
otc: 'Sin Receta',
parapharmacy: 'Parafarmacia',
dermocosmética: 'Dermocosmética',
'Fórmulas lácteas': 'Fórmulas lácteas',
vitaminas: 'Vitaminas',
analgésicos: 'Analgésicos'
};
const sourceColors = {
cima: '#2563eb',
promofarma: '#16a34a',
docmorris: '#e11d48',
primor: '#7c3aed',
parapharmacy: '#16a34a'
};
const sourceLabels = {
cima: 'CIMA',
promofarma: 'Promofarma',
docmorris: 'DocMorris',
primor: 'Primor',
parapharmacy: 'Parafarmacia'
};
function ProductResults({ products, onSelect }) {
if (!products || products.length === 0) {
return (
<div className="no-results">
<p>No se encontraron productos</p>
</div>
);
}
return (
<div className="product-results">
{products.map((product) => (
<ProductCard
key={product._id || product.id}
product={product}
onSelect={onSelect}
/>
))}
</div>
);
}
function ProductCard({ product, onSelect }) {
const nutriScore = product.nutriscore;
const nutriScoreColors = {
a: '#16a34a',
b: '#65a30d',
c: '#eab308',
d: '#f97316',
e: '#dc2626'
};
return (
<div className="product-card" onClick={() => onSelect(product)}>
<div className="product-card-image">
{product.image_url ? (
<img src={product.image_url} alt={product.name} loading="lazy" />
) : (
<div className="product-image-placeholder">
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
<circle cx="8.5" cy="8.5" r="1.5" />
<polyline points="21 15 16 10 5 21" />
</svg>
</div>
)}
</div>
<div className="product-card-content">
<div className="product-card-badges">
<span
className="source-badge"
style={{ backgroundColor: sourceColors[product.source] }}
>
{sourceLabels[product.source]}
</span>
<span className="category-badge">
{categoryLabels[product.category] || product.category}
</span>
{product.source === 'openfoodfacts' && nutriScore && (
<span
className="nutri-score-badge"
style={{ backgroundColor: nutriScoreColors[nutriScore.toLowerCase()] || '#9ca3af' }}
>
Nutri-Score {nutriScore.toUpperCase()}
</span>
)}
</div>
<div className="product-card-header">
<h3>{product.name}</h3>
</div>
<div className="product-card-body">
{product.brand && (
<p><strong>Marca:</strong> {product.brand}</p>
)}
{product.price != null && (
<p className="product-price"><strong>Precio:</strong> {product.price} €</p>
)}
{product.source === 'cima' && product.active_ingredient && (
<p><strong>Principio Activo:</strong> {product.active_ingredient}</p>
)}
{product.source === 'cima' && product.dosage && (
<p><strong>Dosis:</strong> {product.dosage}</p>
)}
</div>
<div className="product-card-footer">
<span className="view-details">Ver detalles →</span>
</div>
</div>
</div>
);
}
export default ProductResults;