feat(rfid): front end view of the readers and there status

This commit is contained in:
2025-07-10 21:29:01 -05:00
parent 6584b37cb0
commit f7b4de8130
15 changed files with 533 additions and 36 deletions

View File

@@ -0,0 +1,20 @@
import { queryOptions } from "@tanstack/react-query";
import axios from "axios";
export function getReaders() {
return queryOptions({
queryKey: ["getReaders"],
queryFn: () => fetchReaders(),
//enabled:
staleTime: 1000,
refetchInterval: 60 * 1000,
refetchOnWindowFocus: true,
});
}
const fetchReaders = async () => {
const { data } = await axios.get(`/api/rfid/getreaders`);
// if we are not localhost ignore the devDir setting.
//const url: string = window.location.host.split(":")[0];
return data.data ?? [];
};

View File

@@ -0,0 +1,97 @@
//import { fixTime } from "@/utils/fixTime";
import { ColumnDef } from "@tanstack/react-table";
import { format } from "date-fns-tz";
// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
export type Readers = {
rfidReader_id: string;
reader: string;
readerIP: string;
lastHeartBeat: string;
lastTrigger: string;
lastTriggerGood: boolean;
active: boolean;
lastTagScanned: string;
goodReads: number;
badReads: number;
totalReads: number;
goodRatio: number;
};
export const readerColumns: ColumnDef<Readers>[] = [
{
accessorKey: "reader",
header: () => <div className="text-left">Name</div>,
},
{
accessorKey: "lastHeartBeat",
header: "Last HeartBeat",
cell: ({ row }) => {
if (row.getValue("lastHeartBeat")) {
const correctDate: any = row.getValue("lastHeartBeat");
const strippedDate = correctDate.replace("Z", ""); // Remove Z
const formattedDate = format(strippedDate, "MM/dd/yyyy HH:mm");
return (
<div className="text-left font-medium">{formattedDate}</div>
);
}
},
},
{
accessorKey: "lastTrigger",
header: "Last Trigger",
cell: ({ row }) => {
if (row.getValue("lastTrigger")) {
const correctDate: any = row.getValue("lastTrigger");
const strippedDate = correctDate.replace("Z", ""); // Remove Z
const formattedDate = format(strippedDate, "MM/dd/yyyy HH:mm");
return (
<div className="text-left font-medium">{formattedDate}</div>
);
}
},
},
{
accessorKey: "lastTriggerGood",
header: "Last Trigger Status",
},
{
accessorKey: "lastTagScanned",
header: "Last Scanned Tag",
},
{
accessorKey: "goodReads",
header: "Total Good Reads",
},
{
accessorKey: "badReads",
header: "Total Bad Reads",
},
{
accessorKey: "totalReads",
header: "Total Reads",
cell: ({ row }) => {
const total =
parseInt(row.getValue("goodReads")) +
parseInt(row.getValue("badReads"));
return <div className="text-left font-medium">{total}</div>;
},
},
{
accessorKey: "goodRatio",
header: "Good Ratio",
cell: ({ row }) => {
const goodRatio =
(parseInt(row.getValue("goodReads")) /
(parseInt(row.getValue("goodReads")) +
parseInt(row.getValue("badReads")))) *
100;
return (
<div className="text-left font-medium">
{isNaN(goodRatio) ? 0 : goodRatio}%
</div>
);
},
},
];

View File

@@ -0,0 +1,174 @@
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
getPaginationRowModel,
} from "@tanstack/react-table";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import { useState } from "react";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { ScrollArea } from "@/components/ui/scroll-area";
import { LstCard } from "@/components/extendedUI/LstCard";
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
//style: any;
}
export function ReaderTable<TData, TValue>({
columns,
data,
//style,
}: DataTableProps<TData, TValue>) {
const [pagination, setPagination] = useState({
pageIndex: 0, //initial page index
pageSize: 10, //default page size
});
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onPaginationChange: setPagination,
state: {
//...
pagination,
},
});
//console.log(parseInt(style.height.replace("px", "")) - 50);
return (
<LstCard>
<div>
<div className="flex flex-row justify-between">
{data.length === 0 ? (
<span>No readers</span>
) : (
<span>Current reader Info</span>
)}
<Select
value={pagination.pageSize.toString()}
onValueChange={(e) =>
setPagination({
...pagination,
pageSize: parseInt(e),
})
}
>
<SelectTrigger className="w-[180px]">
<SelectValue
//id={field.name}
placeholder="Select Page"
defaultValue={10}
/>
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Page Size</SelectLabel>
<SelectItem value="5">5</SelectItem>
<SelectItem value="10">10</SelectItem>
<SelectItem value="50">50</SelectItem>
<SelectItem value="100">100</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>
<ScrollArea className="h-96 rounded-md border m-2">
<Table
// style={{
// width: `${parseInt(style.width.replace("px", "")) - 50}px`,
// height: `${parseInt(style.height.replace("px", "")) - 200}px`,
// cursor: "move",
// }}
>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column
.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={
row.getIsSelected() && "selected"
}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No labels.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</ScrollArea>
</div>
<div className="flex items-center justify-end space-x-2">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</LstCard>
);
}