diff --git a/src/pages/tech-companies.tsx b/src/pages/tech-companies.tsx index 4a013f3..31e8a18 100644 --- a/src/pages/tech-companies.tsx +++ b/src/pages/tech-companies.tsx @@ -1,3 +1,5 @@ +import Fuse from "fuse.js"; +import { useEffect, useState } from "react"; import { useQuery } from "@tanstack/react-query"; // @@ -17,11 +19,18 @@ import { HiOutlineLocationMarker } from "react-icons/hi"; // import { getTechCompanies } from "../utils/apis"; +// +import type { IApiTechCompany } from "../types/apis.types"; + /** * */ export default function TechCompaniesPage() { - const { data, isFetching } = useQuery({ + const [searchTerm, setSearchTerm] = useState(""); + const [filteredItems, setFilteredItems] = useState([]); + + // + const { data: companiesData } = useQuery({ queryKey: ["get-companies-list"], queryFn: async () => { return getTechCompanies(); @@ -29,15 +38,37 @@ export default function TechCompaniesPage() { initialData: [], }); + // + useEffect(() => { + const fuseList = new Fuse(companiesData, { + keys: ["name"], + threshold: 0.3, + ignoreLocation: true, + }); + + const finalItemList = searchTerm + ? fuseList.search(searchTerm).map((res) => res.item) + : companiesData; + + setFilteredItems(finalItemList); + }, [companiesData, searchTerm]); + // return (

Tech companies in Nepal

- {isFetching &&

Loading data...

} +
+ setSearchTerm(e.target.value)} + placeholder="Search for tech company" + className="focus:ring-hightlght w-full max-w-lg rounded-sm bg-gray-800 px-3 py-2 text-gray-200 placeholder:text-gray-400 focus:outline-none focus:ring-1 focus:ring-highlight" + /> +
- {data.map((company) => ( + {filteredItems.map((company) => (