Skip to content

Commit abe0dc8

Browse files
committed
add search in products search
1 parent 2caf72c commit abe0dc8

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

frontend/src/App.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function App() {
2020
const [sortOption, setSortOption] = useState('recommended');
2121
const [activeNav, setActiveNav] = useState('MEN');
2222
const [sneakersView, setSneakersView] = useState(false);
23+
const [searchQuery, setSearchQuery] = useState('');
2324
const [loggedInUser, setLoggedInUser] = useState(() => {
2425
try { return JSON.parse(localStorage.getItem('zappify_user')) || null; } catch { return null; }
2526
});
@@ -92,15 +93,19 @@ function App() {
9293
const sneakerMatch = sneakersView ? product.id >= 30 && product.id <= 44 : true;
9394
const categoryMatch = selectedCategories.length === 0 || selectedCategories.includes(product.category);
9495
const themeMatch = selectedThemes.length === 0 || selectedThemes.includes(product.theme);
95-
return sneakerMatch && categoryMatch && themeMatch;
96+
const searchMatch = searchQuery.trim() === '' ||
97+
product.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
98+
product.brand.toLowerCase().includes(searchQuery.toLowerCase()) ||
99+
product.category.toLowerCase().includes(searchQuery.toLowerCase());
100+
return sneakerMatch && categoryMatch && themeMatch && searchMatch;
96101
});
97102

98103
if (sortOption === 'low-high') result = [...result].sort((a, b) => a.price - b.price);
99104
else if (sortOption === 'high-low') result = [...result].sort((a, b) => b.price - a.price);
100105
else if (sortOption === 'newest') result = [...result].sort((a, b) => b.id - a.id);
101106

102107
return result;
103-
}, [selectedCategories, selectedThemes, sortOption, sneakersView]);
108+
}, [selectedCategories, selectedThemes, sortOption, sneakersView, searchQuery]);
104109

105110
return (
106111
<div className="zappify-app">
@@ -113,6 +118,8 @@ function App() {
113118
loggedInUser={loggedInUser}
114119
onLogout={handleLogout}
115120
onOpenAccount={() => setShowAccount(true)}
121+
searchQuery={searchQuery}
122+
onSearch={setSearchQuery}
116123
/>
117124

118125
<main className="app-main">

frontend/src/components/Header.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from 'react';
22
import { Search, ShoppingCart, Heart, User, Menu, X } from 'lucide-react';
33

4-
const Header = ({ onOpenOverlay, onNavigate, cartCount, wishlistCount, activeNav, loggedInUser, onLogout, onOpenAccount }) => {
4+
const Header = ({ onOpenOverlay, onNavigate, cartCount, wishlistCount, activeNav, loggedInUser, onLogout, onOpenAccount, searchQuery, onSearch }) => {
55
const [isSearchFocused, setIsSearchFocused] = useState(false);
66
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
77
const [showUserMenu, setShowUserMenu] = useState(false);
@@ -46,9 +46,14 @@ const Header = ({ onOpenOverlay, onNavigate, cartCount, wishlistCount, activeNav
4646
<input
4747
type="text"
4848
placeholder="What are you looking for?"
49+
value={searchQuery}
50+
onChange={(e) => onSearch(e.target.value)}
4951
onFocus={() => setIsSearchFocused(true)}
5052
onBlur={() => setIsSearchFocused(false)}
5153
/>
54+
{searchQuery && (
55+
<button className="search-clear" onClick={() => onSearch('')}></button>
56+
)}
5257
</div>
5358

5459
<div className="user-actions">

frontend/src/index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,3 +1188,7 @@ ul {
11881188
.cancel-popup hr { border: none; border-top: 1px solid #eee; margin-bottom: 12px; }
11891189
.cancel-popup-btn { float: right; background: none; border: 1.5px solid #e85d04; color: #e85d04; font-weight: 700; font-size: 14px; padding: 8px 20px; border-radius: 8px; cursor: pointer; }
11901190
.order-status.cancelled { background: #fef2f2; color: #dc2626; }
1191+
1192+
/* Search clear button */
1193+
.search-clear { background: none; border: none; cursor: pointer; color: #aaa; font-size: 14px; padding: 0 4px; line-height: 1; }
1194+
.search-clear:hover { color: #e85d04; }

0 commit comments

Comments
 (0)