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 | 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 11x 11x 1x 11x 11x 1x 10x 2x 1x 8x 11x 11x | /**
* UserList Component (GREEN Phase)
*
* Main container component for User Management screen.
* Story 2.5.2: User Management Frontend - Task 5b (GREEN Phase)
*
* Features:
* - User list table with sorting
* - Search and filters
* - Pagination
* - User creation modal
* - User detail modal
* - Role management
* - User deletion (GDPR compliant)
* - Batch import speakers from legacy JSON
*/
import React, { useState } from 'react';
import {
Box,
Typography,
Button,
Alert,
Container,
ToggleButtonGroup,
ToggleButton,
Stack,
useTheme,
useMediaQuery,
} from '@mui/material';
import { BATbernLoader } from '@components/shared/BATbernLoader';
import Grid from '@mui/material/Grid';
import { Add as AddIcon, ViewModule as GridIcon, ViewList as ListIcon } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { useUserManagementStore } from '@/stores/userManagementStore';
import { useUserList } from '@/hooks/useUserManagement';
import UserTable from './UserTable';
import UserCard from './UserCard';
import UserFilters from './UserFilters';
import UserCreateEditModal from './UserCreateEditModal';
import RoleManagerModal from './RoleManagerModal';
import DeleteUserDialog from './DeleteUserDialog';
import UserPagination from './UserPagination';
import type { User } from '@/types/user.types';
const UserList: React.FC = () => {
const { t } = useTranslation('userManagement');
const navigate = useNavigate();
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
const { filters, pagination, sortBy, sortDir, setPage, setLimit, setSort } =
useUserManagementStore();
const [viewMode, setViewMode] = useState<'grid' | 'list'>('list');
const [createModalOpen, setCreateModalOpen] = useState(false);
const [editUser, setEditUser] = useState<User | null>(null);
const [roleManagerUser, setRoleManagerUser] = useState<User | null>(null);
const [deleteDialogUser, setDeleteDialogUser] = useState<User | null>(null);
// Fetch user list with React Query
const { data, isLoading, isError, refetch } = useUserList({
filters,
pagination,
sortBy,
sortDir,
});
const handleRowClick = (user: User) => {
navigate(`/organizer/users/${user.id}`);
};
const handleUserAction = (action: string, user: User) => {
if (action === 'view') {
handleRowClick(user);
} else if (action === 'editRoles') {
setRoleManagerUser(user);
} else if (action === 'delete') {
setDeleteDialogUser(user);
}
};
const handleOpenCreateModal = () => {
setCreateModalOpen(true);
};
const handleViewModeChange = (
_: React.MouseEvent<HTMLElement>,
newMode: 'grid' | 'list' | null
) => {
if (newMode !== null) {
setViewMode(newMode);
}
};
// Loading state
if (isLoading) {
return (
<Container>
<Box
display="flex"
flexDirection="column"
alignItems="center"
justifyContent="center"
minHeight="400px"
>
<BATbernLoader size={96} />
<Typography variant="body1" sx={{ mt: 2 }}>
{t('loading.users')}
</Typography>
</Box>
</Container>
);
}
// Error state
if (isError) {
return (
<Container>
<Box sx={{ mt: 4 }}>
<Alert severity="error" sx={{ mb: 2 }}>
{t('error.loadFailed')}
</Alert>
<Button variant="contained" onClick={() => refetch()}>
{t('common:actions.retry')}
</Button>
</Box>
</Container>
);
}
const users = data?.data || [];
const paginationData = data?.pagination;
return (
<Box sx={{ flexGrow: 1, p: 3 }}>
<Container maxWidth="xl">
{/* Header */}
<Stack
direction={{ xs: 'column', md: 'row' }}
justifyContent="space-between"
alignItems={{ xs: 'flex-start', md: 'center' }}
spacing={2}
mb={3}
>
<Typography variant="h4" component="h1">
{t('title')}
</Typography>
<Stack direction="row" spacing={1}>
{/* View Toggle — hidden on mobile (cards forced) */}
{!isMobile && (
<ToggleButtonGroup
value={viewMode}
exclusive
onChange={handleViewModeChange}
aria-label={t('common:labels.viewMode')}
size="small"
>
<ToggleButton value="grid" aria-label={t('common:labels.gridView')}>
<GridIcon />
</ToggleButton>
<ToggleButton value="list" aria-label={t('common:labels.listView')}>
<ListIcon />
</ToggleButton>
</ToggleButtonGroup>
)}
<Button
variant="contained"
color="primary"
startIcon={<AddIcon />}
onClick={handleOpenCreateModal}
data-testid="user-add-button"
>
<Box component="span" sx={{ display: { xs: 'none', sm: 'inline' } }}>
{t('addUser')}
</Box>
</Button>
</Stack>
</Stack>
{/* Filters Panel */}
<UserFilters />
{/* User List - Cards on mobile, Grid or Table on desktop */}
{isMobile ? (
<Stack spacing={2}>
{users.map((user) => (
<UserCard
key={user.id}
user={user}
onClick={handleRowClick}
onAction={handleUserAction}
/>
))}
</Stack>
) : viewMode === 'list' ? (
<UserTable
users={users}
onRowClick={handleRowClick}
sortBy={sortBy as 'name' | 'email' | 'company'}
sortDir={sortDir}
onSortChange={(field, dir) => setSort(field, dir)}
onAction={handleUserAction}
/>
) : (
<Grid container spacing={2}>
{users.map((user) => (
<Grid size={{ xs: 12, sm: 6, md: 4, lg: 3 }} key={user.id}>
<UserCard user={user} onClick={handleRowClick} onAction={handleUserAction} />
</Grid>
))}
</Grid>
)}
{/* Pagination */}
{paginationData && (
<UserPagination
page={paginationData.page}
totalPages={paginationData.totalPages}
limit={paginationData.limit}
onPageChange={(newPage) => setPage(newPage)}
onLimitChange={(newLimit) => setLimit(newLimit)}
/>
)}
{/* Modals */}
{/* Create/Edit User Modal */}
<UserCreateEditModal
open={createModalOpen || !!editUser}
user={editUser}
onClose={() => {
setCreateModalOpen(false);
setEditUser(null);
}}
onSuccess={() => {
refetch();
setCreateModalOpen(false);
setEditUser(null);
}}
/>
<RoleManagerModal
user={roleManagerUser}
open={!!roleManagerUser}
onClose={() => setRoleManagerUser(null)}
onSuccess={() => refetch()}
/>
<DeleteUserDialog
user={deleteDialogUser}
open={!!deleteDialogUser}
onClose={() => setDeleteDialogUser(null)}
onSuccess={() => refetch()}
/>
</Container>
</Box>
);
};
export default UserList;
|