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

Add phone number search for organization users #10598

Merged
merged 6 commits into from
Feb 18, 2025
Merged
Changes from 1 commit
Commits
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
72 changes: 58 additions & 14 deletions src/pages/Organization/OrganizationUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useQuery } from "@tanstack/react-query";
import { useCallback } from "react";
import { useTranslation } from "react-i18next";

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

import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";

import { Avatar } from "@/components/Common/Avatar";
import SearchByMultipleFields from "@/components/Common/SearchByMultipleFields";
import { CardGridSkeleton } from "@/components/Common/SkeletonLoading";
import { UserStatusIndicator } from "@/components/Users/UserListAndCard";

Expand All @@ -30,19 +31,62 @@ interface Props {
export default function OrganizationUsers({ id, navOrganizationId }: Props) {
const { qParams, updateQuery, Pagination, resultsPerPage } = useFilters({
limit: 15,
cacheBlacklist: ["search"],
cacheBlacklist: ["name", "phone_number"],
});
const { t } = useTranslation();

const searchOptions = [
{
key: "username",
type: "text" as const,
placeholder: "Search by username",
value: qParams.name || "",
},
{
key: "phone_number",
type: "phone" as const,
placeholder: "Search by phone number",
value: qParams.phone_number || "",
},
];

const handleSearch = useCallback((key: string, value: string) => {
const searchParams = {
name: key === "username" ? value : "",
phone_number:
key === "phone_number"
? value.length >= 13 || value === ""
? value
: undefined
: undefined,
};
updateQuery(searchParams);
}, []);

const handleFieldChange = () => {
updateQuery({
name: undefined,
phone_number: undefined,
});
};

const openAddUserSheet = qParams.sheet === "add";
const openLinkUserSheet = qParams.sheet === "link";

const { data: users, isFetching: isFetchingUsers } = useQuery({
queryKey: ["organizationUsers", id, qParams.search, qParams.page],
queryKey: [
"organizationUsers",
id,
qParams.name,
qParams.phone_number,
qParams.page,
],
queryFn: query.debounced(organizationApi.listUsers, {
pathParams: { id },
queryParams: {
username: qParams.search,
username: qParams.name,
phone_number: qParams.phone_number,
page: qParams.page,
limit: resultsPerPage,
offset: ((qParams.page ?? 1) - 1) * resultsPerPage,
},
Expand Down Expand Up @@ -88,16 +132,16 @@ export default function OrganizationUsers({ id, navOrganizationId }: Props) {
</div>
</div>
<div className="flex gap-2">
<Input
type="text"
placeholder="Search users..."
value={qParams.search || ""}
onChange={(e) =>
updateQuery({
search: e.target.value as string,
})
}
className="max-w-sm"
<SearchByMultipleFields
id="user-search"
options={searchOptions}
initialOptionIndex={Math.max(
searchOptions.findIndex((option) => option.value !== ""),
0,
)}
onSearch={handleSearch}
onFieldChange={handleFieldChange}
className="w-full"
data-cy="search-user"
/>
</div>
Expand Down
Loading