All files / pages InvitationsPage.tsx

65.82% Statements 52/79
81.08% Branches 30/37
63.63% Functions 14/22
69.33% Lines 52/75

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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358                                                                                  65x 65x 65x 65x 65x 65x 65x   65x 65x 65x 65x   65x 21x 21x 21x 21x 21x 21x 21x           21x     21x     65x 22x 22x   22x 22x 22x 22x 18x 18x   3x   21x       65x 21x     65x                                 65x                                   65x                                     65x 47x   16x   16x       15x       65x         47x                 47x         47x             47x           47x                                                                       65x                               1x                                                                                                       23x                                                                                                 47x                      
/**
 * Invitations management page
 * Manage user invitations
 */
 
import { useCallback, useEffect, useState } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import {
    RefreshCw,
    Mail,
    Copy,
    Trash2,
    CheckCircle2,
    AlertTriangle,
    XCircle,
    Clock,
    Send
} from 'lucide-react';
import {
    getInvitations,
    resendInvitation,
    cancelInvitation,
    type Invitation
} from '@/services/invitations.service';
import { getAccounts, type AccountListItem } from '@/services/accounts.service';
import { handleApiError } from '@/lib/api';
import { DataTable, type Column } from '@/components/common/DataTable';
import { useToast } from "@/hooks/use-toast";
import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from '@/components/ui/select';
import { Label } from '@/components/ui/label';
import { CreateInvitationDialog } from '@/components/invitations/CreateInvitationDialog';
 
