Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
feat(app): fuzzy search the searchable list
Browse files Browse the repository at this point in the history
  • Loading branch information
yehezkieldio committed Feb 12, 2024
1 parent f5377d3 commit 332abd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
26 changes: 19 additions & 7 deletions apps/app/src/pages/complain/form/tempat-sampah.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { IonContent, IonPage } from "@ionic/react";
import { Card, CardHeader, CardContent, CardTitle, Button, Input } from "@trashtrack/ui";
import { useTrashBinQuery } from "../../../queries/get-trash-bin-query";
import { useHistory } from "react-router-dom";
import { useState } from "react";
import Fuse from "fuse.js";

import { useTrashBinQuery } from "../../../queries/get-trash-bin-query";

interface TrashBin {
id: number;
name: string;
description: string;
latitude: number;
longitude: number;
}

export function ComplainFormTempatSampah() {
const { data, isLoading } = useTrashBinQuery();
const history = useHistory();
const [searchTerm, setSearchTerm] = useState("");

const filteredData = !isLoading
? data.data.filter(
(trashBin: { id: number; name: string; description: string; latitude: number; longitude: number }) =>
trashBin.name.toLowerCase().includes(searchTerm.toLowerCase())
)
: [];
const fuseOptions = {
keys: ["name"],
threshold: 0.4,
};

const fuse = new Fuse(!isLoading ? (data.data as TrashBin[]) : [], fuseOptions);

const filteredData = !isLoading ? fuse.search(searchTerm).map((result) => result.item) : [];

return (
<IonPage>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"clsx": "^2.1.0",
"embla-carousel-autoplay": "8.0.0-rc21",
"express": "^4.18.2",
"fuse.js": "^7.0.0",
"i18next": "^23.8.2",
"lucide-react": "^0.92.0",
"nestjs": "^0.0.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 332abd7

Please sign in to comment.