2024-12-26 00:43:39 +05:00
|
|
|
'use client'
|
|
|
|
import { deviceCartAtom } from '@/lib/atoms'
|
|
|
|
import { cn } from '@/lib/utils'
|
2024-12-25 17:21:04 +05:00
|
|
|
import type { Device } from '@prisma/client'
|
2024-12-26 00:43:39 +05:00
|
|
|
import { useAtom } from 'jotai'
|
2024-12-25 17:21:04 +05:00
|
|
|
import Link from 'next/link'
|
|
|
|
import AddDevicesToCartButton from './add-devices-to-cart-button'
|
|
|
|
import BlockDeviceDialog from './block-device-dialog'
|
|
|
|
import { Badge } from './ui/badge'
|
|
|
|
|
|
|
|
export default function DeviceCard({ device, parentalControl }: { device: Device, parentalControl?: boolean }) {
|
2024-12-26 00:43:39 +05:00
|
|
|
const [devices, setDeviceCart] = useAtom(deviceCartAtom)
|
|
|
|
|
|
|
|
const isChecked = devices.some((d) => d.id === device.id);
|
|
|
|
|
2024-12-25 17:21:04 +05:00
|
|
|
return (
|
2024-12-26 00:43:39 +05:00
|
|
|
<div
|
|
|
|
onKeyUp={() => { }}
|
|
|
|
onClick={() => {
|
|
|
|
if (parentalControl === true) return
|
|
|
|
setDeviceCart((prev) =>
|
|
|
|
devices.some((d) => d.id === device.id)
|
|
|
|
? prev.filter((d) => d.id !== device.id)
|
|
|
|
: [...prev, device]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
className="w-full">
|
|
|
|
<div className={cn("flex text-sm justify-between items-center my-2 p-4 border rounded-md", isChecked ? "bg-accent" : "bg-")}>
|
|
|
|
<div className=''>
|
|
|
|
<div className="font-semibold flex flex-col items-start gap-2 mb-2">
|
|
|
|
<Link
|
|
|
|
className="font-medium hover:underline"
|
|
|
|
href={`/devices/${device.id}`}
|
|
|
|
>
|
|
|
|
{device.name}
|
|
|
|
</Link>
|
|
|
|
<Badge variant={"secondary"}>
|
|
|
|
<span className="font-medium">
|
|
|
|
{device.mac}
|
|
|
|
</span>
|
|
|
|
</Badge>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{device.isActive && (
|
|
|
|
<span className="text-muted-foreground">
|
|
|
|
Active until{" "}
|
|
|
|
{new Date().toLocaleDateString("en-US", {
|
|
|
|
month: "short",
|
|
|
|
day: "2-digit",
|
|
|
|
year: "numeric",
|
|
|
|
})}
|
2024-12-25 17:21:04 +05:00
|
|
|
</span>
|
2024-12-26 00:43:39 +05:00
|
|
|
)}
|
2024-12-25 17:21:04 +05:00
|
|
|
|
2025-01-01 23:48:56 +05:00
|
|
|
{(device.blocked && device.blockedBy === "ADMIN") && (
|
2024-12-26 00:43:39 +05:00
|
|
|
<div className="p-2 rounded border my-2 w-full">
|
2024-12-26 20:25:38 +05:00
|
|
|
<span className='uppercase text-red-500'>Blocked by admin </span>
|
2024-12-26 00:43:39 +05:00
|
|
|
<p className="text-neutral-500">
|
2025-01-01 23:48:56 +05:00
|
|
|
{device?.reasonForBlocking}
|
2024-12-26 00:43:39 +05:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
{!parentalControl ? (
|
|
|
|
<AddDevicesToCartButton device={device} />
|
|
|
|
) : (
|
2025-01-01 23:48:56 +05:00
|
|
|
<BlockDeviceDialog admin={false} type={device.blocked ? "unblock" : "block"} device={device} />
|
2024-12-26 00:43:39 +05:00
|
|
|
)}
|
|
|
|
</div>
|
2024-12-25 17:21:04 +05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|