2024-11-30 23:38:32 +05:00
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCaption,
|
|
|
|
TableCell,
|
|
|
|
TableFooter,
|
|
|
|
TableHead,
|
|
|
|
TableHeader,
|
|
|
|
TableRow,
|
|
|
|
} from "@/components/ui/table";
|
|
|
|
import prisma from "@/lib/db";
|
2024-12-06 14:16:05 +05:00
|
|
|
import AddDevicesToCartButton from "./add-devices-to-cart-button";
|
2024-11-30 23:38:32 +05:00
|
|
|
import Pagination from "./pagination";
|
|
|
|
|
|
|
|
export async function DevicesTable({
|
|
|
|
searchParams,
|
|
|
|
}: {
|
|
|
|
searchParams: Promise<{
|
|
|
|
query: string;
|
|
|
|
page: number;
|
|
|
|
sortBy: string;
|
|
|
|
}>;
|
|
|
|
}) {
|
|
|
|
const query = (await searchParams)?.query || "";
|
|
|
|
const page = (await searchParams)?.page;
|
|
|
|
const sortBy = (await searchParams)?.sortBy || "asc";
|
|
|
|
const totalDevices = await prisma.device.count({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
name: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mac: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const totalPages = Math.ceil(totalDevices / 10);
|
|
|
|
const limit = 10;
|
|
|
|
const offset = (Number(page) - 1) * limit || 0;
|
|
|
|
|
|
|
|
const devices = await prisma.device.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
name: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mac: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
skip: offset,
|
|
|
|
take: limit,
|
|
|
|
orderBy: {
|
|
|
|
name: `${sortBy}` as "asc" | "desc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{devices.length === 0 ? (
|
|
|
|
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
|
|
|
<h3>No devices yet.</h3>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Table className="overflow-scroll">
|
|
|
|
<TableCaption>Table of all devices.</TableCaption>
|
|
|
|
<TableHeader>
|
|
|
|
<TableRow>
|
|
|
|
<TableHead>Device Name</TableHead>
|
|
|
|
<TableHead>MAC Address</TableHead>
|
2024-12-06 14:16:05 +05:00
|
|
|
<TableHead>Actions</TableHead>
|
2024-11-30 23:38:32 +05:00
|
|
|
</TableRow>
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody className="overflow-scroll">
|
|
|
|
{devices.map((device) => (
|
2024-12-06 14:16:05 +05:00
|
|
|
<TableRow key={device.id}>
|
2024-11-30 23:38:32 +05:00
|
|
|
<TableCell className="font-medium">{device.name}</TableCell>
|
|
|
|
<TableCell className="font-medium">{device.mac}</TableCell>
|
|
|
|
<TableCell>
|
2024-12-06 14:16:05 +05:00
|
|
|
<AddDevicesToCartButton device={device} />
|
2024-11-30 23:38:32 +05:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
<TableFooter>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell colSpan={2}>
|
|
|
|
{query.length > 0 && (
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
Showing {devices.length} locations for "{query}
|
|
|
|
"
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className="text-muted-foreground">
|
|
|
|
{totalDevices} devices
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
</TableFooter>
|
|
|
|
</Table>
|
|
|
|
<Pagination totalPages={totalPages} currentPage={page} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|