Enhance SearchBar: remove loading state, add product details in suggestions
- Remove CircularProgress component and loadingSuggestions state - Remove maxHeight limit from suggestions dropdown (already limiting to 8 results) - Display price, VAT, and delivery days in suggestion list - Use existing i18n translation keys (product:inclVat, delivery:times.*)
This commit is contained in:
@@ -7,16 +7,17 @@ import List from "@mui/material/List";
|
|||||||
import ListItem from "@mui/material/ListItem";
|
import ListItem from "@mui/material/ListItem";
|
||||||
import ListItemText from "@mui/material/ListItemText";
|
import ListItemText from "@mui/material/ListItemText";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
import CircularProgress from "@mui/material/CircularProgress";
|
|
||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
import SearchIcon from "@mui/icons-material/Search";
|
import SearchIcon from "@mui/icons-material/Search";
|
||||||
import KeyboardReturnIcon from "@mui/icons-material/KeyboardReturn";
|
import KeyboardReturnIcon from "@mui/icons-material/KeyboardReturn";
|
||||||
import { useNavigate, useLocation } from "react-router-dom";
|
import { useNavigate, useLocation } from "react-router-dom";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
const SearchBar = () => {
|
const SearchBar = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const searchParams = new URLSearchParams(location.search);
|
const searchParams = new URLSearchParams(location.search);
|
||||||
|
const { t } = useTranslation(['product', 'delivery']);
|
||||||
|
|
||||||
// State management
|
// State management
|
||||||
const [searchQuery, setSearchQuery] = React.useState(
|
const [searchQuery, setSearchQuery] = React.useState(
|
||||||
@@ -25,7 +26,6 @@ const SearchBar = () => {
|
|||||||
const [suggestions, setSuggestions] = React.useState([]);
|
const [suggestions, setSuggestions] = React.useState([]);
|
||||||
const [showSuggestions, setShowSuggestions] = React.useState(false);
|
const [showSuggestions, setShowSuggestions] = React.useState(false);
|
||||||
const [selectedIndex, setSelectedIndex] = React.useState(-1);
|
const [selectedIndex, setSelectedIndex] = React.useState(-1);
|
||||||
const [loadingSuggestions, setLoadingSuggestions] = React.useState(false);
|
|
||||||
|
|
||||||
// Refs for debouncing and timers
|
// Refs for debouncing and timers
|
||||||
const debounceTimerRef = React.useRef(null);
|
const debounceTimerRef = React.useRef(null);
|
||||||
@@ -61,12 +61,9 @@ const SearchBar = () => {
|
|||||||
if (!query || query.length < 2) {
|
if (!query || query.length < 2) {
|
||||||
setSuggestions([]);
|
setSuggestions([]);
|
||||||
setShowSuggestions(false);
|
setShowSuggestions(false);
|
||||||
setLoadingSuggestions(false);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoadingSuggestions(true);
|
|
||||||
|
|
||||||
window.socketManager.emit(
|
window.socketManager.emit(
|
||||||
"getSearchProducts",
|
"getSearchProducts",
|
||||||
{
|
{
|
||||||
@@ -74,8 +71,6 @@ const SearchBar = () => {
|
|||||||
limit: 8,
|
limit: 8,
|
||||||
},
|
},
|
||||||
(response) => {
|
(response) => {
|
||||||
setLoadingSuggestions(false);
|
|
||||||
|
|
||||||
if (response && response.products) {
|
if (response && response.products) {
|
||||||
// getSearchProducts returns response.products array
|
// getSearchProducts returns response.products array
|
||||||
const suggestions = response.products.slice(0, 8); // Limit to 8 suggestions
|
const suggestions = response.products.slice(0, 8); // Limit to 8 suggestions
|
||||||
@@ -184,6 +179,15 @@ const SearchBar = () => {
|
|||||||
}, 200);
|
}, 200);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get delivery days based on availability
|
||||||
|
const getDeliveryDays = (product) => {
|
||||||
|
if (product.available === 1) {
|
||||||
|
return t('delivery:times.standard2to3Days');
|
||||||
|
} else if (product.incoming === 1 || product.availableSupplier === 1) {
|
||||||
|
return t('delivery:times.supplier7to9Days');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Handle enter icon click
|
// Handle enter icon click
|
||||||
const handleEnterClick = () => {
|
const handleEnterClick = () => {
|
||||||
delete window.currentSearchQuery;
|
delete window.currentSearchQuery;
|
||||||
@@ -255,13 +259,11 @@ const SearchBar = () => {
|
|||||||
),
|
),
|
||||||
endAdornment: (
|
endAdornment: (
|
||||||
<InputAdornment position="end">
|
<InputAdornment position="end">
|
||||||
{loadingSuggestions && <CircularProgress size={16} />}
|
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={handleEnterClick}
|
onClick={handleEnterClick}
|
||||||
aria-label="Suche starten"
|
aria-label="Suche starten"
|
||||||
sx={{
|
sx={{
|
||||||
ml: loadingSuggestions ? 0.5 : 0,
|
|
||||||
p: 0.5,
|
p: 0.5,
|
||||||
color: "text.secondary",
|
color: "text.secondary",
|
||||||
"&:hover": {
|
"&:hover": {
|
||||||
@@ -288,8 +290,6 @@ const SearchBar = () => {
|
|||||||
left: 0,
|
left: 0,
|
||||||
right: 0,
|
right: 0,
|
||||||
zIndex: 1300,
|
zIndex: 1300,
|
||||||
maxHeight: "300px",
|
|
||||||
overflow: "auto",
|
|
||||||
mt: 0.5,
|
mt: 0.5,
|
||||||
borderRadius: 2,
|
borderRadius: 2,
|
||||||
}}
|
}}
|
||||||
@@ -321,6 +321,11 @@ const SearchBar = () => {
|
|||||||
{suggestion.name}
|
{suggestion.name}
|
||||||
</Typography>
|
</Typography>
|
||||||
}
|
}
|
||||||
|
secondary={
|
||||||
|
<Typography variant="caption" color="text.secondary">
|
||||||
|
{suggestion.price?.toFixed(2)}€ ({t('product:inclVat', { vat: suggestion.vat })}) · {getDeliveryDays(suggestion)}
|
||||||
|
</Typography>
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</ListItem>
|
</ListItem>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user