Skip to content

Commit

Permalink
user search product UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MANISHIMWESalton committed Jul 11, 2024
1 parent d192f48 commit cbd6519
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 76 deletions.
58 changes: 41 additions & 17 deletions src/components/inputs/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useEffect, useState } from "react";
import { FiSearch } from "react-icons/fi";
import "../../styles/SearchInput.scss";
import { useAppDispatch, useAppSelector } from "../../store/store";
import { useNavigate } from "react-router-dom";
import { Link, useNavigate } from "react-router-dom";
import {
searchProduct,
selectSearchResults,
Expand All @@ -17,39 +17,63 @@ interface SearchInputProps {
function SearchInput({ className, placeholder }: SearchInputProps) {
const dispatch = useAppDispatch();
const searchResults = useAppSelector(selectSearchResults);
const searchError = useAppSelector(selectSearchError);
const [search, setSearch] = useState("");
const [isFocused, setIsFocused] = useState(false);
const [isHoved, setIsHoved] = useState(false);

const navigate = useNavigate();
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setSearch(value);
if (value.trim()) {
dispatch(searchProduct({ name: value.trim() }));
}
};
return (
<div>
<form className={`search-container ${className}`}>
<div className="main-search">
<form
className={`search-container ${className}`}
onSubmit={(e) => {
e.preventDefault();
if (search.trim()) {
navigate(`/search?name=${search.trim()}`);
}
}}
>
<div className="search-icon">
<FiSearch />
</div>
<input
type="text"
placeholder={placeholder}
onChange={(e) => {
setSearch(e.target.value);
dispatch(searchProduct({ name: e.target.value }));
}}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
onChange={handleSearchChange}
value={search}
/>
<button
className="search-button"
type="submit"
onClick={() => navigate(`/search?name=${search}`)}
>
Search
</button>
</form>
<div style={{ display: search ? "block" : "none" }}>
{searchResults.length > 0 ? (
searchResults.map((product: any) => (
<p key={product.id}>{product.name}</p>
))
) : (
<p>No product</p>
)}
</div>
{(isFocused || isHoved) && (
<div className="search-result" onMouseEnter={()=> setIsHoved(true)}>
{searchResults.length > 0 ? (
searchResults.map((product: any) => (
<div key={product.id} className="result">
<Link to={`/search?name=${product.name}`} className="link">
<p >{product.name}</p>
</Link>
</div>
)
)
) :null
}
</div>
)}
</div>
);
}
Expand Down
102 changes: 85 additions & 17 deletions src/pages/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/* eslint-disable */
import React, { useEffect, useState } from "react";
import { useAppDispatch, useAppSelector } from "../store/store";
import SkewLoader

from "react-spinners/ClipLoader";
import "../styles/search.scss";
import Header from "../components/layout/Header";
import Footer from "../components/layout/Footer";
import {
searchProduct,
selectSearchResults,
selectSearchError,
} from "../store/features/ProductSlice";
import { useLocation } from "react-router-dom";
import { FaChevronDown } from "react-icons/fa6";
import Product from "../components/product/Product";

const SearchBar: React.FC = () => {
const location = useLocation();
Expand All @@ -15,6 +23,13 @@ const SearchBar: React.FC = () => {
const category = params.get("category");
const maxPrice = params.get("maxPrice");
const minPrice = params.get("minPrice");
const [loading,setLoading] = useState(false)
useEffect(()=>{
setLoading(true)
setTimeout(()=>{
setLoading(false)
},1000)
},[])

const dispatch = useAppDispatch();
const searchResults = useAppSelector(selectSearchResults);
Expand All @@ -30,23 +45,76 @@ const SearchBar: React.FC = () => {
);
}, []);
return (
<div>
<h1>search</h1>
{searchError && <p>Error: {searchError}</p>}
<div>
{searchResults.length > 0 ? (
<ul>
{searchResults.map((product: any) => (
<li key={product.id}>
{product.name} : ${product.price}
</li>
))}
</ul>
) : (
<p>No products found.</p>
)}
</div>
</div>
<>
<Header />
{
loading ? <SkewLoader

color={"#FF6D18"} loading={loading}size={70}/>
:
<>
<div className="product-search">
<div className="main-content-product">
<div className="right-side-bar">
<div className="searchPrice-box">
<span className="product-name">{name}:</span>
<span className="searchSpan-price">Price:</span>
<div className="dropdown-container">
<select className="min-span">
<option value="">Min</option>
<FaChevronDown className="header__selected__iconIcon" />
<option value="10">$10</option>
<option value="20">$20</option>
</select>
<select className="max-span">
<option value="">Max</option>
<FaChevronDown className="header__selected__iconIcon" />
<option value="50">$50</option>
<option value="100">$100</option>
</select>
<span className="searchSpan-price">Discount:</span>
<select className="max-span">
<option value="">Discount</option>
<FaChevronDown className="header__selected__iconIcon" />
<option value="5">5%</option>
<option value="10">10%</option>
</select>
</div>
</div>
</div>
</div>
</div><div className="product-main">
<div className="landing-container">
{false ? (
<div className="loader"></div>
) : searchError ? (
<div className="error-message">
<p>{"Something went wrong. Please try again later."}</p>
</div>
) : (
<div>
<div className="product-list">
{searchResults &&
searchResults.map((product: any) => (
<Product
key={product.id}
images={product.images}
name={product.name}
price={`$${product.price}`}
stock={Number(product.quantity)}
description={product.description}
discount={Number(product.discount.replace("%", ""))} />
))}
</div>
</div>
)}
</div>
</div>
</>
}

<Footer />
</>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/styles/Colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ $black-2: #232f3e;
$black-3: #131a22;
$secondary-color-2: #edeef0;
$font-family: 'Averia Serif Libre', serif;
$container-color:#D9D9D933
80 changes: 80 additions & 0 deletions src/styles/Search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
@import "./Colors";

.product-search {
display: flex;
flex-direction: column;
height: 65px;
background-color: $container-color;
border-bottom: 1px solid #ccc;

.main-content-product {
display: flex;

.right-side-bar {
display: flex;
flex-direction: column;
margin-left: 50px;
height: 40px;
}

.searchPrice-box {
display: flex;
align-items: center;
width: 80rem;
height: 56px;
padding-top: 10px;
padding-bottom: 10px;
line-height: 40px;
margin-left: 5px;

&:not(:last-child) {
margin-bottom: 10px;
}

.product-name {
font-size: 20px;
margin-right: 10px;
width: 30rem;
}

.searchSpan-price {
margin: 0 6px 0 4px;
line-height: 30px;
font-size: 20px;
vertical-align: middle;
display: inline-block;
}

.dropdown-container {
display: flex;
align-items: center;

.min-span,
.max-span,
.discount-span {
display: flex;
align-items: center;
justify-content: space-between;
width: 100px;
height: 30px;
border: 1px solid rgba(255, 109, 24, 0.49);
border-radius: 4px;
padding: 0 5px;
font-size: 15px;
cursor: pointer;
background-color: #fff;
margin-right: 10px;
}
.header__selected__iconIcon {
margin-left: 8px;
color:rgba(255, 109, 24, 0.49);
}

}
}
}
}
.product-main{
height: 500px;
background-color: $container-color;
}
Loading

0 comments on commit cbd6519

Please sign in to comment.