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 | 1x 121x 121x 121x 121x 121x 121x 121x 121x 11x 2x 2x 9x 9x 121x 4x 1x 3x 3x 3x 3x 3x 3x 121x 6x 6x 6x 1x 5x 6x 2x 6x 6x 121x 6x 2x 4x 121x 1x 1x 1x 1x 121x 121x 63x 22x 3x | /**
* Create/Edit Topic Modal Component
* Story 5.2: Topic Selection & Speaker Brainstorming
* Story 5.2a: Edit Topic Feature
*
* Modal dialog for creating new or editing existing topics in the backlog.
* Features:
* - Form with title, description, category fields
* - Validation (required fields, max lengths)
* - Duplicate detection warning (similarity >70%)
* - Success/error feedback
* - i18n support (German/English)
* - Edit mode when topic prop is provided
*/
import React, { useState, useEffect } from 'react';
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
TextField,
Alert,
Box,
CircularProgress,
Select,
MenuItem,
FormControl,
InputLabel,
FormHelperText,
useMediaQuery,
useTheme,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { topicService } from '@/services/topicService';
import type { CreateTopicRequest, Topic } from '@/types/topic.types';
export interface CreateTopicModalProps {
open: boolean;
topic?: Topic | null; // If provided, modal is in edit mode
onClose: () => void;
onSuccess?: () => void;
}
export const CreateTopicModal: React.FC<CreateTopicModalProps> = ({
open,
topic,
onClose,
onSuccess,
}) => {
const { t } = useTranslation(['organizer', 'common']);
const queryClient = useQueryClient();
const theme = useTheme();
const isFullScreen = useMediaQuery(theme.breakpoints.down('md'));
const isEditMode = !!topic;
const [formData, setFormData] = useState<CreateTopicRequest>({
title: '',
description: '',
category: '',
});
const [errors, setErrors] = useState<Record<string, string>>({});
// Populate form when topic changes (edit mode)
useEffect(() => {
if (topic) {
setFormData({
title: topic.title,
description: topic.description || '',
category: topic.category,
});
setErrors({});
} else {
setFormData({ title: '', description: '', category: '' });
setErrors({});
}
}, [topic, open]);
// Mutation for creating/updating topic
const createTopicMutation = useMutation({
mutationFn: (request: CreateTopicRequest) => {
if (isEditMode && topic) {
return topicService.updateTopic(topic.topicCode, request);
}
return topicService.createTopic(request);
},
onSuccess: () => {
// Invalidate topics query to refetch the list
queryClient.invalidateQueries({ queryKey: ['topics'] });
// Reset form
setFormData({ title: '', description: '', category: '' });
setErrors({});
// Call success callback
onSuccess?.();
// Close modal
onClose();
},
});
const validateForm = (): boolean => {
const newErrors: Record<string, string> = {};
const modalPrefix = isEditMode ? 'editModal' : 'createModal';
if (!formData.title.trim()) {
newErrors.title = t(`topicBacklog.${modalPrefix}.errors.titleRequired`, 'Title is required');
} else if (IformData.title.length > 255) {
newErrors.title = t(
`topicBacklog.${modalPrefix}.errors.titleTooLong`,
'Title must be 255 characters or less'
);
}
if (!formData.category.trim()) {
newErrors.category = t(
`topicBacklog.${modalPrefix}.errors.categoryRequired`,
'Category is required'
);
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = () => {
if (!validateForm()) {
return;
}
createTopicMutation.mutate(formData);
};
const handleClose = () => {
Eif (!createTopicMutation.isPending) {
setFormData({ title: '', description: '', category: '' });
setErrors({});
onClose();
}
};
const modalPrefix = isEditMode ? 'editModal' : 'createModal';
return (
<Dialog
open={open}
onClose={handleClose}
maxWidth="sm"
fullWidth
fullScreen={isFullScreen}
data-testid="create-topic-modal"
>
<DialogTitle>
{t(`topicBacklog.${modalPrefix}.title`, isEditMode ? 'Edit Topic' : 'Create New Topic')}
</DialogTitle>
<DialogContent>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, mt: 2 }}>
{createTopicMutation.isError && (
<Alert severity="error" data-testid="topic-form-error">
{t(
`topicBacklog.${modalPrefix}.errors.${isEditMode ? 'updateFailed' : 'createFailed'}`,
`Failed to ${isEditMode ? 'update' : 'create'} topic. Please try again.`
)}
</Alert>
)}
<TextField
label={t(`topicBacklog.${modalPrefix}.fields.title`, 'Title')}
value={formData.title}
onChange={(e) => setFormData({ ...formData, title: e.target.value })}
error={!!errors.title}
helperText={errors.title}
required
fullWidth
disabled={createTopicMutation.isPending}
inputProps={{ maxLength: 255, 'data-testid': 'topic-title-input' }}
/>
<TextField
label={t(`topicBacklog.${modalPrefix}.fields.description`, 'Description')}
value={formData.description || ''}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
error={!!errors.description}
helperText={errors.description}
fullWidth
multiline
rows={4}
disabled={createTopicMutation.isPending}
inputProps={{ 'data-testid': 'topic-description-input' }}
/>
<FormControl
fullWidth
required
error={!!errors.category}
disabled={createTopicMutation.isPending}
data-testid="topic-category-select"
>
<InputLabel>{t(`topicBacklog.${modalPrefix}.fields.category`, 'Category')}</InputLabel>
<Select
value={formData.category}
onChange={(e) => setFormData({ ...formData, category: e.target.value })}
label={t(`topicBacklog.${modalPrefix}.fields.category`, 'Category')}
data-testid="topic-category-select-input"
>
<MenuItem value="technical" data-testid="category-option-technical">
{t('topicBacklog.filters.categories.technical', 'Technical')}
</MenuItem>
<MenuItem value="management" data-testid="category-option-management">
{t('topicBacklog.filters.categories.management', 'Management')}
</MenuItem>
<MenuItem value="soft_skills" data-testid="category-option-soft-skills">
{t('topicBacklog.filters.categories.softSkills', 'Soft Skills')}
</MenuItem>
<MenuItem value="industry_trends" data-testid="category-option-industry-trends">
{t('topicBacklog.filters.categories.industryTrends', 'Industry Trends')}
</MenuItem>
<MenuItem value="tools_platforms" data-testid="category-option-tools-platforms">
{t('topicBacklog.filters.categories.toolsPlatforms', 'Tools & Platforms')}
</MenuItem>
</Select>
{errors.category && <FormHelperText>{errors.category}</FormHelperText>}
</FormControl>
</Box>
</DialogContent>
<DialogActions>
<Button
onClick={handleClose}
disabled={createTopicMutation.isPending}
data-testid="cancel-topic-button"
>
{t('common:actions.cancel')}
</Button>
<Button
variant="contained"
onClick={handleSubmit}
disabled={createTopicMutation.isPending}
startIcon={createTopicMutation.isPending ? <CircularProgress size={20} /> : undefined}
data-testid="submit-topic-button"
>
{createTopicMutation.isPending
? t(
`topicBacklog.${modalPrefix}.${isEditMode ? 'saving' : 'creating'}`,
isEditMode ? 'Saving...' : 'Creating...'
)
: t(
`topicBacklog.${modalPrefix}.${isEditMode ? 'save' : 'create'}`,
isEditMode ? 'Save Changes' : 'Create Topic'
)}
</Button>
</DialogActions>
</Dialog>
);
};
|