diff --git a/src/components/search.tsx b/src/components/search.tsx index c8551c3..5fc0ab2 100644 --- a/src/components/search.tsx +++ b/src/components/search.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import { Input, Text, Image, HStack, Box, Center } from "@chakra-ui/react"; +import { Input, Text, Image, HStack, Box, Center, Spinner } from "@chakra-ui/react"; import useDebounce from "@/hooks/useDebounce"; import { SVGCardProps } from "@/interfaces/components"; import CustomLink from "@/common/link"; @@ -10,6 +10,7 @@ import Tap from "@/animations/tap"; const Search = () => { const [search, setSearch] = useState(""); + const [empty, setEmpty] = useState(false); const [results, setResults] = useState([]); const debouncedSearch = useDebounce(search, 500); @@ -18,6 +19,7 @@ const Search = () => { fetch(getSvgByQuery + debouncedSearch).then((res) => { if (res.ok) { res.json().then((data) => { + setEmpty(data.length === 0); setResults(data); }); } @@ -25,6 +27,11 @@ const Search = () => { } }, [debouncedSearch]); + const handleFilter = (e: React.ChangeEvent) => { + setEmpty(false); + setSearch(e.target.value); + }; + const handleClear = () => { setSearch(""); setResults([]); @@ -38,11 +45,13 @@ const Search = () => { size="lg" placeholder="Search svgs..." value={search} - onChange={(e) => setSearch(e.target.value)} + onChange={handleFilter} /> + {search && !empty && results.length === 0 && ()} + {search && empty && (No results found!)} {results && results.length > 0 && ( <> - + {results.map((item: SVGCardProps) => (