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 | /**
* Mark Contacted Modal Component (Story 5.3 - Task 5b)
*
* Modal dialog for recording speaker outreach attempts
* Features:
* - Contact method selection (email/phone/in-person)
* - Contact date picker
* - Notes text area
* - Form validation
* - i18n support (German/English)
*/
import React, { useState, useEffect } from 'react';
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
TextField,
Select,
MenuItem,
FormControl,
InputLabel,
Box,
IconButton,
Alert,
useMediaQuery,
useTheme,
} from '@mui/material';
import { Close as CloseIcon } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useRecordOutreach } from '../../../hooks/useSpeakerOutreach';
import type { ContactMethod } from '../../../types/speakerOutreach.types';
interface MarkContactedModalProps {
open: boolean;
onClose: () => void;
onSuccess?: () => void;
eventCode: string;
speakerId: string;
speakerName: string;
}
interface FormData {
contactMethod: ContactMethod | '';
contactDate: string;
notes: string;
}
interface FormErrors {
contactMethod?: string;
contactDate?: string;
}
// `<input type="datetime-local">` interprets its value as **local time**. Building the
// default with `toISOString().slice(0, 16)` returns UTC and silently shifts the wall-clock
// time backwards (e.g. CEST organizer at 17:39 sees `15:38` and submits 2h-stale data).
function nowAsLocalDatetimeLocal(): string {
const d = new Date();
const pad = (n: number) => n.toString().padStart(2, '0');
return (
`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` +
`T${pad(d.getHours())}:${pad(d.getMinutes())}`
);
}
const MarkContactedModal: React.FC<MarkContactedModalProps> = ({
open,
onClose,
onSuccess,
eventCode,
speakerId,
speakerName,
}) => {
const { t } = useTranslation('organizer');
const recordOutreachMutation = useRecordOutreach();
const theme = useTheme();
const isFullScreen = useMediaQuery(theme.breakpoints.down('md'));
const initialFormData: FormData = {
contactMethod: '',
contactDate: nowAsLocalDatetimeLocal(),
notes: '',
};
const [formData, setFormData] = useState<FormData>(initialFormData);
const [errors, setErrors] = useState<FormErrors>({});
// Reset form when modal opens
useEffect(() => {
if (open) {
setFormData({
...initialFormData,
contactDate: nowAsLocalDatetimeLocal(),
});
setErrors({});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);
const validateForm = (): boolean => {
const newErrors: FormErrors = {};
if (!formData.contactMethod) {
newErrors.contactMethod = t('speakerOutreach.markContactedModal.error.methodRequired');
}
if (!formData.contactDate) {
newErrors.contactDate = t('speakerOutreach.markContactedModal.error.dateRequired');
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = async () => {
if (!validateForm()) {
return;
}
try {
await recordOutreachMutation.mutateAsync({
eventCode,
speakerId,
request: {
contactMethod: formData.contactMethod as ContactMethod,
contactDate: new Date(formData.contactDate).toISOString(),
notes: formData.notes || undefined,
},
});
onSuccess?.();
onClose();
} catch (error) {
console.error('Failed to record outreach:', error);
}
};
const handleContactMethodChange = (value: string) => {
setFormData({ ...formData, contactMethod: value as ContactMethod });
if (errors.contactMethod) {
setErrors({ ...errors, contactMethod: undefined });
}
};
const handleContactDateChange = (value: string) => {
setFormData({ ...formData, contactDate: value });
if (errors.contactDate) {
setErrors({ ...errors, contactDate: undefined });
}
};
const handleNotesChange = (value: string) => {
setFormData({ ...formData, notes: value });
};
return (
<Dialog
open={open}
onClose={onClose}
maxWidth="sm"
fullWidth
fullScreen={isFullScreen}
data-testid="mark-contacted-modal"
>
<DialogTitle>
<Box display="flex" justifyContent="space-between" alignItems="center">
{t('speakerOutreach.markContactedModal.title')}
<IconButton onClick={onClose} size="small" aria-label={t('common:actions.close')}>
<CloseIcon />
</IconButton>
</Box>
</DialogTitle>
<DialogContent>
<Box display="flex" flexDirection="column" gap={2} mt={1}>
{recordOutreachMutation.isError && (
<Alert severity="error">{t('speakerOutreach.markContactedModal.error.failed')}</Alert>
)}
<TextField label={t('common:role.speaker')} value={speakerName} disabled fullWidth />
<FormControl fullWidth error={!!errors.contactMethod} required>
<InputLabel>{t('speakerOutreach.contactMethod')}</InputLabel>
<Select
value={formData.contactMethod}
onChange={(e) => handleContactMethodChange(e.target.value)}
label={t('speakerOutreach.contactMethod')}
data-testid="contact-method-select"
>
<MenuItem value="email" data-testid="contact-method-option-email">
{t('speakerOutreach.markContactedModal.method.email')}
</MenuItem>
<MenuItem value="phone" data-testid="contact-method-option-phone">
{t('speakerOutreach.markContactedModal.method.phone')}
</MenuItem>
<MenuItem value="in_person" data-testid="contact-method-option-in-person">
{t('speakerOutreach.markContactedModal.method.inPerson')}
</MenuItem>
</Select>
{errors.contactMethod && (
<Box color="error.main" fontSize="0.75rem" mt={0.5}>
{errors.contactMethod}
</Box>
)}
</FormControl>
<TextField
label={t('speakerOutreach.contactDate')}
type="datetime-local"
value={formData.contactDate}
onChange={(e) => handleContactDateChange(e.target.value)}
error={!!errors.contactDate}
helperText={errors.contactDate}
required
fullWidth
InputLabelProps={{ shrink: true }}
data-testid="contact-date"
/>
<TextField
label={t('common:labels.notes')}
value={formData.notes}
onChange={(e) => handleNotesChange(e.target.value)}
multiline
rows={4}
fullWidth
placeholder={t('speakerOutreach.markContactedModal.notesPlaceholder')}
data-testid="contact-notes"
/>
</Box>
</DialogContent>
<DialogActions>
<Button
onClick={onClose}
disabled={recordOutreachMutation.isPending}
data-testid="cancel-button"
>
{t('common:actions.cancel')}
</Button>
<Button
onClick={handleSubmit}
variant="contained"
disabled={recordOutreachMutation.isPending}
data-testid="save-button"
>
{recordOutreachMutation.isPending
? t('common.saving')
: t('speakerOutreach.markContacted')}
</Button>
</DialogActions>
</Dialog>
);
};
export default MarkContactedModal;
|