All files / src/components/organizer/UserManagement UserCard.tsx

80% Statements 24/30
85% Branches 17/20
66.66% Functions 8/12
80% Lines 24/30

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                                                                                        12x 21x 21x   21x 1x     21x 3x 3x     21x       21x 1x 1x 1x     21x 2x 2x 2x       21x 42x   21x   21x               21x                                                                   1x                                                                                                                                                                                                                                   42x                                  
/**
 * UserCard Component
 *
 * Individual user card display with:
 * - Profile picture with fallback to initials
 * - User name and email
 * - Role badges
 * - Company affiliation
 * - Active/Inactive status
 *
 * Story: 2.5.2 - User Management Frontend
 */
 
import React, { useState } from 'react';
import {
  Card,
  CardContent,
  CardActionArea,
  Typography,
  Box,
  Chip,
  Avatar,
  Tooltip,
  IconButton,
  Menu,
  MenuItem,
} from '@mui/material';
import {
  Business as BusinessIcon,
  Email as EmailIcon,
  Cloud as CloudIcon,
  MoreVert as MoreVertIcon,
} from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import type { User, Role } from '@/types/user.types';
import { ROLE_ICONS } from '@/types/user.types';
import CompanyCell from './CompanyCell';
 
export interface UserCardProps {
  user: User;
  onClick: (user: User) => void;
  onAction?: (action: string, user: User) => void;
}
 
export const UserCard: React.FC<UserCardProps> = ({ user, onClick, onAction }) => {
  const { t } = useTranslation('userManagement');
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const handleClick = () => {
    onClick(user);
  };
 
  const handleMenuOpen = (event: React.MouseEvent<HTMLElement>) => {
    event.stopPropagation();
    setAnchorEl(event.currentTarget);
  };
 
  const handleMenuClose = () => {
    setAnchorEl(null);
  };
 
  const handleAction = (event: React.MouseEvent<HTMLElement>, action: string) => {
    event.stopPropagation();
    onAction?.(action, user);
    setAnchorEl(null);
  };
 
  const handleKeyDown = (event: React.KeyboardEvent) => {
    Eif (event.key === 'Enter' || event.key === ' ') {
      event.preventDefault();
      onClick(user);
    }
  };
 
  const getRoleBadgeColor = (role: Role): 'primary' | 'secondary' | 'success' | 'default' => {
    switch (role) {
      case 'ORGANIZER':
        return 'primary';
      case 'SPEAKER':
        return 'secondary';
      case 'PARTNER':
        return 'success';
      default:
        return 'default';
    }
  };
 
  return (
    <Card
      data-testid={`user-card-${user.id}`}
      sx={{
        height: '100%',
        display: 'flex',
        flexDirection: 'column',
        position: 'relative',
      }}
    >
      {onAction && (
        <>
          <IconButton
            size="small"
            onClick={handleMenuOpen}
            aria-label={t('actions.openMenu')}
            data-testid={`user-card-menu-${user.id}`}
            sx={{ position: 'absolute', top: 4, right: 4, zIndex: 1 }}
          >
            <MoreVertIcon />
          </IconButton>
          <Menu
            anchorEl={anchorEl}
            open={Boolean(anchorEl)}
            onClose={handleMenuClose}
            onClick={(e) => e.stopPropagation()}
          >
            <MenuItem
              onClick={(e) => handleAction(e, 'view')}
              data-testid={`user-card-action-view-${user.id}`}
            >
              {t('actions.view')}
            </MenuItem>
            <MenuItem
              onClick={(e) => handleAction(e, 'editRoles')}
              data-testid={`user-card-action-edit-roles-${user.id}`}
            >
              {t('actions.editRoles')}
            </MenuItem>
            <MenuItem
              onClick={(e) => handleAction(e, 'delete')}
              data-testid={`user-card-action-delete-${user.id}`}
            >
              {t('common:actions.delete')}
            </MenuItem>
          </Menu>
        </>
      )}
      <CardActionArea
        onClick={handleClick}
        onKeyDown={handleKeyDown}
        role="button"
        aria-label={t('table.viewUser', { name: `${user.firstName} ${user.lastName}` })}
        tabIndex={0}
        sx={{
          height: '100%',
        }}
      >
        <CardContent sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
          {/* Top Section: Avatar + Info */}
          <Box sx={{ display: 'flex', gap: 2, mb: 2 }}>
            {/* Profile Picture */}
            <Avatar
              src={user.profilePictureUrl || undefined}
              sx={{
                width: 80,
                height: 80,
                bgcolor: 'primary.main',
                fontSize: '2rem',
                flexShrink: 0,
              }}
              imgProps={{ loading: 'lazy' }}
              alt={`${user.firstName} ${user.lastName}`}
            >
              {user.firstName[0]}
              {user.lastName[0]}
            </Avatar>
 
            {/* Info Column */}
            <Box
              sx={{ flexGrow: 1, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 0.5 }}
            >
              {/* Status Badge */}
              <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                <Chip
                  label={user.active ? t('status.active') : t('status.inactive')}
                  size="small"
                  color={user.active ? 'success' : 'default'}
                  sx={{ height: 20, fontSize: '0.7rem' }}
                />
                {user.hasCognitoAccount && (
                  <Tooltip title={t('cognito.tooltip')}>
                    <CloudIcon
                      sx={{ fontSize: 14, color: 'info.main' }}
                      aria-label={t('cognito.linked')}
                    />
                  </Tooltip>
                )}
              </Box>
 
              {/* User Name */}
              <Typography
                variant="h6"
                component="h3"
                sx={{
                  fontSize: '1rem',
                  fontWeight: 600,
                  overflow: 'hidden',
                  textOverflow: 'ellipsis',
                  whiteSpace: 'nowrap',
                  lineHeight: 1.2,
                }}
              >
                {user.firstName} {user.lastName}
              </Typography>
 
              {/* Email */}
              <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                <EmailIcon sx={{ fontSize: 14, color: 'text.secondary', flexShrink: 0 }} />
                <Typography
                  variant="body2"
                  color="text.secondary"
                  sx={{
                    fontSize: '0.8rem',
                    overflow: 'hidden',
                    textOverflow: 'ellipsis',
                    whiteSpace: 'nowrap',
                  }}
                >
                  {user.email}
                </Typography>
              </Box>
 
              {/* Company */}
              {user.companyId && (
                <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                  <BusinessIcon sx={{ fontSize: 14, color: 'text.secondary', flexShrink: 0 }} />
                  <Box sx={{ flexGrow: 1, minWidth: 0 }}>
                    <CompanyCell companyId={user.companyId} />
                  </Box>
                </Box>
              )}
            </Box>
          </Box>
 
          {/* Roles Section - Bottom */}
          <Box sx={{ display: 'flex', gap: 0.5, flexWrap: 'wrap', mt: 'auto' }}>
            {user.roles.map((role) => (
              <Chip
                key={role}
                label={t(`common:role.${role.toLowerCase()}`)}
                size="small"
                color={getRoleBadgeColor(role as Role)}
                icon={<span>{ROLE_ICONS[role as Role]}</span>}
                sx={{ height: 24, fontSize: '0.75rem' }}
              />
            ))}
          </Box>
        </CardContent>
      </CardActionArea>
    </Card>
  );
};
 
export default UserCard;