Skip to content

Commit

Permalink
debounce name input (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbjoralt authored Oct 25, 2023
1 parent b284251 commit a3f0113
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions frontend/src/hooks/useFilteredConsultants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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<string>(currentSearch);
const [lastSearchKeyStrokeTime, setLastSearchKeyStrokeTime] =
useState<number>();

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") || "";

Expand All @@ -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(
Expand Down Expand Up @@ -132,7 +163,7 @@ export function useFilteredConsultants() {
departments,
filteredDepartments,
filteredYears,
currentNameSearch: currentSearch,
currentNameSearch: activeNameSearch,
setNameSearch,
toggleDepartmentFilter,
toggleYearFilter,
Expand Down

0 comments on commit a3f0113

Please sign in to comment.