import axios from "axios"; import { useEffect, useState } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Label } from "../ui/label"; import { HelpCircleIcon, Trash2Icon } from "lucide-react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "../ui/tooltip" import { Checkbox } from "../ui/checkbox"; interface Permission { id: string|undefined path: string allow: boolean|null groupId: string edit: boolean showEdit: boolean isNew: boolean permissionPaths: { path: string, description: string }[] adder: (id: string, path: string, allow: boolean|null) => void remover: (redemption: { id: string, path: string, allow: boolean|null }) => void } const GroupPermission = ({ id, path, allow, groupId, edit, showEdit, isNew, permissionPaths, adder, remover }: Permission) => { const [pathOpen, setPathOpen] = useState(false) const [isEditable, setIsEditable] = useState(edit) const [oldData, setOldData] = useState<{ path: string, allow: boolean|null } | undefined>(undefined) const [permission, setPermission] = useState<{ id: string|undefined, path: string, allow: boolean|null }>({ id, path, allow }); function Save() { if (!permission || !permission.path) return if (isNew) { axios.post("/api/settings/groups/permissions", { path: permission.path, allow: permission.allow, groupId: groupId }).then(d => { if (!d || !d.data) return adder(d.data.id, permission.path, permission.allow) setPermission({ id: undefined, path: "", allow: true }) }) } else { axios.put("/api/settings/groups/permissions", { id: permission.id, path: permission.path, allow: permission.allow }).then(d => { setIsEditable(false) }) } } function Cancel() { if (!oldData) return setPermission({ ...oldData, id: permission.id }) setIsEditable(false) setOldData(undefined) } function Delete() { axios.delete("/api/settings/groups/permissions?id=" + permission.id) .then(d => { remover(d.data) }) } return (
{!isEditable && || isEditable && No permission found. {permissionPaths.map((p) => ( { setPermission({ ...permission, path: permissionPaths.find(v => v.path.toLowerCase() == value.toLowerCase())?.path ?? value.toLowerCase()}) setPathOpen(false) }}>
{p.path}
{p.description}
))}
}
{ if (permission.allow === null) setPermission({ ...permission, allow: false }) else setPermission({ ...permission, allow: null }) }} disabled={!isEditable} /> { setPermission({ ...permission, allow: !permission.allow }) }} disabled={!isEditable || permission === undefined} />
{isEditable && } {isEditable && !isNew && } {showEdit && !isEditable && } {!isEditable && }
); } export default GroupPermission;