All files / features/admin/pages StaffManagementPage.tsx

88.46% Statements 46/52
79.31% Branches 23/29
88.88% Functions 16/18
88.46% Lines 46/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                                                                1x 23x 23x 23x             23x 23x   23x     8x 8x       23x               23x   2x 1x   1x       2x 2x 2x 2x 2x                         23x   1x     1x 1x 1x             23x 2x     23x 1x 1x       1x     23x 1x     23x 23x 10x                   13x 4x                 9x 9x                                               1x             1x                   23x                         1x 1x                                                                                                                                       1x 1x                                                                              
import { useState } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import { Plus, Trash2, Loader2, Key, Edit, Users } from 'lucide-react'
import { toast } from 'sonner'
import { ConfirmationDialog } from '@core/components/ConfirmationDialog'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { api } from '@core/lib/api'
import { Button } from '@core/components/ui/button'
import { Input } from '@core/components/ui/input'
import { Label } from '@core/components/ui/label'
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@core/components/ui/table'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@core/components/ui/card'
import {
    Dialog,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
} from '@core/components/ui/dialog'
import { staffSchema, type StaffFormValues } from '../schemas/staff.schema'
 
export const StaffManagementPage = () => {
    const { t } = useTranslation(['common', 'white-labeling'])
    const queryClient = useQueryClient()
    const [isAddOpen, setIsAddOpen] = useState(false)
    interface StaffMember {
        id: string
        name: string
        code: string
        _count?: { events: number }
    }
    const [editingStaff, setEditingStaff] = useState<StaffMember | null>(null)
    const [deleteId, setDeleteId] = useState<string | null>(null)
 
    const { data: staff = [], isLoading } = useQuery({
        queryKey: ['global-staff'],
        queryFn: async () => {
            const res = await api.get('/staff')
            return res.data
        },
    })
 
    const form = useForm<StaffFormValues>({
        resolver: zodResolver(staffSchema),
        defaultValues: {
            name: '',
            code: '',
        },
    })
 
    const createMutation = useMutation({
        mutationFn: async (values: StaffFormValues) => {
            if (editingStaff) {
                await api.put(`/staff/${editingStaff.id}`, values)
            } else {
                await api.post('/staff', values)
            }
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['global-staff'] })
            setIsAddOpen(false)
            setEditingStaff(null)
            form.reset()
            toast.success(
                editingStaff ? t('admin_team.member_updated') : t('admin_team.success_created'),
            )
        },
        onError: (err: unknown) => {
            console.error('Failed to save staff', err)
            const message =
                (err as { response?: { data?: { message?: string } } })?.response?.data?.message ||
                t('donation.error')
            toast.error(message)
        },
    })
 
    const deleteMutation = useMutation({
        mutationFn: async (id: string) => {
            await api.delete(`/staff/${id}`)
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['global-staff'] })
            toast.success(t('admin_team.member_deleted'))
            setDeleteId(null) // Close the confirmation dialog
        },
        onError: () => {
            toast.error(t('admin_team.error_delete'))
        },
    })
 
    const onSubmit = (values: StaffFormValues) => {
        createMutation.mutate(values)
    }
 
    const handleEdit = (member: StaffMember) => {
        setEditingStaff(member)
        form.reset({
            name: member.name,
            code: member.code,
        })
        setIsAddOpen(true)
    }
 
    const handleDelete = (id: string) => {
        setDeleteId(id)
    }
 
    const renderTableContent = () => {
        if (isLoading) {
            return (
                <TableRow>
                    <TableCell colSpan={4} className="h-24 text-center">
                        <Loader2 className="mr-2 h-4 w-4 animate-spin inline" />
                        {t('admin_team.loading_staff')}
                    </TableCell>
                </TableRow>
            )
        }
 
        if (staff.length === 0) {
            return (
                <TableRow>
                    <TableCell colSpan={4} className="h-24 text-center text-muted-foreground">
                        {t('admin_team.no_staff_found')}
                    </TableCell>
                </TableRow>
            )
        }
 
        return staff.map((member: StaffMember) => (
            <TableRow key={member.id}>
                <TableCell>
                    <div className="flex items-center gap-2">
                        <div className="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center">
                            <Users className="h-4 w-4 text-primary" />
                        </div>
                        <span className="font-medium">{member.name}</span>
                    </div>
                </TableCell>
                <TableCell>
                    <div className="flex items-center gap-2 text-muted-foreground">
                        <Key className="h-3 w-3" />
                        <span className="font-mono">{member.code}</span>
                    </div>
                </TableCell>
                <TableCell>
                    <span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-muted text-muted-foreground">
                        {t('admin_team.events_count', {
                            count: member._count?.events || 0,
                        })}
                    </span>
                </TableCell>
                <TableCell className="text-right">
                    <div className="flex justify-end gap-2">
                        <Button variant="ghost" size="sm" onClick={() => handleEdit(member)}>
                            <Edit className="h-4 w-4" />
                        </Button>
                        <Button
                            variant="ghost"
                            size="sm"
                            className="text-destructive hover:text-destructive hover:bg-destructive/10"
                            onClick={() => handleDelete(member.id)}
                        >
                            <Trash2 className="h-4 w-4" />
                        </Button>
                    </div>
                </TableCell>
            </TableRow>
        ))
    }
 
    return (
        <div className="space-y-6">
            <div className="flex flex-col md:flex-row justify-between gap-4 items-start md:items-center">
                <div>
                    <h2 className="text-3xl font-bold tracking-tight">
                        {t('admin_team.manage_title')}
                    </h2>
                    <p className="text-muted-foreground mt-1">{t('admin_team.manage_subtitle')}</p>
                </div>
 
                <Dialog
                    open={isAddOpen}
                    onOpenChange={(open) => {
                        setIsAddOpen(open)
                        Iif (!open) {
                            setEditingStaff(null)
                            form.reset({ name: '', code: '' })
                        }
                    }}
                >
                    <DialogTrigger asChild>
                        <Button>
                            <Plus className="mr-2 h-4 w-4" />
                            {t('admin_team.add_member')}
                        </Button>
                    </DialogTrigger>
                    <DialogContent>
                        <DialogHeader>
                            <DialogTitle>
                                {editingStaff
                                    ? t('admin_team.edit_member')
                                    : t('admin_team.add_member')}
                            </DialogTitle>
                            <DialogDescription>
                                {editingStaff
                                    ? t('admin_team.edit_member_desc')
                                    : t('admin_team.create_member_desc')}
                            </DialogDescription>
                        </DialogHeader>
                        <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4 py-4">
                            <div className="grid gap-2">
                                <Label htmlFor="name">{t('admin_team.member_name')}</Label>
                                <Input
                                    id="name"
                                    {...form.register('name')}
                                    placeholder={t('admin_team.member_placeholder')}
                                />
                                {form.formState.errors.name && (
                                    <p className="text-sm text-destructive">
                                        {form.formState.errors.name.message}
                                    </p>
                                )}
                            </div>
                            <div className="grid gap-2">
                                <Label htmlFor="code">{t('admin_team.pin_code')}</Label>
                                <Input
                                    id="code"
                                    {...form.register('code')}
                                    placeholder={t('admin_team.pin_placeholder')}
                                />
                                {form.formState.errors.code && (
                                    <p className="text-sm text-destructive">
                                        {form.formState.errors.code.message}
                                    </p>
                                )}
                            </div>
                            <DialogFooter>
                                <Button type="submit" disabled={createMutation.isPending}>
                                    {createMutation.isPending && (
                                        <Loader2 className="mr-2 h-4 w-4 animate-spin" />
                                    )}
                                    {editingStaff
                                        ? t('admin_team.save_changes')
                                        : t('admin_team.add_member')}
                                </Button>
                            </DialogFooter>
                        </form>
                    </DialogContent>
                </Dialog>
 
                <ConfirmationDialog
                    open={!!deleteId}
                    onOpenChange={(open) => !open && setDeleteId(null)}
                    onConfirm={() => deleteMutation.mutate(deleteId!)}
                    title={t('admin_team.confirm_delete_title')}
                    description={t('admin_team.confirm_delete_permanent')}
                    confirmText={t('common.delete')}
                    variant="destructive"
                />
            </div>
 
            <Card
                style={{
                    backgroundColor: 'var(--admin-card-bg)',
                    borderColor: 'var(--admin-border-color)',
                }}
            >
                <CardHeader>
                    <CardTitle>{t('admin_team.pool_title')}</CardTitle>
                    <CardDescription>{t('admin_team.pool_description')}</CardDescription>
                </CardHeader>
                <CardContent>
                    <div className="rounded-md border">
                        <Table>
                            <TableHeader>
                                <TableRow>
                                    <TableHead>{t('admin_team.table_member')}</TableHead>
                                    <TableHead>{t('admin_team.table_pin')}</TableHead>
                                    <TableHead>{t('admin_team.table_assignments')}</TableHead>
                                    <TableHead className="text-right">
                                        {t('common.actions')}
                                    </TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>{renderTableContent()}</TableBody>
                        </Table>
                    </div>
                </CardContent>
            </Card>
        </div>
    )
}