Skip to content

Commit

Permalink
Refactor useSearch to be more generic
Browse files Browse the repository at this point in the history
for reusability across pages
  • Loading branch information
zainfathoni committed Jul 17, 2021
1 parent 63cf7e4 commit bd1e731
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
27 changes: 27 additions & 0 deletions lib/hooks/use-search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState } from "react";

export function useSearch<T = unknown[]>(items: T[], fieldNames: string[]) {
const [filteredItems, setFilteredItems] = useState<T[]>(items);
const handleSubmitKeywords = (keywords: string) => {
const lowerKeywords = keywords.toLowerCase();
setFilteredItems(
items.filter((item) => {
const filterBy = (fieldName: keyof T) => {
const selectedField = item[fieldName];

if (typeof selectedField === "string") {
return selectedField.toLowerCase().includes(lowerKeywords);
} else {
return false;
}
};

const filterFunctions = fieldNames.map((fieldName) =>
filterBy(fieldName as keyof T)
);
return filterFunctions.reduce((acc, curr) => acc || curr, false);
})
);
};
return [filteredItems, handleSubmitKeywords] as const;
}
40 changes: 13 additions & 27 deletions pages/provinces/[provinceSlug]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GetStaticPaths, GetStaticProps } from "next";
import { useState } from "react";
import { ContactList } from "../../../components/contact-list";
import { SearchForm } from "../../../components/search-form";
import { useSearch } from "../../../lib/hooks/use-search";
import provinces, {
Contact,
getProvincesPaths,
Expand All @@ -14,35 +14,21 @@ type ProvinceProps = {
provinceSlug: string;
};

const useSearch = (items: Contact[]) => {
const [filteredItems, setFilteredItems] = useState<Contact[]>(items);
const handleSubmitKeywords = (keywords: string) => {
const lowerKeywords = keywords.toLowerCase();
setFilteredItems(
items.filter((item) => {
const filterBy = (fieldName: keyof Contact) =>
item[fieldName]?.toLowerCase().includes(lowerKeywords) ?? false;
return (
filterBy("kebutuhan") ||
filterBy("penyedia") ||
filterBy("lokasi") ||
filterBy("alamat") ||
filterBy("keterangan") ||
filterBy("kontak") ||
filterBy("tautan") ||
filterBy("tambahan_informasi") ||
filterBy("bentuk_verifikasi")
);
})
);
};
return [filteredItems, handleSubmitKeywords] as const;
};

export default function ProvincePage(props: ProvinceProps) {
const { province, provinceSlug } = props;
const [filteredContacts, handleSubmitKeywords] = useSearch(
props.province.data
props.province.data,
[
"kebutuhan",
"penyedia",
"lokasi",
"alamat",
"keterangan",
"kontak",
"tautan",
"tambahan_informasi",
"bentuk_verifikasi",
]
);

if (province) {
Expand Down
17 changes: 3 additions & 14 deletions pages/provinces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,17 @@ import { GetStaticProps } from "next";
import provinces from "../../lib/provinces";
import { getInitial, getSlug } from "../../lib/string-utils";
import { SearchForm } from "../../components/search-form";
import { useState } from "react";
import { ProvinceList, ProvinceListItem } from "../../components/province-list";
import { useSearch } from "../../lib/hooks/use-search";

type ProvincesPageProps = {
provincesList: ProvinceListItem[];
};

const useSearch = (items: ProvinceListItem[]) => {
const [filteredItems, setFilteredItems] = useState<ProvinceListItem[]>(items);
const handleSubmitKeywords = (keywords: string) => {
setFilteredItems(
items.filter((item) => {
return item.name.toLowerCase().includes(keywords.toLowerCase());
})
);
};
return [filteredItems, handleSubmitKeywords] as const;
};

export default function ProvincesPage(props: ProvincesPageProps) {
const [filteredProvinces, handleSubmitKeywords] = useSearch(
props.provincesList
props.provincesList,
["name"]
);
return (
<div>
Expand Down

0 comments on commit bd1e731

Please sign in to comment.