export default function InvitationsPage() {
    const { toast } = useToast();
    const [data, setData] = useState<Invitation[] | null>(null);
    const [total, setTotal] = useState(0);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState<string | null>(null);
    const [statusFilter, setStatusFilter] = useState<string>('all');
    const [isCreateOpen, setIsCreateOpen] = useState(false);
 
    const [accounts, setAccounts] = useState<AccountListItem[]>([]);
    const [loadingAccounts, setLoadingAccounts] = useState(false);
    const [showAccountFilter, setShowAccountFilter] = useState(false);
    const [accountFilter, setAccountFilter] = useState<string>('all');
 
    useEffect(() => {
        const loadAccounts = async () => {
            try {
                setLoadingAccounts(true);
                const response = await getAccounts({ page_size: 100 });
                Eif (response.items && response.items.length > 0) {
                    setAccounts(response.items);
                    setShowAccountFilter(true);
                }
            } catch (error) {
                // Ignore error - likely not superadmin
                console.debug("Failed to load accounts for filter", error);
            } finally {
                setLoadingAccounts(false);
            }
        };
        loadAccounts();
    }, []);
 
    const fetchInvitations = useCallback(async () => {
        setLoading(true);
        setError(null);
 
        try {
            const filter = statusFilter !== 'all' ? statusFilter : undefined;
            const acctFilter = accountFilter !== 'all' ? accountFilter : undefined;
            const response = await getInvitations(filter, acctFilter);
            setData(response.invitations);
            setTotal(response.total);
        } catch (err) {
            setError(handleApiError(err));
        } finally {
            setLoading(false);
        }
    }, [statusFilter, accountFilter]);
 
    useEffect(() => {
        fetchInvitations();
    }, [fetchInvitations]);
 
    const handleResend = async (id: string) => {
        try {
            await resendInvitation(id);
            toast({
                title: "Success",
                description: "Invitation email resent successfully",
            });
            await fetchInvitations();
        } catch (err) {
            toast({
                variant: "destructive",
                title: "Error",
                description: handleApiError(err),
            });
        }
    };
 
    const handleCopyLink = async (token: string) => {
        try {
            // Construct URL based on current origin
            const url = `${window.location.origin}/accept-invitation?token=${token}`;
            await navigator.clipboard.writeText(url);
            toast({
                title: "Copied",
                description: "Invitation link copied to clipboard",
            });
        } catch (err) {
            toast({
                variant: "destructive",
                title: "Error",
                description: "Failed to copy link",
            });
        }
    };
 
    const handleCancel = async (id: string) => {
        if (!confirm('Are you sure you want to cancel this invitation?')) return;
 
        try {
            await cancelInvitation(id);
            toast({
                title: "Success",
                description: "Invitation cancelled successfully",
            });
            await fetchInvitations();
        } catch (err) {
            toast({
                variant: "destructive",
                title: "Error",
                description: handleApiError(err),
            });
        }
    };
 
    const getStatusBadge = (status: string) => {
        switch (status) {
            case 'accepted':
                return <Badge variant="outline" className="text-green-600 border-green-200 bg-green-50"><CheckCircle2 className="w-3 h-3 mr-1" /> Accepted</Badge>;
            case 'expired':
                return <Badge variant="outline" className="text-red-600 border-red-200 bg-red-50"><XCircle className="w-3 h-3 mr-1" /> Expired</Badge>;
            case 'cancelled':
                return <Badge variant="outline" className="text-gray-600 border-gray-200 bg-gray-50"><XCircle className="w-3 h-3 mr-1" /> Cancelled</Badge>;
            default:
                return <Badge variant="outline" className="text-blue-600 border-blue-200 bg-blue-50"><Clock className="w-3 h-3 mr-1" /> Pending</Badge>;
        }
    };
 
    const columns: Column<Invitation>[] = [
        {
            header: 'Account',
            accessorKey: 'account_code',
            className: 'font-medium w-24',
            render: (inv) => <span className="text-sm font-mono text-muted-foreground">{inv.account_code || "Unknown"}</span>
        },
        {
            header: 'Email',
            accessorKey: 'email',
            className: 'font-medium'
        },
        {
            header: 'Status',
            render: (inv) => getStatusBadge(inv.status)
        },
        {
            header: 'Email Sent',
            render: (inv) => (
                inv.email_sent ?
                    <div className="flex items-center text-green-600 text-sm"><CheckCircle2 className="w-4 h-4 mr-1" /> Sent</div> :
                    <div className="flex items-center text-amber-600 text-sm"><AlertTriangle className="w-4 h-4 mr-1" /> Failed</div>
            )
        },
        {
            header: 'Expires',
            render: (inv) => <span className="text-sm text-muted-foreground">{new Date(inv.expires_at).toLocaleDateString()}</span>
        },
        {
            header: 'Actions',
            className: 'text-right',
            render: (inv) => (
                <div className="flex justify-end gap-2">
                    {inv.status === 'pending' && (
                        <>
                            <Button
                                variant="ghost"
                                size="icon"
                                onClick={(e) => { e.stopPropagation(); handleResend(inv.id); }}
                                title="Resend Email"
                                className="text-blue-600 hover:text-blue-700"
                            >
                                <Send className="h-4 w-4" />
                            </Button>
                            <Button
                                variant="ghost"
                                size="icon"
                                onClick={(e) => { e.stopPropagation(); handleCopyLink(inv.token); }}
                                title="Copy Link"
                            >
                                <Copy className="h-4 w-4" />
                            </Button>
                            <Button
                                variant="ghost"
                                size="icon"
                                onClick={(e) => { e.stopPropagation(); handleCancel(inv.id); }}
                                title="Cancel Invitation"
                                className="text-destructive hover:text-destructive"
                            >
                                <Trash2 className="h-4 w-4" />
                            </Button>
                        </>
                    )}
                </div>
            )
        }
    ];
 
    return (
        <div className="space-y-6">
            <CreateInvitationDialog
                open={isCreateOpen}
                onOpenChange={setIsCreateOpen}
                onSuccess={() => {
                    fetchInvitations();
                }}
            />
 
            {/* Header */}
            <div className="flex items-center justify-between">
                <div>
                    <h1 className="text-3xl font-bold">Invitations</h1>
                    <p className="text-muted-foreground mt-2">Manage team invitations</p>
                </div>
                <Button onClick={() => setIsCreateOpen(true)}>
                    <Mail className="mr-2 h-4 w-4" />
                    Invite User
                </Button>
            </div>
 
            <Card>
                <CardHeader>
                    <CardTitle className="flex items-center gap-2">
                        <Mail className="h-5 w-5 text-primary" />
                        Invitation Management
                    </CardTitle>
                    <CardDescription>
                        View and manage pending invitations
                    </CardDescription>
                </CardHeader>
                <CardContent className="space-y-4">
 
                    {/* Filters */}
                    <div className="flex flex-wrap items-end gap-4">
                        <div className="space-y-2">
                            <Label htmlFor="status">Status</Label>
                            <Select
                                value={statusFilter}
                                onValueChange={setStatusFilter}
                            >
                                <SelectTrigger id="status" className="w-32">
                                    <SelectValue placeholder="All" />
                                </SelectTrigger>
                                <SelectContent>
                                    <SelectItem value="all">All</SelectItem>
                                    <SelectItem value="pending">Pending</SelectItem>
                                    <SelectItem value="accepted">Accepted</SelectItem>
                                    <SelectItem value="expired">Expired</SelectItem>
                                </SelectContent>
                            </Select>
                        </div>
 
                        {showAccountFilter && (
                            <div className="space-y-2">
                                <Label htmlFor="account">Account</Label>
                                <Select
                                    value={accountFilter}
                                    onValueChange={setAccountFilter}
                                    disabled={loadingAccounts}
                                >
                                    <SelectTrigger id="account" className="w-48">
                                        <SelectValue placeholder="All Accounts" />
                                    </SelectTrigger>
                                    <SelectContent>
                                        <SelectItem value="all">All Accounts</SelectItem>
                                        {accounts.map((account) => (
                                            <SelectItem key={account.id} value={account.id}>
                                                {account.name} ({account.account_code})
                                            </SelectItem>
                                        ))}
                                    </SelectContent>
                                </Select>
                            </div>
                        )}
 
                        <div className="ml-auto">
                            <Button variant="outline" size="icon" onClick={fetchInvitations} disabled={loading}>
                                <RefreshCw className={`h-4 w-4 ${loading ? 'animate-spin' : ''}`} />
                            </Button>
                        </div>
                    </div>
 
                    {/* Error State */}
                    {error && (
                        <div className="bg-destructive/10 border border-destructive/20 rounded-lg p-4">
                            <p className="text-destructive font-medium">Failed to load invitations</p>
                            <p className="text-sm text-muted-foreground mt-1">{error}</p>
                            <Button onClick={fetchInvitations} className="mt-4" size="sm">
                                Try Again
                            </Button>
                        </div>
                    )}
 
                    {/* Loading State */}
                    {loading && !data && (
                        <div className="flex items-center justify-center py-12">
                            <RefreshCw className="h-8 w-8 animate-spin text-muted-foreground" />
                        </div>
                    )}
 
                    {/* Table */}
                    {!loading && data && data.length === 0 && (
                        <div className="text-center py-12">
                            <Mail className="h-12 w-12 mx-auto mb-4 text-muted-foreground opacity-50" />
                            <h3 className="text-lg font-medium mb-2">No invitations found</h3>
                            <Button variant="outline" onClick={() => setIsCreateOpen(true)}>
                                Invite your first user
                            </Button>
                        </div>
                    )}
 
                    {!loading && data && data.length > 0 && (
                        <DataTable
                            data={data}
                            columns={columns}
                            keyExtractor={(inv) => inv.id}
                            totalItems={total}
                            pagination={undefined} // Pagination not implemented in backend list yet (PRD F5.3 didn't specify, but list endpoint returns all)
                        />
                    )}
 
                </CardContent>
            </Card>
        </div>
    );
}