From a3f011350660c2f33c19d3a61936e875d4b84858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B8ralt?= <61310258+jonasbjoralt@users.noreply.github.com> Date: Wed, 25 Oct 2023 11:11:17 +0200 Subject: [PATCH] debounce name input (#191) --- frontend/src/hooks/useFilteredConsultants.ts | 47 ++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/frontend/src/hooks/useFilteredConsultants.ts b/frontend/src/hooks/useFilteredConsultants.ts index 073db720..ce0ccdc4 100644 --- a/frontend/src/hooks/useFilteredConsultants.ts +++ b/frontend/src/hooks/useFilteredConsultants.ts @@ -2,7 +2,7 @@ import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { Consultant, Department, YearRange } from "@/types"; -import { useCallback, useContext, useEffect } from "react"; +import { useCallback, useContext, useEffect, useState } from "react"; import { FilteredContext } from "@/components/FilteredConsultantProvider"; import { yearRanges } from "@/components/ExperienceFilter"; @@ -11,8 +11,42 @@ export function useFilteredConsultants() { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); - const currentSearch = searchParams.get("search") || ""; + + const [activeNameSearch, setActiveNameSearch] = + useState(currentSearch); + const [lastSearchKeyStrokeTime, setLastSearchKeyStrokeTime] = + useState(); + + useEffect(() => { + let nameSearchDebounceTimer = setTimeout(() => { + if ( + lastSearchKeyStrokeTime && + Date.now() - lastSearchKeyStrokeTime > 250 + ) { + const currentDepartmentFilter = searchParams.get("depFilter") || ""; + const currentYearFilter = searchParams.get("yearFilter") || ""; + + router.push( + `${pathname}?search=${activeNameSearch}&depFilter=${currentDepartmentFilter}&yearFilter=${currentYearFilter}`, + ); + } + }, 250); + + // this will clear Timeout + // when component unmount like in willComponentUnmount + // and show will not change to true + return () => { + clearTimeout(nameSearchDebounceTimer); + }; + }, [ + lastSearchKeyStrokeTime, + activeNameSearch, + searchParams, + router, + pathname, + ]); + const currentDepartmentFilter = searchParams.get("depFilter") || ""; const currentYearFilter = searchParams.get("yearFilter") || ""; @@ -34,11 +68,8 @@ export function useFilteredConsultants() { ); function setNameSearch(newSearch: string) { - const currentDepartmentFilter = searchParams.get("depFilter") || ""; - const currentYearFilter = searchParams.get("yearFilter") || ""; - router.push( - `${pathname}?search=${newSearch}&depFilter=${currentDepartmentFilter}&yearFilter=${currentYearFilter}`, - ); + setActiveNameSearch(newSearch); + setLastSearchKeyStrokeTime(Date.now()); } const toggleDepartmentFilter = useCallback( @@ -132,7 +163,7 @@ export function useFilteredConsultants() { departments, filteredDepartments, filteredYears, - currentNameSearch: currentSearch, + currentNameSearch: activeNameSearch, setNameSearch, toggleDepartmentFilter, toggleYearFilter,