diff --git a/src/components/search.tsx b/src/components/search.tsx index 1b6f3e9..c8551c3 100644 --- a/src/components/search.tsx +++ b/src/components/search.tsx @@ -1,10 +1,8 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; import { Input, Text, Image, HStack, Box, Center } from "@chakra-ui/react"; -import useSWR from "swr"; import useDebounce from "@/hooks/useDebounce"; import { SVGCardProps } from "@/interfaces/components"; import CustomLink from "@/common/link"; -import Error from "@/components/error"; import { getSvgByQuery } from "@/services"; import CustomIconBtn from "@/common/iconBtn"; import { Trash } from "phosphor-react"; @@ -12,15 +10,24 @@ import Tap from "@/animations/tap"; const Search = () => { const [search, setSearch] = useState(""); + const [results, setResults] = useState([]); const debouncedSearch = useDebounce(search, 500); - const { data, error } = useSWR(`${getSvgByQuery}${debouncedSearch}`); - if (error) { - ; - } + useEffect(() => { + if (debouncedSearch) { + fetch(getSvgByQuery + debouncedSearch).then((res) => { + if (res.ok) { + res.json().then((data) => { + setResults(data); + }); + } + }); + } + }, [debouncedSearch]); const handleClear = () => { setSearch(""); + setResults([]); }; return ( @@ -29,14 +36,14 @@ const Search = () => { width="full" variant="flushed" size="lg" - placeholder="Search" + placeholder="Search svgs..." value={search} onChange={(e) => setSearch(e.target.value)} /> - {data && data.length > 0 && ( + {results && results.length > 0 && ( <> - - {data.map((item: SVGCardProps) => ( + + {results.map((item: SVGCardProps) => ( { ))} - } - onClick={handleClear} - /> + + } + onClick={handleClear} + /> + )}