Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added debouncing for APIs #9801

Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ ESLINT_NO_DEV_ERRORS=true
CARE_CDN_URL="https://egov-s3-facility-10bedicu.s3.amazonaws.com https://egov-s3-patient-data-10bedicu.s3.amazonaws.com http://localhost:4566"
REACT_ALLOWED_LOCALES="en,hi,ta,ml,mr,kn"

REACT_ENABLED_APPS=""
REACT_ENABLED_APPS= ""
4 changes: 2 additions & 2 deletions package-lock.json

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

15 changes: 12 additions & 3 deletions src/pages/FacilityOrganization/FacilityOrganizationView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { Link } from "raviger";
import { useState } from "react";
import { useEffect, useState } from "react";

import CareIcon from "@/CAREUI/icons/CareIcon";

Expand All @@ -11,6 +11,8 @@ import { Input } from "@/components/ui/input";

import Pagination from "@/components/Common/Pagination";

import useDebounce from "@/hooks/useDebounce";

import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";

Expand All @@ -25,8 +27,15 @@ interface Props {
export default function FacilityOrganizationView({ id, facilityId }: Props) {
const [page, setPage] = useState(1);
const [searchQuery, setSearchQuery] = useState("");
const [debouncedQuery, setDebouncedQuery] = useState("");
const limit = 12; // 3x4 grid
const debounceQuery = useDebounce((newParams) => {
setDebouncedQuery(newParams);
}, 500);

useEffect(() => {
debounceQuery(searchQuery);
}, [searchQuery]);
const { data: children, isLoading } = useQuery({
queryKey: [
"facilityOrganization",
Expand All @@ -35,15 +44,15 @@ export default function FacilityOrganizationView({ id, facilityId }: Props) {
id,
page,
limit,
searchQuery,
debouncedQuery,
],
queryFn: query(routes.facilityOrganization.list, {
pathParams: { facilityId, organizationId: id },
queryParams: {
parent: id,
offset: (page - 1) * limit,
limit,
name: searchQuery || undefined,
name: debouncedQuery || undefined,
},
}),
});
Expand Down
20 changes: 16 additions & 4 deletions src/pages/Organization/OrganizationFacilities.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { Link } from "raviger";
import { useEffect, useState } from "react";

import CareIcon from "@/CAREUI/icons/CareIcon";

Expand All @@ -9,6 +10,7 @@ import { Input } from "@/components/ui/input";

import { Avatar } from "@/components/Common/Avatar";

import useDebounce from "@/hooks/useDebounce";
import useFilters from "@/hooks/useFilters";

import routes from "@/Utils/request/api";
Expand All @@ -30,15 +32,25 @@ export default function OrganizationFacilities({
const { qParams, Pagination, advancedFilter, resultsPerPage, updateQuery } =
useFilters({ limit: 14, cacheBlacklist: ["facility"] });

const [debouncedParams, setDebouncedParams] = useState(qParams);

const debounceParams = useDebounce((newParams) => {
setDebouncedParams(newParams);
}, 500);

useEffect(() => {
debounceParams(qParams);
}, [JSON.stringify(qParams)]);

const { data: facilities, isLoading } = useQuery({
queryKey: ["organizationFacilities", id, qParams],
queryKey: ["organizationFacilities", id, debouncedParams],
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Update query parameters to match the debouncing split.

If implementing the suggested split of debounced parameters, update the query parameters accordingly.

 queryKey: ["organizationFacilities", id, debouncedParams],
 queryFn: query(routes.facility.list, {
   queryParams: {
-    page: debouncedParams.page,
+    page: qParams.page,
     limit: resultsPerPage,
-    offset: (debouncedParams.page - 1) * resultsPerPage,
+    offset: (qParams.page - 1) * resultsPerPage,
     organization: id,
-    name: debouncedParams.name,
+    name: debouncedSearch || undefined,
     ...advancedFilter.filter,
   },
 }),

Also applies to: 45-49

queryFn: query(routes.facility.list, {
queryParams: {
page: qParams.page,
page: debouncedParams.page,
limit: resultsPerPage,
offset: (qParams.page - 1) * resultsPerPage,
offset: (debouncedParams.page - 1) * resultsPerPage,
organization: id,
name: qParams.name,
name: debouncedParams.name,
...advancedFilter.filter,
},
}),
Expand Down
18 changes: 14 additions & 4 deletions src/pages/Organization/OrganizationView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { Link } from "raviger";
import { useState } from "react";
import { useEffect, useState } from "react";

import CareIcon from "@/CAREUI/icons/CareIcon";

Expand All @@ -11,6 +11,8 @@ import { Input } from "@/components/ui/input";

import Pagination from "@/components/Common/Pagination";

import useDebounce from "@/hooks/useDebounce";

import query from "@/Utils/request/query";
import { Organization, getOrgLabel } from "@/types/organization/organization";
import organizationApi from "@/types/organization/organizationApi";
Expand All @@ -25,16 +27,24 @@ interface Props {
export default function OrganizationView({ id, navOrganizationId }: Props) {
const [page, setPage] = useState(1);
const [searchQuery, setSearchQuery] = useState("");
Copy link
Member

Choose a reason for hiding this comment

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

this should be debounced instead

Copy link
Contributor Author

@i0am0arunava i0am0arunava Jan 8, 2025

Choose a reason for hiding this comment

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

this should be debounced instead

i tried this one but problem is If searchQuery is debounced directly, the input's value={searchQuery} value will lag because the state only updates after the debounce delay

const [searchQuery, setSearchQuery] = useDebouncedState("", 500);

const limit = 12; // 3x4 grid

const { data: children, isLoading } = useQuery({
  queryKey: ["organization", id, "children", page, limit, searchQuery],
  queryFn: query(organizationApi.list, {
    queryParams: {
      parent: id,
      offset: (page - 1) * limit,
      limit,
      name: searchQuery || undefined,
    },
  }),
});

return (
  <Input
    placeholder="Search by name..."
    value={searchQuery}
    onChange={(e) => {
      setSearchQuery(e.target.value);
      setPage(1); // Reset to first page on search
    }}
    className="w-full"
  />
);

To fix this, we need two states but for distinct purposes:

A searchQuery state to handle the immediate user input.
A debounced state (debouncedParams) for triggering the query.

const limit = 12; // 3x4 grid

const [debouncedQuery, setDebouncedQuery] = useState("");

const limit = 12; // 3x4 grid
const debounceQuery = useDebounce((newParams) => {
setDebouncedQuery(newParams);
}, 500);
useEffect(() => {
debounceQuery(searchQuery);
}, [searchQuery]);
const { data: children, isLoading } = useQuery({
queryKey: ["organization", id, "children", page, limit, searchQuery],
queryKey: ["organization", id, "children", page, limit],
queryFn: query(organizationApi.list, {
queryParams: {
parent: id,
offset: (page - 1) * limit,
limit,
name: searchQuery || undefined,
name: debouncedQuery || undefined,
},
}),
});
Expand Down
Loading