All files / src/components/user/ProfileHeader ProfileHeader.tsx

93.33% Statements 14/15
83.33% Branches 10/12
100% Functions 5/5
93.33% Lines 14/15

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                                              1x             1x 12x   12x 1x 1x 1x       12x 12x 12x           12x 1x     12x                                                                                                                                                                                                                                                 24x                                            
/**
 * ProfileHeader Component
 * Story 2.6: User Account Management Frontend
 * Displays profile photo, name, email, company, roles, member since
 */
 
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Box, Avatar, Typography, Chip, IconButton, Stack, Badge, Tooltip } from '@mui/material';
import {
  PhotoCamera as PhotoCameraIcon,
  Delete as DeleteIcon,
  CheckCircle as CheckCircleIcon,
} from '@mui/icons-material';
import { format } from 'date-fns';
import type { User } from '@/types/userAccount.types';
 
interface ProfileHeaderProps {
  user: User;
  onPhotoUpload: (file: File) => void;
  onPhotoRemove: () => void;
}
 
const roleIcons: Record<string, string> = {
  ORGANIZER: '🎯',
  SPEAKER: '🎤',
  PARTNER: '👔',
  ATTENDEE: '👤',
};
 
const ProfileHeader: React.FC<ProfileHeaderProps> = ({ user, onPhotoUpload, onPhotoRemove }) => {
  const { t } = useTranslation('userManagement');
 
  const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    Eif (file) {
      onPhotoUpload(file);
    }
  };
 
  const formatMemberSince = (dateString: string): string => {
    try {
      return format(new Date(dateString), 'MMMM yyyy');
    } catch {
      return dateString;
    }
  };
 
  const getInitials = (firstName: string, lastName: string): string => {
    return `${firstName.charAt(0)}${lastName.charAt(0)}`.toUpperCase();
  };
 
  return (
    <Box sx={{ mb: 4 }}>
      <Stack direction={{ xs: 'column', sm: 'row' }} spacing={3} alignItems="center">
        {/* Profile Photo */}
        <Box sx={{ position: 'relative' }}>
          <Badge
            overlap="circular"
            anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
            badgeContent={
              <>
                <input
                  accept="image/jpeg,image/png"
                  style={{ display: 'none' }}
                  id="profile-photo-upload"
                  type="file"
                  onChange={handleFileSelect}
                  data-testid="profile-photo-input"
                />
                <label htmlFor="profile-photo-upload">
                  <Tooltip title={t('profileHeader.uploadTooltip')}>
                    <IconButton
                      aria-label={t('modal.editUser.uploadPhotoAria')}
                      component="span"
                      size="small"
                      sx={{
                        bgcolor: 'primary.main',
                        color: 'white',
                        '&:hover': { bgcolor: 'primary.dark' },
                      }}
                      data-testid="upload-photo-button"
                    >
                      <PhotoCameraIcon fontSize="small" />
                    </IconButton>
                  </Tooltip>
                </label>
              </>
            }
          >
            <Avatar
              src={user.profilePictureUrl}
              alt={`${user.firstName} ${user.lastName}`}
              sx={{ width: 120, height: 120, fontSize: '2rem' }}
              data-testid="profile-photo"
            >
              {!user.profilePictureUrl && getInitials(user.firstName, user.lastName)}
            </Avatar>
          </Badge>
 
          {user.profilePictureUrl && (
            <Tooltip title={t('profileHeader.removeTooltip')}>
              <IconButton
                aria-label={t('modal.editUser.removePhotoAria')}
                size="small"
                onClick={onPhotoRemove}
                sx={{
                  position: 'absolute',
                  top: -8,
                  right: -8,
                  bgcolor: 'error.main',
                  color: 'white',
                  '&:hover': { bgcolor: 'error.dark' },
                }}
                data-testid="remove-photo-button"
              >
                <DeleteIcon fontSize="small" />
              </IconButton>
            </Tooltip>
          )}
        </Box>
 
        {/* User Info */}
        <Box sx={{ flex: 1, textAlign: { xs: 'center', sm: 'left' } }}>
          {/* Name */}
          <Typography variant="h4" component="h1" gutterBottom data-testid="user-name">
            {user.firstName} {user.lastName}
          </Typography>
 
          {/* Email with Verified Badge */}
          <Box
            sx={{
              display: 'flex',
              alignItems: 'center',
              gap: 1,
              mb: 1,
              justifyContent: { xs: 'center', sm: 'flex-start' },
            }}
          >
            <Typography variant="body1" color="text.secondary" data-testid="user-email">
              {user.email}
            </Typography>
            {user.emailVerified && (
              <Chip
                icon={<CheckCircleIcon />}
                label={t('profileHeader.verified')}
                size="small"
                color="success"
                variant="outlined"
                data-testid="email-verified-badge"
              />
            )}
          </Box>
 
          {/* Company */}
          {user.company && (
            <Typography
              variant="body2"
              color="text.secondary"
              sx={{ mb: 1 }}
              data-testid="user-company"
            >
              {user.company.name}
            </Typography>
          )}
 
          {/* Role Badges */}
          <Stack
            direction="row"
            spacing={1}
            sx={{ mb: 1, justifyContent: { xs: 'center', sm: 'flex-start' }, flexWrap: 'wrap' }}
          >
            {user.roles.map((role) => (
              <Chip
                key={role}
                label={`${roleIcons[role] || ''} ${role.charAt(0) + role.slice(1).toLowerCase()}`}
                size="small"
                variant="filled"
                color="primary"
                data-testid="role-badge"
              />
            ))}
          </Stack>
 
          {/* Member Since */}
          <Typography variant="caption" color="text.secondary" data-testid="member-since">
            {t('profileHeader.memberSince', { date: formatMemberSince(user.memberSince) })}
          </Typography>
        </Box>
      </Stack>
    </Box>
  );
};
 
export default ProfileHeader;