fixes
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"use client"
|
||||
|
||||
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 { Check, X } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { PositionDialog } from "./dialogs/positionDialog";
|
||||
import { deletePosition } from "@/actions/positions";
|
||||
import type { Position } from "@/types";
|
||||
|
||||
interface Props {
|
||||
positions: Position[] | null;
|
||||
setPositions: React.Dispatch<React.SetStateAction<Position[] | null>>;
|
||||
}
|
||||
|
||||
export default function Positions({ positions, setPositions }: Props) {
|
||||
const t = useTranslations("MePage.items.admin.positions")
|
||||
|
||||
function handleCreated(position: Position) {
|
||||
setPositions((prev) => [...(prev || []), position])
|
||||
}
|
||||
|
||||
function handleUpdated(code: string, position: Position) {
|
||||
setPositions((prev) => (prev || []).map((p) => (p.code === code ? position : p)))
|
||||
}
|
||||
|
||||
async function handleDelete(code: string) {
|
||||
const result = await deletePosition(code)
|
||||
if (result.success) {
|
||||
setPositions((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">{t("title")}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t("subtitle")}</p>
|
||||
</div>
|
||||
<Table>
|
||||
<TableHeader className="rounded-lg">
|
||||
<TableRow>
|
||||
<TableHead>{t("columns.code")}</TableHead>
|
||||
<TableHead>{t("columns.name")}</TableHead>
|
||||
<TableHead>{t("columns.description")}</TableHead>
|
||||
<TableHead className="text-right">{t("columns.actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{positions?.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4} className="text-center text-sm text-muted-foreground py-6">{t("empty")}</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{positions?.map((position) => (
|
||||
<TableRow key={position.code}>
|
||||
<TableCell className="font-mono">{position.code}</TableCell>
|
||||
<TableCell>{position.name}</TableCell>
|
||||
<TableCell className="text-muted-foreground">{position.description || t("noDescription")}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="icon" className="size-8">
|
||||
<MoreHorizontalIcon />
|
||||
<span className="sr-only">{t("columns.actions")}</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<PositionDialog
|
||||
mode="edit"
|
||||
position={position}
|
||||
onSuccess={(updated) => handleUpdated(position.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(position.code)}>{t("actions.delete")}</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
<PositionDialog
|
||||
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