All files / src/components/organizer/EventManagement WorkflowProgressBarWithQuery.tsx

88.88% Statements 16/18
93.33% Branches 14/15
80% Functions 4/5
88.88% Lines 16/18

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                                                                1x         21x             21x     13x           21x 11x                     10x 3x                             7x       7x     7x   7x     7x   7x                                                                 3x                             3x                                
/**
 * WorkflowProgressBarWithQuery Component (Story 5.1a - Task 9)
 *
 * AC: 15-17 (Frontend Integration)
 *
 * Displays workflow progress with React Query integration:
 * - Fetches workflow status from GET /api/v1/events/{code}/workflow/status
 * - Real-time updates via refetchInterval
 * - Loading and error states
 * - Validation blocker messages
 * - Current workflow state display
 */
 
import React from 'react';
import { Box, LinearProgress, Typography, Alert, Button, Stack, Chip } from '@mui/material';
import { BATbernLoader } from '@components/shared/BATbernLoader';
import { Warning as WarningIcon, Refresh as RefreshIcon } from '@mui/icons-material';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { workflowService } from '@/services/workflowService';
import {
  getWorkflowProgress,
  getWorkflowStateI18nKey,
  getProgressColor,
} from '@/utils/workflow/workflowState';
 
interface WorkflowProgressBarWithQueryProps {
  eventCode: string;
  refetchInterval?: number; // Optional refetch interval in ms (default: 5000ms)
  compact?: boolean;
}
 
export const WorkflowProgressBarWithQuery: React.FC<WorkflowProgressBarWithQueryProps> = ({
  eventCode,
  refetchInterval = 5000,
  compact = false,
}) => {
  const { t } = useTranslation('events');
 
  // Fetch workflow status using React Query
  const {
    data: workflowStatus,
    isLoading,
    isError,
    refetch,
  } = useQuery({
    queryKey: ['workflowStatus', eventCode],
    queryFn: () => workflowService.getWorkflowStatus(eventCode),
    refetchInterval,
    staleTime: 0, // Always refetch
  });
 
  // Loading state
  if (isLoading) {
    return (
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 2, p: 2 }}>
        <BATbernLoader size={48} />
        <Typography variant="body2" color="text.secondary">
          {t('workflow.loading')}
        </Typography>
      </Box>
    );
  }
 
  // Error state
  if (isError) {
    return (
      <Alert
        severity="error"
        action={
          <Button size="small" startIcon={<RefreshIcon />} onClick={() => refetch()}>
            {t('common.retry')}
          </Button>
        }
      >
        {t('workflow.error')}
      </Alert>
    );
  }
 
  // No data state
  Iif (!workflowStatus) {
    return <Alert severity="info">{t('workflow.noData', 'No workflow status available')}</Alert>;
  }
 
  const { currentState, validationMessages, blockedTransitions } = workflowStatus;
 
  // Calculate progress
  const progress = getWorkflowProgress(currentState);
  // Get translated state label
  const stateLabel = t(getWorkflowStateI18nKey(currentState), currentState);
 
  // Determine progress bar color
  const progressColor = getProgressColor(progress);
 
  return (
    <Box sx={{ width: '100%' }}>
      <Stack spacing={2}>
        {/* Current State Header */}
        <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <Typography variant="body2" color="text.secondary" fontWeight="medium">
            {t('workflow.currentState', 'Current State')}: <strong>{stateLabel}</strong>
          </Typography>
          <Typography variant="caption" color="text.secondary">
            {progress}%
          </Typography>
        </Box>
 
        {/* Progress Bar */}
        <LinearProgress
          variant="determinate"
          value={progress}
          color={progressColor}
          sx={{ height: 8, borderRadius: 1 }}
        />
 
        {/* Validation Messages (Blockers) */}
        {validationMessages && validationMessages.length > 0 && (
          <Alert
            severity="warning"
            icon={<WarningIcon />}
            sx={{ '& .MuiAlert-message': { width: '100%' } }}
          >
            <Typography variant="body2" fontWeight="medium" gutterBottom>
              {t('workflow.blockers', { count: validationMessages.length })}:
            </Typography>
            <Stack spacing={0.5}>
              {validationMessages.map((message, index) => (
                <Typography key={index} variant="caption" component="div">
                  • {message}
                </Typography>
              ))}
            </Stack>
          </Alert>
        )}
 
        {/* Blocked Transitions Info */}
        {!compact && blockedTransitions && blockedTransitions.length > 0 && (
          <Box sx={{ display: 'flex', gap: 1, flexWrap: 'wrap' }}>
            <Typography variant="caption" color="text.secondary">
              {t('workflow.blockedTransitions', 'Blocked:')}
            </Typography>
            {blockedTransitions.map((state, index) => (
              <Chip
                key={index}
                label={t(getWorkflowStateI18nKey(state), state)}
                size="small"
                variant="outlined"
                color="warning"
              />
            ))}
          </Box>
        )}
      </Stack>
    </Box>
  );
};
 
export default WorkflowProgressBarWithQuery;