Skip to content

Commit

Permalink
Merge pull request #120 from dotkom/feat/debounce-search
Browse files Browse the repository at this point in the history
feat(search): add debounce to search when searching for a course
  • Loading branch information
kharann authored May 19, 2021
2 parents 39de78b + de65cd5 commit dc99b29
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .lighthouserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {
'first-cpu-idle': ['warning', { minScore: 0.7 }], // Ouff performance
'legacy-javascript': 'off', // Maybe Google Analytics?
'uses-rel-preload': 'off', // Maybe fixable?
'uses-rel-preconnect': 'off',
'mainthread-work-breakdown': 'off',
'max-potential-fid': 'off',
'unused-javascript': 'off', // Maybe fixable?
'works-offline': 'off', // Enable for PWA
'offline-start-url': 'off', // Enable for PWA
Expand Down
19 changes: 19 additions & 0 deletions common/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useState, useEffect } from 'react';

const useDebounce = <T>(value: T, delay: number) => {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
};

export default useDebounce;
2 changes: 1 addition & 1 deletion package-lock.json

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

6 changes: 4 additions & 2 deletions pages/course/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Course, CourseSort, COURSE_ORDERING } from 'models/Course';
import { GetStaticProps } from 'next';
import { Department } from 'models/Department';
import { Faculty } from 'models/Faculty';
import useDebounce from 'common/hooks/useDebounce';

interface StaticProps {
departments: Department[];
Expand Down Expand Up @@ -44,8 +45,9 @@ const CourseListPage: FC<StaticProps> = ({ departments, faculties }) => {
const [departmentId, setDepartmentId] = useState<number | null>(null);
const [facultyId, setFacultyId] = useState<number | null>(null);
const query = Array.isArray(queryParam) ? queryParam.join(',') : queryParam;
const getSearchUrl = useMemo(() => getSearchUrlPaginatedGetter(query, sortOrder, departmentId, facultyId), [
query,
const debouncedQuery = useDebounce(query, 200);
const getSearchUrl = useMemo(() => getSearchUrlPaginatedGetter(debouncedQuery, sortOrder, departmentId, facultyId), [
debouncedQuery,
sortOrder,
departmentId,
facultyId,
Expand Down

1 comment on commit dc99b29

@vercel
Copy link

@vercel vercel bot commented on dc99b29 May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.