All files / src/components/organizer/EventPage RegistrationActionsMenu.tsx

49.01% Statements 25/51
84.61% Branches 11/13
18.18% Functions 4/22
48% Lines 24/50

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                                                                                                        2x       75x 75x 75x 75x 75x 75x 75x 75x     75x                 75x               75x                 75x         75x         75x       75x         75x   75x   75x               2x 2x                                                                                     1x 1x 1x                                                                                                                                                                                                                                    
/**
 * Registration Actions Menu Component
 *
 * Provides action buttons for managing event registrations.
 * - Cancel: Sets registration status to CANCELLED
 * - Delete: Permanently removes the registration (with confirmation dialog)
 */
 
import React, { useState } from 'react';
import {
  Box,
  IconButton,
  Tooltip,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogContentText,
  DialogActions,
  Button,
  Snackbar,
  Alert,
  Menu,
  MenuItem,
  ListItemIcon,
  ListItemText,
} from '@mui/material';
import {
  Cancel as CancelIcon,
  Delete as DeleteIcon,
  Email as EmailIcon,
  MoreVert as MoreVertIcon,
} from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import {
  cancelRegistration,
  deleteRegistration,
  resendConfirmationEmail,
} from '../../../services/api/eventRegistrationService';
import type { EventParticipant } from '../../../types/eventParticipant.types';
import { useBreakpoints } from '@/hooks/useBreakpoints';
 
interface RegistrationActionsMenuProps {
  participant: EventParticipant;
  /**
   * 'inline' (default, desktop table): a row of action icon buttons.
   * 'overflow' (mobile cards, Story 14.G.4): a single ⋯ button → a Menu of the
   * same actions (FR47).
   */
  variant?: 'inline' | 'overflow';
}
 
