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 Search Feature in Devices with name and location wise | Add Clear button in Location Search #10831

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
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
20 changes: 18 additions & 2 deletions src/components/Location/LocationSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { t } from "i18next";
import { useState } from "react";
import { useTranslation } from "react-i18next";

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

import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
Expand All @@ -23,7 +26,7 @@ import locationApi from "@/types/location/locationApi";
interface LocationSearchProps {
facilityId: string;
mode?: "kind" | "instance";
onSelect: (location: LocationList) => void;
onSelect: (location: LocationList | null) => void;
disabled?: boolean;
value?: LocationList | null;
}
Expand All @@ -35,6 +38,7 @@ export function LocationSearch({
disabled,
value,
}: LocationSearchProps) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");

Expand All @@ -55,6 +59,18 @@ export function LocationSearch({
aria-expanded={open}
>
{stringifyNestedObject(value || { name: "" }) || "Select location..."}
{value && (
<Button
onClick={(e) => {
e.stopPropagation();
onSelect(null);
}}
variant="ghost"
className="hover:bg-transparent"
>
<CareIcon icon="l-times" className="h-4 w-4" />
</Button>
)}
</div>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
Expand Down
73 changes: 49 additions & 24 deletions src/pages/Facility/settings/devices/DevicesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,83 @@ 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 PageTitle from "@/components/Common/PageTitle";
import Pagination from "@/components/Common/Pagination";
import { CardGridSkeleton } from "@/components/Common/SkeletonLoading";
import { LocationSearch } from "@/components/Location/LocationSearch";

import useFilters from "@/hooks/useFilters";

import query from "@/Utils/request/query";
import DeviceCard from "@/pages/Facility/settings/devices/components/DeviceCard";
import deviceApi from "@/types/device/deviceApi";
import { LocationList } from "@/types/location/location";

interface Props {
facilityId: string;
}

export default function DevicesList({ facilityId }: Props) {
const { t } = useTranslation();
const [page, setPage] = useState(1);
const [selectedLocation, setSelectedLocation] = useState<LocationList | null>(
null,
);

const limit = 12;
const { qParams, updateQuery, Pagination, resultsPerPage } = useFilters({
limit: 12,
cacheBlacklist: ["search_text", "current_location"],
});

const { data, isLoading } = useQuery({
queryKey: ["devices", facilityId, page, limit],
queryKey: ["devices", facilityId, qParams, resultsPerPage],
queryFn: query.debounced(deviceApi.list, {
pathParams: { facility_id: facilityId },
queryParams: {
offset: (page - 1) * limit,
limit,
search_text: qParams.search_text,
current_location: selectedLocation?.id || undefined,
limit: resultsPerPage,
offset: (qParams.page - 1) * resultsPerPage,
},
}),
});

return (
<div className="space-y-6">
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
<div className="flex flex-col sm:flex-row justify-between items-center gap-4 w-full">
<div className="flex items-center gap-4">
<PageTitle title={t("devices")} />
</div>

<Button variant="primary" asChild>
<Link href="/devices/create">
<CareIcon icon="l-plus" className="h-4 w-4 mr-2" />
{t("add_device")}
</Link>
</Button>
<div className="flex flex-col sm:flex-row w-full sm:w-auto items-center gap-2 sm:gap-4">
<div className="w-full sm:w-72">
<Input
placeholder={t("search_by_name")}
value={qParams.search_text}
onChange={(e) => {
updateQuery({ search_text: e.target.value });
}}
className="w-full border border-gray-300 rounded-lg px-4 py-2 shadow-sm focus:ring-2 focus:ring-primary focus:border-primary"
/>
</div>
<div className="flex items-center gap-2 w-full sm:w-60">
<LocationSearch
facilityId={facilityId}
onSelect={(location: LocationList | null) => {
updateQuery({
current_location: location?.id || undefined,
});
setSelectedLocation(location);
}}
value={selectedLocation}
/>
</div>
<Button variant="primary" asChild>
<Link href="/devices/create">
<CareIcon icon="l-plus" className="h-4 w-4 mr-2" />
{t("add_device")}
</Link>
</Button>
</div>
</div>

{isLoading ? (
Expand All @@ -71,16 +105,7 @@ export default function DevicesList({ facilityId }: Props) {
</Card>
)}
</div>
{data && data.count > limit && (
<div className="flex justify-center">
<Pagination
data={{ totalCount: data.count }}
onChange={(page, _) => setPage(page)}
defaultPerPage={limit}
cPage={page}
/>
</div>
)}
<Pagination totalCount={data?.count ?? 0} />
</div>
)}
</div>
Expand Down
Loading