naim-firebase-auth-wrapper
Version:
React components and hooks for Firebase Authentication and Firestore with Mantine UI
93 lines • 4.63 kB
JavaScript
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { Paper, Title, Text, Button, Group, Loader, Stack, Alert } from '@mantine/core';
import { IconAlertCircle, IconCheck, IconX } from '@tabler/icons-react';
import { useAuth } from '../hooks/useAuth';
import { getInvitationByToken, acceptInvitation, declineInvitation } from '../services/firestore';
export const AcceptInvitation = ({ token, onAcceptSuccess, onDeclineSuccess, onError }) => {
const { user } = useAuth();
const [invitation, setInvitation] = useState(null);
const [loading, setLoading] = useState(true);
const [processing, setProcessing] = useState(false);
const [error, setError] = useState(null);
const [success, setSuccess] = useState(null);
useEffect(() => {
const loadInvitation = async () => {
try {
const inv = await getInvitationByToken(token);
setInvitation(inv);
if (!inv) {
setError('Invitation not found or has been processed');
}
else if (inv.status !== 'pending') {
setError(`This invitation has already been ${inv.status}`);
}
else if (new Date(inv.expiresAt) < new Date()) {
setError('This invitation has expired');
}
}
catch (error) {
console.error('Error loading invitation:', error);
setError('Failed to load invitation details');
onError?.(error);
}
finally {
setLoading(false);
}
};
loadInvitation();
}, [token, onError]);
const handleAccept = async () => {
if (!user || !invitation)
return;
setProcessing(true);
setError(null);
try {
await acceptInvitation(token, user.uid);
setSuccess(`You have joined ${invitation.orgName} as a ${invitation.role}`);
onAcceptSuccess?.();
}
catch (error) {
console.error('Error accepting invitation:', error);
setError(error.message || 'Failed to accept invitation');
onError?.(error);
}
finally {
setProcessing(false);
}
};
const handleDecline = async () => {
if (!invitation)
return;
setProcessing(true);
setError(null);
try {
await declineInvitation(token);
setSuccess('You have declined the invitation');
onDeclineSuccess?.();
}
catch (error) {
console.error('Error declining invitation:', error);
setError(error.message || 'Failed to decline invitation');
onError?.(error);
}
finally {
setProcessing(false);
}
};
if (loading) {
return _jsx(Loader, {});
}
if (error) {
return (_jsx(Paper, { radius: "md", p: "xl", withBorder: true, children: _jsx(Alert, { icon: _jsx(IconAlertCircle, { size: 16 }), title: "Error", color: "red", children: error }) }));
}
if (success) {
return (_jsx(Paper, { radius: "md", p: "xl", withBorder: true, children: _jsx(Alert, { icon: _jsx(IconCheck, { size: 16 }), title: "Success", color: "green", children: success }) }));
}
if (!invitation) {
return (_jsx(Paper, { radius: "md", p: "xl", withBorder: true, children: _jsx(Alert, { icon: _jsx(IconAlertCircle, { size: 16 }), title: "Error", color: "red", children: "Invitation not found" }) }));
}
return (_jsx(Paper, { radius: "md", p: "xl", withBorder: true, children: _jsxs(Stack, { gap: "md", children: [_jsx(Title, { order: 2, children: "Organization Invitation" }), _jsxs(Text, { children: ["You have been invited to join ", _jsx("strong", { children: invitation.orgName }), " as a ", _jsx("strong", { children: invitation.role }), "."] }), _jsxs(Text, { size: "sm", children: ["Invited by: ", invitation.inviterName] }), _jsxs(Text, { size: "sm", children: ["Expires: ", new Date(invitation.expiresAt).toLocaleDateString()] }), _jsxs(Group, { justify: "flex-end", mt: "md", children: [_jsx(Button, { variant: "outline", color: "red", leftSection: _jsx(IconX, { size: 16 }), onClick: handleDecline, loading: processing, children: "Decline" }), _jsx(Button, { leftSection: _jsx(IconCheck, { size: 16 }), onClick: handleAccept, loading: processing, children: "Accept Invitation" })] })] }) }));
};
//# sourceMappingURL=AcceptInvitation.js.map