new features
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
"use client"
|
||||
|
||||
import { assignPositionToUser, revokePositionFromUser } from "@/actions/positions";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
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 type { AdminUser, Position } from "@/types";
|
||||
import { Check, MoreHorizontalIcon, X } from "lucide-react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface Props {
|
||||
users: AdminUser[] | null;
|
||||
setUsers: React.Dispatch<React.SetStateAction<AdminUser[] | null>>;
|
||||
positions: Position[] | null;
|
||||
canAssignPositions: boolean;
|
||||
canRevokePositions: boolean;
|
||||
}
|
||||
|
||||
export default function UserPositions({
|
||||
users,
|
||||
setUsers,
|
||||
positions,
|
||||
canAssignPositions,
|
||||
canRevokePositions,
|
||||
}: Props) {
|
||||
const t = useTranslations("MePage.items.admin.userPositions")
|
||||
|
||||
async function handleAssign(userId: string, positionCode: string) {
|
||||
const result = await assignPositionToUser(userId, positionCode)
|
||||
if (!result.success) {
|
||||
toast(t("assign.error"), { icon: <X size={16} /> })
|
||||
return
|
||||
}
|
||||
|
||||
const added = positions?.find((p) => p.code === positionCode)
|
||||
if (added) {
|
||||
setUsers((prev) => (prev || []).map((u) => {
|
||||
if (u.id !== userId) return u
|
||||
if (u.positions.some((p) => p.code === positionCode)) return u
|
||||
return { ...u, positions: [...u.positions, added] }
|
||||
}))
|
||||
}
|
||||
|
||||
toast(t("assign.success"), { icon: <Check size={16} /> })
|
||||
}
|
||||
|
||||
async function handleRevoke(userId: string, positionCode: string) {
|
||||
const result = await revokePositionFromUser(userId, positionCode)
|
||||
if (!result.success) {
|
||||
toast(t("revoke.error"), { icon: <X size={16} /> })
|
||||
return
|
||||
}
|
||||
|
||||
setUsers((prev) => (prev || []).map((u) => (
|
||||
u.id === userId
|
||||
? { ...u, positions: u.positions.filter((p) => p.code !== positionCode) }
|
||||
: u
|
||||
)))
|
||||
|
||||
toast(t("revoke.success"), { icon: <Check 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.user")}</TableHead>
|
||||
<TableHead>{t("columns.positions")}</TableHead>
|
||||
<TableHead className="text-right">{t("columns.actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{users === null && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={3} className="text-center text-sm text-muted-foreground py-6">{t("loadError")}</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{users?.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={3} className="text-center text-sm text-muted-foreground py-6">{t("empty")}</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{users?.map((user) => (
|
||||
<TableRow key={user.id}>
|
||||
<TableCell>
|
||||
<div className="flex flex-col">
|
||||
<span>{user.first_name} {user.second_name}</span>
|
||||
<span className="text-xs text-muted-foreground">{user.email}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{user.positions.length === 0 && <span className="text-sm text-muted-foreground">{t("noPositions")}</span>}
|
||||
{user.positions.map((position) => (
|
||||
<Badge key={position.code} variant="secondary">{position.name || position.code}</Badge>
|
||||
))}
|
||||
</div>
|
||||
</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">
|
||||
{canAssignPositions && (positions || []).map((position) => (
|
||||
<DropdownMenuItem
|
||||
key={`assign-${user.id}-${position.code}`}
|
||||
disabled={user.positions.some((p) => p.code === position.code)}
|
||||
onClick={() => handleAssign(user.id, position.code)}
|
||||
>
|
||||
{t("actions.assign")} {position.name}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
{canAssignPositions && (user.positions.filter(p => p.code !== "kotosoft_id_staff")).length > 0 && canRevokePositions && <DropdownMenuSeparator />}
|
||||
{canRevokePositions && user.positions.filter(p => p.code !== "kotosoft_id_staff").map((position) => (
|
||||
<DropdownMenuItem
|
||||
key={`revoke-${user.id}-${position.code}`}
|
||||
variant="destructive"
|
||||
onClick={() => handleRevoke(user.id, position.code)}
|
||||
>
|
||||
{t("actions.revoke")} {position.name}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user