From 97ca7662f097a9453745dda15f4d6a4da6ff5947 Mon Sep 17 00:00:00 2001 From: MANISHIMWESalton <149325279+MANISHIMWESalton@users.noreply.github.com> Date: Thu, 11 Jul 2024 02:18:29 +0200 Subject: [PATCH] user search product UI --- src/components/inputs/SearchInput.tsx | 58 +++++++++---- src/pages/Search.tsx | 104 +++++++++++++++++++---- src/styles/Colors.scss | 1 + src/styles/Search.scss | 87 +++++++++++++++++++ src/styles/SearchInput.scss | 117 +++++++++++++++++--------- 5 files changed, 291 insertions(+), 76 deletions(-) create mode 100644 src/styles/Search.scss diff --git a/src/components/inputs/SearchInput.tsx b/src/components/inputs/SearchInput.tsx index 8feecedd..7ce3ae7e 100644 --- a/src/components/inputs/SearchInput.tsx +++ b/src/components/inputs/SearchInput.tsx @@ -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, @@ -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) => { + const value = e.target.value; + setSearch(value); + if (value.trim()) { + dispatch(searchProduct({ name: value.trim() })); + } + }; return ( -
-
+
+ { + e.preventDefault(); + if (search.trim()) { + navigate(`/search?name=${search.trim()}`); + } + }} + >
{ - setSearch(e.target.value); - dispatch(searchProduct({ name: e.target.value })); - }} + onFocus={() => setIsFocused(true)} + onBlur={() => setIsFocused(false)} + onChange={handleSearchChange} + value={search} /> -
- {searchResults.length > 0 ? ( - searchResults.map((product: any) => ( -

{product.name}

- )) - ) : ( -

No product

- )} -
+ {(isFocused || isHoved) && ( +
setIsHoved(true)} > + {searchResults.length > 0 ? ( + searchResults.map((product: any) => ( +
+ +

{product.name}

+ +
+ ) + ) + ) :null + } +
+ )}
); } diff --git a/src/pages/Search.tsx b/src/pages/Search.tsx index dadbacff..5997a630 100644 --- a/src/pages/Search.tsx +++ b/src/pages/Search.tsx @@ -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(); @@ -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); @@ -30,23 +45,78 @@ const SearchBar: React.FC = () => { ); }, []); return ( -
-

search

- {searchError &&

Error: {searchError}

} -
- {searchResults.length > 0 ? ( -
    - {searchResults.map((product: any) => ( -
  • - {product.name} : ${product.price} -
  • - ))} -
- ) : ( -

No products found.

- )} -
-
+ <> +
+ { + loading ? + : + <> +
+
+
+
+ {name}: + Price: +
+ + +
+
+ Discount: + +
+
+
+
+
+
+ {false ? ( +
+ ) : searchError ? ( +
+

{"Something went wrong. Please try again later."}

+
+ ) : ( +
+
+ {searchResults && + searchResults.map((product: any) => ( + + ))} +
+
+ )} +
+
+ + } + +