diff --git a/actions/omada-actions.ts b/actions/omada-actions.ts index 4638677..af689bd 100644 --- a/actions/omada-actions.ts +++ b/actions/omada-actions.ts @@ -123,11 +123,7 @@ export async function blockDevice({ if (!macAddress) { throw new Error("macAddress is a required parameter"); } - // const device = await prisma.device.findFirst({ - // where: { - // mac: macAddress, - // }, - // }); + try { const baseUrl: string = process.env.OMADA_BASE_URL || ""; const url: string = `${baseUrl}/api/v2/sites/${process.env.OMADA_SITE_ID}/cmd/clients/${formatMacAddress(macAddress)}/${type}`; diff --git a/app/(dashboard)/devices/[deviceId]/page.tsx b/app/(dashboard)/devices/[deviceId]/page.tsx index 37739da..bd6dfb8 100644 --- a/app/(dashboard)/devices/[deviceId]/page.tsx +++ b/app/(dashboard)/devices/[deviceId]/page.tsx @@ -1,3 +1,8 @@ +import BlockDeviceDialog from "@/components/block-device-dialog"; +import Search from "@/components/search"; +import { Badge } from "@/components/ui/badge"; +import { getDevice } from "@/queries/devices"; +import { tryCatch } from "@/utils/tryCatch"; import React from "react"; export default async function DeviceDetails({ @@ -6,20 +11,46 @@ export default async function DeviceDetails({ params: Promise<{ deviceId: string }>; }) { const deviceId = (await params)?.deviceId; + const [error, device] = await tryCatch(getDevice({ deviceId: deviceId })); + if (error) return
{error.message}
; + if (!device) return null; - return null; return (
-
-

{device?.name}

- {device?.mac} +
+
+

+ {device?.name} +

+ {device?.mac} +

+ Device active until{" "} + {new Date(device?.expiry_date || "").toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + })} +

+
+
+ {device?.expiry_date && new Date() < new Date(device.expiry_date) && ( +

+ ACTIVE +

+ )} + +
{JSON.stringify(device.blocked, null, 2)}
+
- {/* */} + {/* d.id === device.id); + const isChecked = devices.some((d) => d.id === device.id); - return ( - setDeviceCart((prev) => - isChecked - ? prev.filter((d) => d.id !== device.id) - : [...prev, device] - )} - /> - ) + return ( + + setDeviceCart((prev) => + isChecked + ? prev.filter((d) => d.id !== device.id) + : [...prev, device], + ) + } + /> + ); } diff --git a/components/block-device-dialog.tsx b/components/block-device-dialog.tsx index 71db266..9124b61 100644 --- a/components/block-device-dialog.tsx +++ b/components/block-device-dialog.tsx @@ -3,17 +3,17 @@ import { blockDevice } from "@/actions/omada-actions"; import { Button } from "@/components/ui/button"; import { - Dialog, - DialogContent, - DialogFooter, - DialogHeader, - DialogTitle, - DialogTrigger, + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; +import type { Device } from "@/lib/backend-types"; import { cn } from "@/lib/utils"; import { zodResolver } from "@hookform/resolvers/zod"; -import type { Device } from "@prisma/client"; import { OctagonX } from "lucide-react"; import { useState } from "react"; import { type SubmitHandler, useForm } from "react-hook-form"; @@ -23,160 +23,159 @@ import { TextShimmer } from "./ui/text-shimmer"; import { Textarea } from "./ui/textarea"; const validationSchema = z.object({ - reasonForBlocking: z.string().min(5, { message: "Reason is required" }), + reasonForBlocking: z.string().min(5, { message: "Reason is required" }), }); export default function BlockDeviceDialog({ - device, - type, - admin, + device, + type, + admin, }: { device: Device; type: "block" | "unblock"; admin?: boolean }) { - const [disabled, setDisabled] = useState(false); - const [open, setOpen] = useState(false); - const { - register, - handleSubmit, - formState: { errors }, - } = useForm>({ - resolver: zodResolver(validationSchema), - }); + const [disabled, setDisabled] = useState(false); + const [open, setOpen] = useState(false); + const { + register, + handleSubmit, + formState: { errors }, + } = useForm>({ + resolver: zodResolver(validationSchema), + }); - const onSubmit: SubmitHandler> = (data) => { - setDisabled(true); - console.log(data); - toast.promise( - blockDevice({ - macAddress: device.mac, - type: type, - reason: data.reasonForBlocking, - blockedBy: "ADMIN", - // reason: data.reasonForBlocking, - }), - { - loading: "Blocking...", - success: () => { - setDisabled(false); - setOpen((prev) => !prev); - return "Blocked!"; - }, - error: (error) => { - setDisabled(false); - return error || "Something went wrong"; - }, - }, - ); - setDisabled(false); - }; + const onSubmit: SubmitHandler> = (data) => { + setDisabled(true); + console.log(data); + toast.promise( + blockDevice({ + macAddress: device.mac, + type: type, + reason: data.reasonForBlocking, + blockedBy: "ADMIN", + // reason: data.reasonForBlocking, + }), + { + loading: "Blocking...", + success: () => { + setDisabled(false); + setOpen((prev) => !prev); + return "Blocked!"; + }, + error: (error) => { + setDisabled(false); + return error || "Something went wrong"; + }, + }, + ); + setDisabled(false); + }; - return ( -
- {device.blocked ? ( - - ) : ( - <> - {!admin ? ( - - ) : ( - - - - - - - - Please provide a reason for blocking this device. - - -
-
-
- -