mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-02-22 17:22:17 +00:00
- Updated `package.json` to include a new script for launching Prisma Studio. - Modified `signup` function in `auth-actions.ts` to include account number in user data. - Refactored `createPayment` function in `payment.ts` to improve error handling and return structured responses. - Updated UI components in the dashboard to improve layout and responsiveness, including changes to `UserDevices` and `UserPayments` pages. - Introduced new `AdminDevicesTable` and `UsersPaymentsTable` components for better admin functionalities. - Enhanced `DeviceCartDrawer` to provide user feedback during payment processing. - Added account number input to the signup form and updated validation schema accordingly. - Updated Prisma schema to include a new `ninja_user_id` field for user management. These changes improve the overall functionality, maintainability, and user experience of the application, particularly in user management and payment processing.
188 lines
4.7 KiB
TypeScript
188 lines
4.7 KiB
TypeScript
import {
|
|
Table,
|
|
TableBody,
|
|
TableCaption,
|
|
TableCell,
|
|
TableFooter,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from "@/components/ui/table";
|
|
import { auth } from "@/lib/auth";
|
|
import prisma from "@/lib/db";
|
|
import { headers } from "next/headers";
|
|
import ClickableRow from "./clickable-row";
|
|
import DeviceCard from "./device-card";
|
|
import Pagination from "./pagination";
|
|
|
|
export async function DevicesTable({
|
|
searchParams,
|
|
parentalControl,
|
|
}: {
|
|
searchParams: Promise<{
|
|
query: string;
|
|
page: number;
|
|
sortBy: string;
|
|
}>;
|
|
parentalControl?: boolean;
|
|
}) {
|
|
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;
|
|
const sortBy = (await searchParams)?.sortBy || "asc";
|
|
const totalDevices = await prisma.device.count({
|
|
where: {
|
|
userId: isAdmin ? undefined : session?.session.userId,
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
mac: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
],
|
|
NOT: {
|
|
payment: {
|
|
paid: false
|
|
}
|
|
},
|
|
isActive: isAdmin ? undefined : parentalControl,
|
|
blocked: isAdmin ? undefined : parentalControl !== undefined ? undefined : false,
|
|
},
|
|
});
|
|
|
|
const totalPages = Math.ceil(totalDevices / 10);
|
|
const limit = 10;
|
|
const offset = (Number(page) - 1) * limit || 0;
|
|
|
|
const devices = await prisma.device.findMany({
|
|
where: {
|
|
userId: session?.session.userId,
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
mac: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
],
|
|
NOT: {
|
|
payment: {
|
|
paid: false
|
|
},
|
|
},
|
|
isActive: parentalControl,
|
|
blocked: parentalControl !== undefined ? undefined : false,
|
|
},
|
|
|
|
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>
|
|
) : (
|
|
<>
|
|
<div className="hidden sm:block">
|
|
<Table className="overflow-scroll">
|
|
<TableCaption>Table of all devices.</TableCaption>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>Device Name</TableHead>
|
|
<TableHead>MAC Address</TableHead>
|
|
<TableHead>#</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>
|
|
// <span className="text-muted-foreground">
|
|
// Active until{" "}
|
|
// {new Date().toLocaleDateString("en-US", {
|
|
// month: "short",
|
|
// day: "2-digit",
|
|
// year: "numeric",
|
|
// })}
|
|
// </span>
|
|
// {parentalControl && (
|
|
// <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.mac}</TableCell>
|
|
// <TableCell>
|
|
// {!parentalControl ? (
|
|
// <AddDevicesToCartButton device={device} />
|
|
// ) : (
|
|
// <BlockDeviceButton device={device} />
|
|
// )}
|
|
// </TableCell>
|
|
// </TableRow>
|
|
<ClickableRow admin={isAdmin} key={device.id} device={device} parentalControl={parentalControl} />
|
|
))}
|
|
</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>
|
|
<div className="sm:hidden my-4">
|
|
{devices.map((device) => (
|
|
<DeviceCard parentalControl={parentalControl} key={device.id} device={device} />
|
|
))}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|