All files / src/components/organizer/Tasks TaskWidget.tsx

68.96% Statements 20/29
66.66% Branches 10/15
45.45% Functions 5/11
67.85% Lines 19/28

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                                                        2x 15x 15x 15x 15x 15x           15x     13x         15x                   15x       15x         15x     15x 13x             39x                   2x                           2x                                                   2x                                 2x                                                                              
/**
 * TaskWidget Component (Story 5.5 Phase 6 - Modified for routing)
 *
 * AC24: Critical tasks widget for dashboard sidebar
 * Wireframe: docs/wireframes/5.5-content-review-task-system-ux-flow.md
 *
 * Features:
 * - Displays all active tasks (pending, todo, in_progress)
 * - Shows task name, event, due date, assignee
 * - "View All Tasks" button navigates to /organizer/tasks page
 * - Auto-refreshes on task completion
 */
 
import React, { useState } from 'react';
import { List, ListItem, Typography, Box, Button, Chip, Stack, Skeleton } from '@mui/material';
import { CheckCircle as CheckCircleIcon, OpenInNew as OpenInNewIcon } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { de, enUS } from 'date-fns/locale';
import { taskService, type EventTaskResponse } from '@/services/taskService';
import { TaskCard } from './TaskCard';
import { CustomTaskModal } from './CustomTaskModal';
 
interface TaskWidgetProps {
  organizerUsername: string;
}
 
export const TaskWidget: React.FC<TaskWidgetProps> = ({ organizerUsername }) => {
  const { t, i18n } = useTranslation('events');
  const queryClient = useQueryClient();
  const navigate = useNavigate();
  const locale = i18n.language === 'de' ? de : enUS;
  const [editingTask, setEditingTask] = useState<EventTaskResponse | null>(null);
 
  // Fetch critical tasks
  const {
    data: criticalTasks = [],
    isLoading,
    isError,
  } = useQuery({
    queryKey: ['tasks', 'critical', organizerUsername],
    queryFn: () => taskService.getMyTasks(true),
    refetchInterval: 60000, // Auto-refresh every 60 seconds
  });
 
  // Complete task mutation
  const completeTaskMutation = useMutation({
    mutationFn: ({ taskId, notes }: { taskId: string; notes?: string }) =>
      taskService.completeTask(taskId, { notes }),
    onSuccess: () => {
      // Invalidate critical tasks query to refresh the list
      queryClient.invalidateQueries({ queryKey: ['tasks', 'critical'] });
      queryClient.invalidateQueries({ queryKey: ['tasks', 'my-tasks'] });
    },
  });
 
  const handleCompleteTask = (taskId: string) => {
    completeTaskMutation.mutate({ taskId });
  };
 
  const handleViewAllTasks = () => {
    navigate('/organizer/tasks');
  };
 
  // Show all non-completed tasks (backend already filters)
  const activeTasks = criticalTasks.filter((task) => task.status !== 'completed');
 
  // Loading state
  if (isLoading) {
    return (
      <Box>
        <Typography variant="h6" gutterBottom>
          {t('tasks.criticalTasks', 'Critical Tasks')}
        </Typography>
        <List>
          {[1, 2, 3].map((index) => (
            <ListItem key={index} data-testid={`skeleton-task-${index}`}>
              <Skeleton variant="rectangular" width="100%" height={80} />
            </ListItem>
          ))}
        </List>
      </Box>
    );
  }
 
  // Error state
  Iif (isError) {
    return (
      <Box textAlign="center" py={4}>
        <Typography variant="h6" gutterBottom>
          {t('tasks.criticalTasks', 'Critical Tasks')}
        </Typography>
        <Typography variant="body2" color="error">
          {t('tasks.errorLoadingTasks', 'Failed to load tasks')}
        </Typography>
      </Box>
    );
  }
 
  // Empty state
  Iif (activeTasks.length === 0) {
    return (
      <Box textAlign="center" py={4}>
        <Typography variant="h6" gutterBottom>
          {t('tasks.criticalTasks', 'Critical Tasks')}
        </Typography>
        <CheckCircleIcon color="success" sx={{ fontSize: 48, mb: 2 }} />
        <Typography variant="body2" color="text.secondary">
          {t('tasks.noCriticalTasks', 'No critical tasks')}
        </Typography>
        <Typography variant="body2" color="text.secondary" gutterBottom>
          {t('tasks.greatJob', 'Great job!')}
        </Typography>
        <Button
          variant="outlined"
          size="small"
          onClick={handleViewAllTasks}
          sx={{ mt: 2 }}
          endIcon={<OpenInNewIcon />}
        >
          {t('tasks.viewAllTasks', 'View All Tasks')}
        </Button>
      </Box>
    );
  }
 
  return (
    <Box aria-label={t('tasks.criticalTasksWidget', 'Critical tasks widget')}>
      <Stack direction="row" justifyContent="space-between" alignItems="center" mb={2}>
        <Typography variant="h6">{t('tasks.criticalTasks', 'Critical Tasks')}</Typography>
        <Chip
          label={`${activeTasks.length} ${activeTasks.length === 1 ? t('tasks.task', 'task') : t('tasks.tasks', 'tasks')}`}
          size="small"
          color="primary"
          aria-label={t('tasks.activeAriaLabel', {
            count: activeTasks.length,
            taskLabel: activeTasks.length === 1 ? t('tasks.task') : t('tasks.tasks'),
          })}
        />
      </Stack>
 
      <List sx={{ maxHeight: 400, overflow: 'auto' }} data-testid="critical-tasks-list">
        {activeTasks.map((task) => (
          <TaskCard
            key={task.id}
            task={task}
            locale={locale}
            onComplete={handleCompleteTask}
            onEdit={(t) => setEditingTask(t)}
            showCompleteButton={true}
            showEventCode={true}
            showTriggerState={false}
            showCompletionInfo={false}
            t={t}
          />
        ))}
      </List>
 
      {/* View All Tasks Button */}
      <Box mt={2}>
        <Button
          variant="outlined"
          fullWidth
          size="small"
          onClick={handleViewAllTasks}
          endIcon={<OpenInNewIcon />}
        >
          {t('tasks.viewAllTasks', 'View All Tasks')}
        </Button>
      </Box>
 
      {/* Edit Task Modal */}
      <CustomTaskModal
        open={editingTask !== null}
        onClose={() => setEditingTask(null)}
        eventId={null}
        organizerUsername={organizerUsername}
        existingTask={editingTask ?? undefined}
      />
    </Box>
  );
};