mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-02 03:38:22 +00:00
WIP feat(admin-devices): enhance device management from admin with dynamic filters and improved blocking functionality
This commit is contained in:
@ -1,204 +1,181 @@
|
||||
// import {
|
||||
// Table,
|
||||
// TableBody,
|
||||
// TableCaption,
|
||||
// TableCell,
|
||||
// TableFooter,
|
||||
// TableHead,
|
||||
// TableHeader,
|
||||
// TableRow,
|
||||
// } from "@/components/ui/table";
|
||||
// import { headers } from "next/headers";
|
||||
// import Link from "next/link";
|
||||
// import BlockDeviceDialog from "../block-device-dialog";
|
||||
// import DeviceCard from "../device-card";
|
||||
// import Pagination from "../pagination";
|
||||
import { HandCoins } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/auth";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCaption,
|
||||
TableCell,
|
||||
TableFooter,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { getDevices } from "@/queries/devices";
|
||||
import { tryCatch } from "@/utils/tryCatch";
|
||||
import AddDevicesToCartButton from "../add-devices-to-cart-button";
|
||||
import BlockDeviceDialog from "../block-device-dialog";
|
||||
import ClientErrorMessage from "../client-error-message";
|
||||
import DeviceCard from "../device-card";
|
||||
import Pagination from "../pagination";
|
||||
|
||||
export async function AdminDevicesTable({
|
||||
searchParams,
|
||||
parentalControl,
|
||||
}: {
|
||||
searchParams: Promise<{
|
||||
query: string;
|
||||
page: number;
|
||||
sortBy: string;
|
||||
[key: string]: unknown;
|
||||
}>;
|
||||
parentalControl?: boolean;
|
||||
}) {
|
||||
console.log(parentalControl);
|
||||
// const session = await auth.api.getSession({
|
||||
// headers: await headers(),
|
||||
// });
|
||||
// const isAdmin = session?.user.role === "ADMIN";
|
||||
const query = (await searchParams)?.query || "";
|
||||
const page = (await searchParams)?.page;
|
||||
console.log(query, 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 resolvedParams = await searchParams;
|
||||
const session = await getServerSession(authOptions);
|
||||
const isAdmin = session?.user?.is_superuser;
|
||||
|
||||
// const totalPages = Math.ceil(totalDevices / 10);
|
||||
// const limit = 10;
|
||||
// const offset = (Number(page) - 1) * limit || 0;
|
||||
const page = Number.parseInt(resolvedParams.page as string) || 1;
|
||||
const limit = 10;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// const devices = await prisma.device.findMany({
|
||||
// where: {
|
||||
// OR: [
|
||||
// {
|
||||
// name: {
|
||||
// contains: query || "",
|
||||
// mode: "insensitive",
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// mac: {
|
||||
// contains: query || "",
|
||||
// mode: "insensitive",
|
||||
// },
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// include: {
|
||||
// User: true,
|
||||
// payments: true,
|
||||
// },
|
||||
// skip: offset,
|
||||
// take: limit,
|
||||
// orderBy: {
|
||||
// name: `${sortBy}` as "asc" | "desc",
|
||||
// },
|
||||
// });
|
||||
return null;
|
||||
// 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>
|
||||
// ) : (
|
||||
// <>
|
||||
// <div className="hidden sm:block">
|
||||
// <Table className="overflow-scroll">
|
||||
// <TableCaption>Table of all devices.</TableCaption>
|
||||
// <TableHeader>
|
||||
// <TableRow>
|
||||
// <TableHead>Device Name</TableHead>
|
||||
// <TableHead>User</TableHead>
|
||||
// <TableHead>MAC Address</TableHead>
|
||||
// <TableHead>isActive</TableHead>
|
||||
// <TableHead>blocked</TableHead>
|
||||
// <TableHead>blockedBy</TableHead>
|
||||
// <TableHead>expiryDate</TableHead>
|
||||
// </TableRow>
|
||||
// </TableHeader>
|
||||
// <TableBody className="overflow-scroll">
|
||||
// {devices.map((device) => (
|
||||
// <TableRow key={device.id}>
|
||||
// <TableCell>
|
||||
// <div className="flex flex-col items-start">
|
||||
// <Link
|
||||
// className="font-medium hover:underline"
|
||||
// href={`/devices/${device.id}`}
|
||||
// >
|
||||
// {device.name}
|
||||
// </Link>
|
||||
// {device.isActive && (
|
||||
// <span className="text-muted-foreground">
|
||||
// Active until{" "}
|
||||
// {new Date().toLocaleDateString("en-US", {
|
||||
// month: "short",
|
||||
// day: "2-digit",
|
||||
// year: "numeric",
|
||||
// })}
|
||||
// </span>
|
||||
// )}
|
||||
// Build params object for getDevices
|
||||
const apiParams: Record<string, string | number | undefined> = {};
|
||||
for (const [key, value] of Object.entries(resolvedParams)) {
|
||||
if (value !== undefined && value !== "") {
|
||||
apiParams[key] = typeof value === "number" ? value : String(value);
|
||||
}
|
||||
}
|
||||
apiParams.limit = limit;
|
||||
apiParams.offset = offset;
|
||||
|
||||
// {device.blocked && (
|
||||
// <div className="p-2 rounded border my-2">
|
||||
// <span>Comment: </span>
|
||||
// <p className="text-neutral-500">
|
||||
// blocked because he was watching youtube
|
||||
// </p>
|
||||
// </div>
|
||||
// )}
|
||||
// </div>
|
||||
// </TableCell>
|
||||
// <TableCell className="font-medium">
|
||||
// {device.User?.name}
|
||||
// </TableCell>
|
||||
const [error, devices] = await tryCatch(
|
||||
getDevices(apiParams, true),
|
||||
);
|
||||
if (error) {
|
||||
if (error.message === "UNAUTHORIZED") {
|
||||
redirect("/auth/signin");
|
||||
} else {
|
||||
return <ClientErrorMessage message={error.message} />;
|
||||
}
|
||||
}
|
||||
const { meta, data } = devices;
|
||||
return (
|
||||
<div>
|
||||
{data?.length === 0 ? (
|
||||
<div className="h-[calc(100svh-400px)] text-muted-foreground flex flex-col items-center justify-center my-4">
|
||||
<h3>No devices.</h3>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="hidden sm:block">
|
||||
<Table className="overflow-scroll">
|
||||
<TableCaption>Table of all devices.</TableCaption>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Device Name</TableHead>
|
||||
<TableHead>User</TableHead>
|
||||
<TableHead>MAC Address</TableHead>
|
||||
<TableHead>Vendor</TableHead>
|
||||
<TableHead>#</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody className="overflow-scroll">
|
||||
{data?.map((device) => (
|
||||
<TableRow
|
||||
key={device.id}
|
||||
>
|
||||
<TableCell>
|
||||
<div className="flex flex-col items-start">
|
||||
<Link
|
||||
className={cn(
|
||||
"hover:underline font-semibold",
|
||||
device.is_active ? "text-green-600" : "",
|
||||
)}
|
||||
href={`/devices/${device.id}`}
|
||||
>
|
||||
{device.name}
|
||||
</Link>
|
||||
{device.is_active ? (
|
||||
<div className="text-muted-foreground">
|
||||
Active until{" "}
|
||||
<span className="font-semibold">
|
||||
{new Date(device.expiry_date || "").toLocaleDateString(
|
||||
"en-US",
|
||||
{
|
||||
month: "short",
|
||||
day: "2-digit",
|
||||
year: "numeric",
|
||||
},
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground">Device Inactive</p>
|
||||
)}
|
||||
{device.has_a_pending_payment && (
|
||||
<Link href={`/payments/${device.pending_payment_id}`}>
|
||||
<span className="bg-muted rounded px-2 p-1 mt-2 flex hover:underline items-center justify-center gap-2 text-muted-foreground">
|
||||
Payment Pending{" "}
|
||||
<HandCoins className="animate-pulse" size={14} />
|
||||
</span>
|
||||
</Link>
|
||||
)}
|
||||
|
||||
// <TableCell className="font-medium">{device.mac}</TableCell>
|
||||
// <TableCell>
|
||||
// {device.isActive ? "Active" : "Inactive"}
|
||||
// </TableCell>
|
||||
// <TableCell>
|
||||
// {device.blocked ? "Blocked" : "Not Blocked"}
|
||||
// </TableCell>
|
||||
// <TableCell>
|
||||
// {device.blocked ? device.blockedBy : ""}
|
||||
// </TableCell>
|
||||
// <TableCell>
|
||||
// {new Date().toLocaleDateString("en-US", {
|
||||
// month: "short",
|
||||
// day: "2-digit",
|
||||
// year: "numeric",
|
||||
// })}
|
||||
// </TableCell>
|
||||
// <TableCell>
|
||||
// <BlockDeviceDialog
|
||||
// admin={isAdmin}
|
||||
// type={device.blocked ? "unblock" : "block"}
|
||||
// device={device}
|
||||
// />
|
||||
// </TableCell>
|
||||
// </TableRow>
|
||||
// ))}
|
||||
// </TableBody>
|
||||
// <TableFooter>
|
||||
// <TableRow>
|
||||
// <TableCell colSpan={7}>
|
||||
// {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>
|
||||
// <div className="sm:hidden my-4">
|
||||
// {devices.map((device) => (
|
||||
// <DeviceCard
|
||||
// parentalControl={parentalControl}
|
||||
// key={device.id}
|
||||
// device={device}
|
||||
// />
|
||||
// ))}
|
||||
// </div>
|
||||
// </>
|
||||
// )}
|
||||
// </div>
|
||||
// );
|
||||
{device.blocked_by === "ADMIN" && device.blocked && (
|
||||
<div className="p-2 rounded border my-2 bg-white dark:bg-neutral-800 shadow">
|
||||
<span className="font-semibold">Comment</span>
|
||||
<p className="text-neutral-400">{device?.reason_for_blocking}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">{device.user}</TableCell>
|
||||
<TableCell className="font-medium">{device.mac}</TableCell>
|
||||
<TableCell className="font-medium">{device?.vendor}</TableCell>
|
||||
<TableCell>
|
||||
<BlockDeviceDialog
|
||||
admin={isAdmin}
|
||||
type={device.blocked ? "unblock" : "block"}
|
||||
device={device}
|
||||
/>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
<TableFooter>
|
||||
<TableRow>
|
||||
<TableCell colSpan={5} className="text-muted-foreground">
|
||||
{meta?.total === 1 ? (
|
||||
<p className="text-center">
|
||||
Total {meta?.total} device.
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-center">
|
||||
Total {meta?.total} devices.
|
||||
</p>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableFooter>
|
||||
</Table>
|
||||
</div>
|
||||
<div className="sm:hidden my-4">
|
||||
{data?.map((device) => (
|
||||
<DeviceCard
|
||||
parentalControl={parentalControl}
|
||||
key={device.id}
|
||||
device={device}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<Pagination
|
||||
totalPages={meta?.last_page}
|
||||
currentPage={meta?.current_page}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
</div >
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user