All files / src/components/organizer/PartnerManagement PartnerList.tsx

85% Statements 34/40
68.88% Branches 31/45
55.55% Functions 5/9
89.47% Lines 34/38

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                                      4x 14x 14x 14x 14x 14x     14x           14x 14x                   14x     14x 1x       6x                             13x 1x 1x 1x   1x                         12x 1x                                   11x 14x 14x   14x 14x 14x 1x 1x     14x             14x 10x       20x                                                                                                                                             1x       2x                                                                                                                                      
import React from 'react';
import {
  Box,
  Stack,
  Typography,
  Button,
  ButtonGroup,
  Skeleton,
  Alert,
  useTheme,
  useMediaQuery,
} from '@mui/material';
import Grid from '@mui/material/Grid';
import { FirstPage, NavigateBefore, NavigateNext, LastPage } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { PartnerCard } from './PartnerCard';
import { usePartners } from '@/hooks/usePartners';
import { usePartnerStore } from '@/stores/partnerStore';
 
export const PartnerList: React.FC = () => {
  const { t } = useTranslation('partners');
  const { viewMode, filters, searchQuery, sortBy, sortOrder, page, setPage } = usePartnerStore();
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
  const effectiveViewMode = isMobile ? 'list' : viewMode;
 
  // Fetch partners using React Query
  const { data, isLoading, isError, error } = usePartners(
    filters,
    { sortBy, sortOrder },
    { page, size: 20 }
  );
 
  const allPartners = data?.data || [];
  const partners = searchQuery
    ? allPartners.filter((partner) => {
        const q = searchQuery.toLowerCase();
        return (
          partner.companyName.toLowerCase().includes(q) ||
          partner.company?.displayName?.toLowerCase().includes(q) ||
          partner.company?.name?.toLowerCase().includes(q)
        );
      })
    : allPartners;
  const pagination = data?.metadata;
 
  // Loading state
  if (isLoading) {
    return (
      <Box data-testid="partner-list-loading">
        <Grid container spacing={2} data-testid="partner-list-grid">
          {[...Array(6)].map((_, index) => (
            <Grid size={{ xs: 12, sm: 6, md: 4 }} key={index}>
              <Skeleton
                variant="rectangular"
                height={300}
                data-testid="partner-skeleton"
                sx={{ borderRadius: 1 }}
              />
            </Grid>
          ))}
        </Grid>
      </Box>
    );
  }
 
  // Error state
  if (isError) {
    const errorObj = error as Error & { correlationId?: string };
    const errorMessage = errorObj?.message || t('error.failedToLoad');
    const correlationId = errorObj?.correlationId || 'N/A';
 
    return (
      <Box data-testid="partner-list-error">
        <Alert severity="error">
          <Typography variant="body1">{errorMessage}</Typography>
          <Typography variant="caption" color="text.secondary">
            {t('error.correlationId')}: {correlationId}
          </Typography>
        </Alert>
      </Box>
    );
  }
 
  // Empty state
  if (partners.length === 0) {
    return (
      <Box
        data-testid="partner-list-empty"
        sx={{
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          minHeight: 300,
        }}
      >
        <Typography variant="h6" color="text.secondary">
          {t('empty.noPartners')}
        </Typography>
      </Box>
    );
  }
 
  // Pagination controls
  const showPagination = pagination && pagination.totalPages > 1;
  const isFirstPage = pagination?.page === 0;
  const isLastPage = pagination && pagination.page === pagination.totalPages - 1;
 
  const handleFirstPage = () => setPage(0);
  const handlePrevPage = () => setPage(Math.max(0, page - 1));
  const handleNextPage = () => {
    Eif (pagination) {
      setPage(Math.min(pagination.totalPages - 1, page + 1));
    }
  };
  const handleLastPage = () => {
    if (pagination) {
      setPage(pagination.totalPages - 1);
    }
  };
 
  // Grid view
  if (effectiveViewMode === 'grid') {
    return (
      <Box>
        <Grid container spacing={2} data-testid="partner-grid" data-columns="3" data-spacing="3">
          {partners.map((partner) => (
            <Grid size={{ xs: 12, sm: 6, md: 4 }} key={partner.companyName}>
              <PartnerCard partner={partner} />
            </Grid>
          ))}
        </Grid>
 
        {showPagination && (
          <Box
            component="nav"
            role="navigation"
            aria-label={t('pagination.ariaLabel')}
            data-testid="partner-pagination"
            sx={{
              display: 'flex',
              justifyContent: 'space-between',
              alignItems: 'center',
              mt: 3,
              pt: 2,
              borderTop: 1,
              borderColor: 'divider',
            }}
          >
            <Typography variant="body2" color="text.secondary" data-testid="pagination-info">
              {t('pagination.page', {
                current: (pagination?.page || 0) + 1,
                total: pagination?.totalPages || 1,
              })}{' '}
              • {t('pagination.partners', { count: pagination?.totalItems || 0 })}
            </Typography>
 
            <ButtonGroup variant="outlined" size="small">
              <Button
                onClick={handleFirstPage}
                disabled={isFirstPage}
                aria-label={t('pagination.firstPage')}
                data-testid="first-page-button"
              >
                <FirstPage />
              </Button>
              <Button
                onClick={handlePrevPage}
                disabled={isFirstPage}
                aria-label={t('pagination.previousPage')}
                data-testid="prev-page-button"
              >
                <NavigateBefore />
              </Button>
              <Button
                onClick={handleNextPage}
                disabled={isLastPage}
                aria-label={t('pagination.nextPage')}
                data-testid="next-page-button"
              >
                <NavigateNext />
              </Button>
              <Button
                onClick={handleLastPage}
                disabled={isLastPage}
                aria-label={t('pagination.lastPage')}
                data-testid="last-page-button"
              >
                <LastPage />
              </Button>
            </ButtonGroup>
          </Box>
        )}
      </Box>
    );
  }
 
  // List view (single column)
  return (
    <Box>
      <Stack spacing={2} data-testid="partner-list-list">
        {partners.map((partner) => (
          <PartnerCard key={partner.companyName} partner={partner} />
        ))}
      </Stack>
 
      {showPagination && (
        <Box
          component="nav"
          role="navigation"
          aria-label={t('pagination.ariaLabel')}
          data-testid="partner-pagination"
          sx={{
            display: 'flex',
            justifyContent: 'space-between',
            alignItems: 'center',
            mt: 3,
            pt: 2,
            borderTop: 1,
            borderColor: 'divider',
          }}
        >
          <Typography variant="body2" color="text.secondary" data-testid="pagination-info">
            {t('pagination.page', {
              current: (pagination?.page || 0) + 1,
              total: pagination?.totalPages || 1,
            })}{' '}
            • {t('pagination.partners', { count: pagination?.totalItems || 0 })}
          </Typography>
 
          <ButtonGroup variant="outlined" size="small">
            <Button
              onClick={handleFirstPage}
              disabled={isFirstPage}
              aria-label={t('pagination.firstPage')}
              data-testid="first-page-button"
            >
              <FirstPage />
            </Button>
            <Button
              onClick={handlePrevPage}
              disabled={isFirstPage}
              aria-label={t('pagination.previousPage')}
              data-testid="prev-page-button"
            >
              <NavigateBefore />
            </Button>
            <Button
              onClick={handleNextPage}
              disabled={isLastPage}
              aria-label={t('pagination.nextPage')}
              data-testid="next-page-button"
            >
              <NavigateNext />
            </Button>
            <Button
              onClick={handleLastPage}
              disabled={isLastPage}
              aria-label={t('pagination.lastPage')}
              data-testid="last-page-button"
            >
              <LastPage />
            </Button>
          </ButtonGroup>
        </Box>
      )}
    </Box>
  );
};