diff --git a/src/pages/Organization/OrganizationView.tsx b/src/pages/Organization/OrganizationView.tsx index 88cb629baf1..e7f1d8c39b2 100644 --- a/src/pages/Organization/OrganizationView.tsx +++ b/src/pages/Organization/OrganizationView.tsx @@ -1,6 +1,6 @@ import { useQuery } from "@tanstack/react-query"; import { Link } from "raviger"; -import { useState } from "react"; +import { useCallback } from "react"; import { useTranslation } from "react-i18next"; import CareIcon from "@/CAREUI/icons/CareIcon"; @@ -8,11 +8,12 @@ import CareIcon from "@/CAREUI/icons/CareIcon"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; -import { Input } from "@/components/ui/input"; -import Pagination from "@/components/Common/Pagination"; +import SearchByMultipleFields from "@/components/Common/SearchByMultipleFields"; import { CardGridSkeleton } from "@/components/Common/SkeletonLoading"; +import useFilters from "@/hooks/useFilters"; + import query from "@/Utils/request/query"; import { Organization, getOrgLabel } from "@/types/organization/organization"; import organizationApi from "@/types/organization/organizationApi"; @@ -28,20 +29,47 @@ interface Props { export default function OrganizationView({ id, navOrganizationId }: Props) { const { t } = useTranslation(); - const [page, setPage] = useState(1); - const [searchQuery, setSearchQuery] = useState(""); - const limit = 12; // 3x4 grid + const { qParams, updateQuery, Pagination, resultsPerPage } = useFilters({ + limit: 12, // 3x4 grid + cacheBlacklist: ["name"], + }); + + const searchOptions = [ + { + key: "name", + type: "text" as const, + placeholder: t("search_by_name"), + value: qParams.name || "", + }, + ]; + + const handleSearch = useCallback( + (key: string, value: string) => { + const searchParams = { + name: key === "name" ? value : "", + }; + updateQuery(searchParams); + }, + [updateQuery], + ); + + const handleFieldChange = useCallback(() => { + updateQuery({ + name: undefined, + }); + }, [updateQuery]); const { data: children, isFetching } = useQuery({ - queryKey: ["organization", id, "children", page, limit, searchQuery], + queryKey: ["organization", id, "children", qParams], queryFn: query.debounced(organizationApi.list, { queryParams: { parent: id, - offset: (page - 1) * limit, - limit, - name: searchQuery || undefined, + offset: ((qParams.page ?? 1) - 1) * resultsPerPage, + limit: resultsPerPage, + name: qParams.name || undefined, }, }), + enabled: !!id, }); // Hack for the sidebar to work @@ -62,13 +90,13 @@ export default function OrganizationView({ id, navOrganizationId }: Props) { />
- { - setSearchQuery(e.target.value); - setPage(1); // Reset to first page on search - }} +
@@ -127,23 +155,16 @@ export default function OrganizationView({ id, navOrganizationId }: Props) { ) : ( - {searchQuery + {qParams.name ? t("no_organizations_found") : t("no_sub_organizations_found")} )} - {children && children.count > limit && ( -
- setPage(page)} - defaultPerPage={limit} - cPage={page} - /> -
- )} +
+ +
)}