All files / src/components/organizer/SpeakerDrawer AssignedOrganizerField.tsx

69.23% Statements 9/13
83.33% Branches 5/6
50% Functions 4/8
81.81% Lines 9/11

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                        1x       8x 8x   8x           8x 3x               1x             1x                   8x                                                                    
import React, { useState } from 'react';
import { Box, Typography, CircularProgress, Snackbar, Alert } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { OrganizerSelect } from '@/components/shared/OrganizerSelect/OrganizerSelect';
import { usePatchSpeakerPool } from '@/hooks/useSpeakerPool';
import type { SpeakerPoolEntry } from '@/types/speakerPool.types';
 
interface AssignedOrganizerFieldProps {
  speaker: SpeakerPoolEntry;
  eventCode: string;
}
 
export const AssignedOrganizerField: React.FC<AssignedOrganizerFieldProps> = ({
  speaker,
  eventCode,
}) => {
  const { t } = useTranslation('organizer');
  const patchMutation = usePatchSpeakerPool();
 
  const [snackbar, setSnackbar] = useState<{
    open: boolean;
    message: string;
    severity: 'success' | 'error';
  }>({ open: false, message: '', severity: 'success' });
 
  const handleChange = (organizerId: string) => {
    patchMutation.mutate(
      {
        eventCode,
        speakerId: speaker.id,
        request: { assignedOrganizerId: organizerId || undefined },
      },
      {
        onSuccess: () => {
          setSnackbar({
            open: true,
            message: t('speakers.organizerUpdated', 'Organizer updated'),
            severity: 'success',
          });
        },
        onError: () => {
          setSnackbar({
            open: true,
            message: t('speakers.organizerUpdateFailed', 'Failed to update organizer'),
            severity: 'error',
          });
        },
      }
    );
  };
 
  return (
    <Box>
      <Typography variant="subtitle2" gutterBottom>
        {t('speakerBrainstorm.form.assignOrganizer')}
      </Typography>
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
        <OrganizerSelect
          value={speaker.assignedOrganizerId ?? ''}
          onChange={handleChange}
          includeUnassigned={true}
          disabled={patchMutation.isPending}
          size="small"
          fullWidth
        />
        {patchMutation.isPending && <CircularProgress size={20} />}
      </Box>
 
      <Snackbar
        open={snackbar.open}
        autoHideDuration={4000}
        onClose={() => setSnackbar((s) => ({ ...s, open: false }))}
        anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
      >
        <Alert
          onClose={() => setSnackbar((s) => ({ ...s, open: false }))}
          severity={snackbar.severity}
          sx={{ width: '100%' }}
        >
          {snackbar.message}
        </Alert>
      </Snackbar>
    </Box>
  );
};