const RegistrationActionsMenu: React.FC<RegistrationActionsMenuProps> = ({
  participant,
  variant = 'inline',
}) => {
  const { t } = useTranslation('events');
  const { isMobile } = useBreakpoints();
  const queryClient = useQueryClient();
  const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
  const [resendSuccess, setResendSuccess] = useState(false);
  const [resendError, setResendError] = useState<string | null>(null);
  const [menuAnchor, setMenuAnchor] = useState<null | HTMLElement>(null);
  const closeMenu = () => setMenuAnchor(null);
 
  // Cancel registration mutation
  const cancelMutation = useMutation({
    mutationFn: () => cancelRegistration(participant.eventCode, participant.registrationCode),
    onSuccess: () => {
      // Invalidate queries to refresh the list (use hyphenated key to match hook)
      queryClient.invalidateQueries({ queryKey: ['event-registrations', participant.eventCode] });
    },
  });
 
  // Resend confirmation email mutation
  const resendMutation = useMutation({
    mutationFn: () => resendConfirmationEmail(participant.eventCode, participant.registrationCode),
    onSuccess: () => setResendSuccess(true),
    onError: (err: Error) =>
      setResendError(err.message || t('eventPage.participantTable.actions.resendError')),
  });
 
  // Delete registration mutation
  const deleteMutation = useMutation({
    mutationFn: () => deleteRegistration(participant.eventCode, participant.registrationCode),
    onSuccess: () => {
      // Invalidate queries to refresh the list (use hyphenated key to match hook)
      queryClient.invalidateQueries({ queryKey: ['event-registrations', participant.eventCode] });
      setDeleteDialogOpen(false);
    },
  });
 
  const handleCancelClick = (e: React.MouseEvent) => {
    e.stopPropagation(); // Prevent row click event
    cancelMutation.mutate();
  };
 
  const handleDeleteClick = (e: React.MouseEvent) => {
    e.stopPropagation(); // Prevent row click event
    setDeleteDialogOpen(true);
  };
 
  const handleDeleteConfirm = () => {
    deleteMutation.mutate();
  };
 
  const handleDeleteCancel = () => {
    setDeleteDialogOpen(false);
  };
 
  // Don't show cancel button if already cancelled
  const showCancelButton = participant.status !== 'CANCELLED';
  // Only show resend for pending (REGISTERED) registrations
  const showResendButton = participant.status === 'REGISTERED';
 
  return (
    <>
      {variant === 'overflow' ? (
        <>
          <Tooltip title={t('eventPage.participantTable.actions.menu', 'Actions')}>
            <IconButton
              size="small"
              onClick={(e) => {
                e.stopPropagation();
                setMenuAnchor(e.currentTarget);
              }}
              data-testid="registration-actions-overflow"
              aria-label={t('eventPage.participantTable.actions.menu', 'Actions')}
            >
              <MoreVertIcon fontSize="small" />
            </IconButton>
          </Tooltip>
          <Menu anchorEl={menuAnchor} open={!!menuAnchor} onClose={closeMenu}>
            {showResendButton && (
              <MenuItem
                onClick={(e) => {
                  e.stopPropagation();
                  closeMenu();
                  resendMutation.mutate();
                }}
                disabled={resendMutation.isPending}
              >
                <ListItemIcon>
                  <EmailIcon fontSize="small" />
                </ListItemIcon>
                <ListItemText>
                  {t('eventPage.participantTable.actions.resendConfirmation')}
                </ListItemText>
              </MenuItem>
            )}
            {showCancelButton && (
              <MenuItem
                onClick={(e) => {
                  e.stopPropagation();
                  closeMenu();
                  cancelMutation.mutate();
                }}
                disabled={cancelMutation.isPending}
              >
                <ListItemIcon>
                  <CancelIcon fontSize="small" />
                </ListItemIcon>
                <ListItemText>{t('eventPage.participantTable.actions.cancel')}</ListItemText>
              </MenuItem>
            )}
            <MenuItem
              onClick={(e) => {
                e.stopPropagation();
                closeMenu();
                setDeleteDialogOpen(true);
              }}
              disabled={deleteMutation.isPending}
            >
              <ListItemIcon>
                <DeleteIcon fontSize="small" />
              </ListItemIcon>
              <ListItemText>{t('eventPage.participantTable.actions.delete')}</ListItemText>
            </MenuItem>
          </Menu>
        </>
      ) : (
        <Box sx={{ display: 'flex', gap: 0.5 }}>
          {showResendButton && (
            <Tooltip title={t('eventPage.participantTable.actions.resendConfirmation')}>
              <IconButton
                size="small"
                onClick={(e) => {
                  e.stopPropagation();
                  resendMutation.mutate();
                }}
                disabled={resendMutation.isPending}
                sx={{ color: 'info.main' }}
              >
                <EmailIcon fontSize="small" />
              </IconButton>
            </Tooltip>
          )}
          {showCancelButton && (
            <Tooltip title={t('eventPage.participantTable.actions.cancel')}>
              <IconButton
                size="small"
                onClick={handleCancelClick}
                disabled={cancelMutation.isPending}
                sx={{ color: 'warning.main' }}
              >
                <CancelIcon fontSize="small" />
              </IconButton>
            </Tooltip>
          )}
 
          <Tooltip title={t('eventPage.participantTable.actions.delete')}>
            <IconButton
              size="small"
              onClick={handleDeleteClick}
              disabled={deleteMutation.isPending}
              sx={{ color: 'error.main' }}
            >
              <DeleteIcon fontSize="small" />
            </IconButton>
          </Tooltip>
        </Box>
      )}
 
      {/* Resend success/error feedback */}
      <Snackbar
        open={resendSuccess}
        autoHideDuration={4000}
        onClose={() => setResendSuccess(false)}
        anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
      >
        <Alert severity="success" onClose={() => setResendSuccess(false)}>
          {t('eventPage.participantTable.actions.resendSuccess')}
        </Alert>
      </Snackbar>
      <Snackbar
        open={!!resendError}
        autoHideDuration={6000}
        onClose={() => setResendError(null)}
        anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
      >
        <Alert severity="error" onClose={() => setResendError(null)}>
          {resendError}
        </Alert>
      </Snackbar>
 
      {/* Delete Confirmation Dialog */}
      <Dialog
        open={deleteDialogOpen}
        onClose={handleDeleteCancel}
        fullScreen={isMobile}
        aria-labelledby="delete-dialog-title"
        aria-describedby="delete-dialog-description"
      >
        <DialogTitle id="delete-dialog-title">
          {t('eventPage.participantTable.deleteDialog.title')}
        </DialogTitle>
        <DialogContent>
          <DialogContentText id="delete-dialog-description">
            {t('eventPage.participantTable.deleteDialog.message', {
              name: `${participant.firstName} ${participant.lastName}`,
            })}
          </DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleDeleteCancel} disabled={deleteMutation.isPending}>
            {t('common:actions.cancel')}
          </Button>
          <Button
            onClick={handleDeleteConfirm}
            color="error"
            variant="contained"
            disabled={deleteMutation.isPending}
            autoFocus
          >
            {t('common:actions.delete')}
          </Button>
        </DialogActions>
      </Dialog>
    </>
  );
};
 
export default RegistrationActionsMenu;