fixes
This commit is contained in:
@@ -1,87 +1,96 @@
|
||||
"use client"
|
||||
|
||||
import { useState, useEffect, useMemo } from "react";
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardFooter } from "@/components/ui/card";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { MoreHorizontalIcon } from "lucide-react"
|
||||
import CreatePermissionDialog from "./dialogs/createPermissionDialog";
|
||||
import { Permission } from "@/types";
|
||||
import { deletePermission, listAllPermissions } from "@/actions/positions";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import EditPermissionDialog from "./dialogs/editPermissionDialog";
|
||||
import { MoreHorizontalIcon } from "lucide-react";
|
||||
import { Check, X } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { PermissionDialog } from "./dialogs/permissionDialog";
|
||||
import { removePermission } from "@/actions/positions";
|
||||
import type { Permission } from "@/types";
|
||||
|
||||
export default function Permissions() {
|
||||
const [permissions, setPermissions] = useState<Permission[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
interface Props {
|
||||
permissions: Permission[] | null;
|
||||
setPermissions: React.Dispatch<React.SetStateAction<Permission[] | null>>;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
const data = await listAllPermissions();
|
||||
setPermissions(data);
|
||||
setIsLoading(false);
|
||||
}
|
||||
export default function Permissions({ permissions, setPermissions }: Props) {
|
||||
const t = useTranslations("MePage.items.admin.permissions")
|
||||
const pt = useTranslations("permissions")
|
||||
|
||||
load()
|
||||
}, [])
|
||||
function handleCreated(permission: Permission) {
|
||||
setPermissions((prev) => [...(prev || []), permission])
|
||||
}
|
||||
|
||||
const permissionsMemo = useMemo(() => permissions, [permissions]);
|
||||
function handleUpdated(code: string, permission: Permission) {
|
||||
setPermissions((prev) => (prev || []).map((p) => (p.code === code ? permission : p)))
|
||||
}
|
||||
|
||||
async function handleDelete(code: string) {
|
||||
const result = await deletePermission(code)
|
||||
|
||||
const result = await removePermission(code)
|
||||
if (result.success) {
|
||||
setPermissions((prev) =>
|
||||
prev.filter((p) => p.code !== code)
|
||||
)
|
||||
setPermissions((prev) => (prev || []).filter((p) => p.code !== code))
|
||||
toast(t("delete.success"), { icon: <Check size={16} /> })
|
||||
} else {
|
||||
toast(t("delete.error"), { icon: <X size={16} /> })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-1">
|
||||
<h2 className="text-lg">Permissions</h2>
|
||||
<h4 className="text-sm text-muted-foreground">Here You can create permissions which later You can assign them to positions</h4>
|
||||
<h2 className="text-lg">{t("title")}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t("subtitle")}</p>
|
||||
</div>
|
||||
<Card size="sm" className="w-full">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<Table>
|
||||
<TableHeader className="rounded-lg">
|
||||
<TableRow>
|
||||
<TableHead>{t("columns.code")}</TableHead>
|
||||
<TableHead>{t("columns.description")}</TableHead>
|
||||
<TableHead className="text-right">{t("columns.actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{permissions?.length === 0 && (
|
||||
<TableRow>
|
||||
<TableHead>Code</TableHead>
|
||||
<TableHead>Description</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
<TableCell colSpan={3} className="text-center text-sm text-muted-foreground py-6">{t("empty")}</TableCell>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{permissionsMemo.map((permission) => (
|
||||
)}
|
||||
{permissions?.map((permission) => (
|
||||
<TableRow key={permission.code}>
|
||||
<TableCell>{permission.code}</TableCell>
|
||||
<TableCell>{permission.description}</TableCell>
|
||||
<TableCell className="font-mono">{permission.code}</TableCell>
|
||||
<TableCell className="text-muted-foreground">{pt(`${permission.code}`)}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="icon" className="size-8">
|
||||
<MoreHorizontalIcon />
|
||||
<span className="sr-only">Open menu</span>
|
||||
<span className="sr-only">{t("columns.actions")}</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<EditPermissionDialog permission={permission} />
|
||||
<PermissionDialog
|
||||
mode="edit"
|
||||
permission={permission}
|
||||
onSuccess={(updated) => handleUpdated(permission.code, updated)}
|
||||
trigger={<Button variant="ghost" size="sm" className="w-full px-2 justify-start">{t("actions.edit")}</Button>}
|
||||
/>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem variant="destructive" onClick={() => handleDelete(permission.code)}>Delete</DropdownMenuItem>
|
||||
<DropdownMenuItem variant="destructive" onClick={() => handleDelete(permission.code)}>{t("actions.delete")}</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
{isLoading && <p className="mx-2 my-1 text-muted-foreground animate-pulse">Permissions are loading...</p>}
|
||||
</Table>
|
||||
<CardFooter>
|
||||
<CreatePermissionDialog />
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</TableBody>
|
||||
</Table>
|
||||
<PermissionDialog
|
||||
mode="create"
|
||||
onSuccess={handleCreated}
|
||||
trigger={<Button variant="outline" size="sm" className="w-full">{t("actions.create")}</Button>}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user