Skip to content

Commit

Permalink
Error while using back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrin2005 committed Jan 7, 2025
1 parent a460730 commit aefe830
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/components/Facility/FacilityUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ import query from "@/Utils/request/query";

export default function FacilityUsers(props: { facilityId: number }) {
const { t } = useTranslation();
const { Pagination } = useFilters({
const { qParams, updateQuery, Pagination } = useFilters({
limit: 18,
cacheBlacklist: ["username"],
});
const [searchTerm, setSearchTerm] = useState("");
const [activeTab, setActiveTab] = useState(0);
const { facilityId } = props;
const { username } = qParams;

const handleSearch = (key: string, value: string) => {
updateQuery({
...qParams,
[key]: value || undefined,
});
};

const { data: userListData, isLoading: userListLoading } = useQuery({
queryKey: ["facilityUsers", facilityId],
queryKey: ["facilityUsers", facilityId, qParams],
queryFn: query(routes.facility.getUsers, {
pathParams: { facility_id: facilityId },
queryParams: {
username,
limit: qParams.limit || 18,
offset: ((qParams.page || 1) - 1) * (qParams.limit || 18),
},
}),
enabled: !!facilityId,
});

if (userListLoading) {
return <div>Loading...</div>;
}

if (!userListData) {
return <div>{t("no_users_found")}</div>;
}

const filteredUsers = searchTerm
? userListData.results.filter((user) => {
const searchString = searchTerm.toLowerCase();
return (
user.username?.toLowerCase().includes(searchString) ||
user.first_name?.toLowerCase().includes(searchString) ||
user.last_name?.toLowerCase().includes(searchString)
);
})
: userListData.results;

return (
<Page title={`${t("users")}`} hideBack={true} breadcrumbs={false}>
<CountBlock
Expand All @@ -60,14 +60,16 @@ export default function FacilityUsers(props: { facilityId: number }) {
/>

<UserListView
users={filteredUsers}
onSearch={(value) => setSearchTerm(value)}
searchValue={searchTerm}
users={userListData?.results ?? []}
onSearch={(username) => handleSearch("username", username)}
searchValue={username || ""}
activeTab={activeTab}
onTabChange={setActiveTab}
/>

<Pagination totalCount={userListData.count} />
{userListData.count > (qParams.limit || 18) && (
<Pagination totalCount={userListData.count} />
)}
</Page>
);
}

0 comments on commit aefe830

Please sign in to comment.