diff --git a/components/block-device-dialog.tsx b/components/block-device-dialog.tsx index 5615de0..2a918b5 100644 --- a/components/block-device-dialog.tsx +++ b/components/block-device-dialog.tsx @@ -6,12 +6,12 @@ import { useActionState, useEffect, useState, useTransition } from "react"; import { toast } from "sonner"; 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"; @@ -21,151 +21,151 @@ import { TextShimmer } from "./ui/text-shimmer"; import { Textarea } from "./ui/textarea"; export type BlockDeviceFormState = { - message: string; - success: boolean; - fieldErrors?: { - reason_for_blocking?: string[]; - }; - payload?: FormData; + message: string; + success: boolean; + fieldErrors?: { + reason_for_blocking?: string[]; + }; + payload?: FormData; }; const initialState: BlockDeviceFormState = { - message: "", - success: false, - fieldErrors: {}, + message: "", + success: false, + fieldErrors: {}, }; export default function BlockDeviceDialog({ - device, - // admin, - parentalControl = false, + device, + admin, + parentalControl = false, }: { - device: Device; - type: "block" | "unblock"; - admin?: boolean; - parentalControl?: boolean; + device: Device; + type: "block" | "unblock"; + admin?: boolean; + parentalControl?: boolean; }) { - const [open, setOpen] = useState(false); - const [state, formAction, isPending] = useActionState( - blockDeviceAction, - initialState, - ); - const [isTransitioning, startTransition] = useTransition(); + const [open, setOpen] = useState(false); + const [state, formAction, isPending] = useActionState( + blockDeviceAction, + initialState, + ); + const [isTransitioning, startTransition] = useTransition(); - const handleSimpleBlock = () => { - startTransition(() => { - const formData = new FormData(); - formData.append("deviceId", String(device.id)); - formData.append("reason_for_blocking", ""); - formData.append("action", "simple-block"); - formData.append("blocked_by", "PARENT"); + const handleSimpleBlock = () => { + startTransition(() => { + const formData = new FormData(); + formData.append("deviceId", String(device.id)); + formData.append("reason_for_blocking", ""); + formData.append("action", "simple-block"); + formData.append("blocked_by", "PARENT"); - formAction(formData); - }); - }; + formAction(formData); + }); + }; - const handleUnblock = () => { - startTransition(() => { - const formData = new FormData(); - formData.append("deviceId", String(device.id)); - formData.append("reason_for_blocking", ""); - formData.append("action", "unblock"); - formData.append("blocked_by", "PARENT"); + const handleUnblock = () => { + startTransition(() => { + const formData = new FormData(); + formData.append("deviceId", String(device.id)); + formData.append("reason_for_blocking", ""); + formData.append("action", "unblock"); + formData.append("blocked_by", "PARENT"); - formAction(formData); - }); - }; + formAction(formData); + }); + }; - // Show toast notifications based on state changes - useEffect(() => { - if (state.message) { - if (state.success) { - toast.success(state.message); - if (open) setOpen(false); - } else { - toast.error(state.message); - } - } - }, [state, open]); + // Show toast notifications based on state changes + useEffect(() => { + if (state.message) { + if (state.success) { + toast.success(state.message); + if (open) setOpen(false); + } else { + toast.error(state.message); + } + } + }, [state, open]); - const isLoading = isPending || isTransitioning; + const isLoading = isPending || isTransitioning; - // If device is blocked and user is not admin, show unblock button - if (device.blocked && parentalControl) { - return ( - - ); - } + // If device is blocked and user is not admin, show unblock button + if (device.blocked && parentalControl) { + return ( + + ); + } - // If device is not blocked and user is not admin, show simple block button - if (!device.blocked && parentalControl) { - return ( - - ); - } + // If device is not blocked and user is not admin, show simple block button + if ((!device.blocked && parentalControl) || !admin) { + return ( + + ); + } - // If user is admin, show block with reason dialog - return ( -
- - - - - - - Block 🚫 - - Please provide a reason for blocking this device - - -
- - - + // If user is admin, show block with reason dialog + return ( +
+ + + + + + + Block 🚫 + + Please provide a reason for blocking this device + + + + + + -
-
- -