feat(F-178): completed feature
This commit is contained in:
@@ -117,7 +117,17 @@ export default function RegisterPage() {
|
||||
setSearching(true);
|
||||
setSearchError('');
|
||||
try {
|
||||
const res = await posApi.searchProducts(q, config?.session?.storeId) as { items: SearchResult[] };
|
||||
const normalized = q.trim();
|
||||
if (/^\d{8,14}$/.test(normalized)) {
|
||||
try {
|
||||
const exact = await posApi.productByEan(normalized) as SearchResult;
|
||||
setSearchResults([exact]);
|
||||
return;
|
||||
} catch (err) {
|
||||
if ((err as { status?: number }).status !== 404) throw err;
|
||||
}
|
||||
}
|
||||
const res = await posApi.searchProducts(normalized, config?.session?.storeId) as { items: SearchResult[] };
|
||||
setSearchResults(res.items ?? []);
|
||||
} catch (err) {
|
||||
setSearchResults([]);
|
||||
@@ -145,6 +155,26 @@ export default function RegisterPage() {
|
||||
setSearchResults([]);
|
||||
};
|
||||
|
||||
const addSearchResultWithEnter = async () => {
|
||||
const normalized = search.trim();
|
||||
if (!normalized) return;
|
||||
setSearchError('');
|
||||
try {
|
||||
if (/^\d{8,14}$/.test(normalized)) {
|
||||
const exact = await posApi.productByEan(normalized) as SearchResult;
|
||||
addToCart(exact);
|
||||
return;
|
||||
}
|
||||
if (searchResults.length === 1 && searchResults[0]) addToCart(searchResults[0]);
|
||||
} catch (err) {
|
||||
if ((err as { status?: number }).status === 404) {
|
||||
setSearchError(`No existe ningún producto con EAN ${normalized}`);
|
||||
} else {
|
||||
setSearchError(err instanceof Error ? err.message : 'No se pudo buscar el producto');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const removeFromCart = (variantId: string) => {
|
||||
setCart(cart.filter(i => i.variantId !== variantId));
|
||||
};
|
||||
@@ -300,6 +330,12 @@ export default function RegisterPage() {
|
||||
type="text"
|
||||
value={search}
|
||||
onChange={e => setSearch(e.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
void addSearchResultWithEnter();
|
||||
}
|
||||
}}
|
||||
placeholder="Buscar producto o escanear EAN…"
|
||||
className="w-full px-4 py-3 border-2 border-gray-200 rounded-xl text-lg focus:border-[#2D6A4F] outline-none"
|
||||
autoFocus
|
||||
|
||||
Reference in New Issue
Block a user