From 16c5dfe8be3c4271214eb4d1dfff4be14a8fbe2d Mon Sep 17 00:00:00 2001 From: EETagent Date: Mon, 5 Dec 2022 01:26:49 +0100 Subject: [PATCH] feat: use fuzzy search instead --- frontend/package.json | 1 + frontend/pnpm-lock.yaml | 7 +++++++ .../(authenticated)/dashboard/+page.svelte | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 49ed25c..169b1c0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -37,6 +37,7 @@ "dependencies": { "axios": "^1.2.0", "filedrop-svelte": "^0.1.2", + "fuse.js": "^6.6.2", "isomorphic-dompurify": "^0.24.0", "svelte-forms-lib": "^2.0.1", "swiper": "^8.4.5", diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml index b43b646..62d28b6 100644 --- a/frontend/pnpm-lock.yaml +++ b/frontend/pnpm-lock.yaml @@ -11,6 +11,7 @@ specifiers: eslint-config-prettier: ^8.5.0 eslint-plugin-svelte3: ^4.0.0 filedrop-svelte: ^0.1.2 + fuse.js: ^6.6.2 isomorphic-dompurify: ^0.24.0 prettier: ^2.7.1 prettier-plugin-svelte: ^2.8.1 @@ -30,6 +31,7 @@ specifiers: dependencies: axios: 1.2.0 filedrop-svelte: 0.1.2 + fuse.js: 6.6.2 isomorphic-dompurify: 0.24.0 svelte-forms-lib: 2.0.1 swiper: 8.4.5 @@ -1208,6 +1210,11 @@ packages: resolution: {integrity: sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==} dev: true + /fuse.js/6.6.2: + resolution: {integrity: sha512-cJaJkxCCxC8qIIcPBF9yGxY0W/tVZS3uEISDxhYIdtk8OL93pe+6Zj7LjCqVV4dzbqcriOZ+kQ/NE4RXZHsIGA==} + engines: {node: '>=10'} + dev: false + /glob-parent/5.1.2: resolution: {integrity: sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==} engines: {node: '>= 6'} diff --git a/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte b/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte index ed6513d..d256faa 100644 --- a/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte +++ b/frontend/src/routes/(admin)/admin/(authenticated)/dashboard/+page.svelte @@ -6,6 +6,7 @@ import { onMount } from 'svelte'; import type { PageData } from '../$types'; import CreateCandidateModal from '$lib/components/admin/CreateCandidateModal.svelte'; + import Fuse from 'fuse.js'; let candidates: Array = [{}]; @@ -57,15 +58,16 @@ $: candidatesTable = candidates; let searchValue: string = ''; + $: fuse = new Fuse(candidates, { + keys: ['applicationId', 'name', 'surname', 'study'] + }); + const search = () => { - candidatesTable = candidates.filter((candidate) => { - return ( - candidate.applicationId?.toString().toLowerCase().includes(searchValue.toLowerCase()) || - candidate.name?.toLowerCase().includes(searchValue.toLowerCase()) || - candidate.surname?.toLowerCase().includes(searchValue.toLowerCase()) || - candidate.study?.toLowerCase().includes(searchValue.toLowerCase()) - ); - }); + if (searchValue === '' || !searchValue) { + candidatesTable = candidates; + } else { + candidatesTable = fuse.search(searchValue).map((result) => result.item); + } };