Improve ProductSelector response handling and UI
Enhanced the ProductSelector to robustly handle different response shapes from the products API, preventing runtime errors. Also adjusted dropdown and scroll area sizing for better UI consistency.
This commit is contained in:
@@ -50,7 +50,15 @@ export default function ProductSelector({
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await fetchClient('/promotions/products/all');
|
||||
setProducts(response);
|
||||
// Ensure we only set an array to state to avoid runtime errors
|
||||
if (Array.isArray(response)) {
|
||||
setProducts(response);
|
||||
} else if (response && Array.isArray((response as any).products)) {
|
||||
setProducts((response as any).products);
|
||||
} else {
|
||||
console.error('Unexpected products response shape:', response);
|
||||
setProducts([]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching products:', error);
|
||||
} finally {
|
||||
@@ -119,8 +127,8 @@ export default function ProductSelector({
|
||||
{/* Dropdown using absolute positioning within relative container */}
|
||||
{open && (
|
||||
<div
|
||||
className="absolute top-full left-0 right-0 z-50 mt-1 bg-background border border-border rounded-md shadow-lg max-h-64 overflow-hidden"
|
||||
style={{ minWidth: '300px' }}
|
||||
className="absolute top-full left-0 right-0 z-50 mt-1 bg-background border border-border rounded-md shadow-lg"
|
||||
style={{ minWidth: '300px', maxHeight: '400px' }}
|
||||
>
|
||||
{/* Search Header */}
|
||||
<div className="flex items-center gap-2 p-3 border-b bg-muted/30">
|
||||
@@ -135,7 +143,7 @@ export default function ProductSelector({
|
||||
</div>
|
||||
|
||||
{/* Products List */}
|
||||
<ScrollArea className="max-h-48">
|
||||
<ScrollArea className="h-[300px]">
|
||||
{loading ? (
|
||||
<div className="p-4 text-center text-sm text-muted-foreground">
|
||||
Loading products...
|
||||
|
||||
Reference in New Issue
Block a user