All files / src/components/organizer/EventPage EventInfoTab.tsx

64.7% Statements 55/85
54.68% Branches 35/64
53.33% Functions 16/30
66.66% Lines 54/81

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528                                                                                                                      10x 10x 10x     1x 13x 13x 13x   13x 13x 13x 13x 13x 13x       13x 13x 13x 13x                       13x   13x 13x 5x                             13x           13x                 13x 5x       13x 5x 5x       5x           13x 1x 1x 1x 1x 1x     1x     1x 1x 1x 1x 1x 1x               13x   13x   13x                                                                                                                                                                                                                                                                 14x                           14x                                                                                                           1x                                       14x                               14x                                         14x                     14x                                 14x                           14x                                                                                                                      
/**
 * EventInfoTab (Epic 14, Story 14.F.2) — the "Info" sub-tab of Details.
 *
 * In-tab editable event identity (replaces the Phase-A read-only summary +
 * edit-modal): theme image (replace via FileUpload / ✨ AI-generate), Title,
 * Description (+ ✨ AI-generate), the selected Topic chip + "Change topic", and
 * When & where. Reuses the shared zod schema + `useUpdateEvent` (recompose, not
 * rewrite; NFR9) — saves changed fields on an explicit Save. "Change topic" is
 * an interim route nav; Story 14.F.3 swaps it for the focused overlay.
 */
 
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { useForm, Controller, type Resolver } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import {
  Alert,
  Box,
  Button,
  Chip,
  CircularProgress,
  IconButton,
  Paper,
  Stack,
  TextField,
  Tooltip,
  Typography,
} from '@mui/material';
import {
  AutoAwesome,
  Topic as TopicIcon,
  Delete as DeleteIcon,
  PhotoCamera as ReplaceIcon,
} from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useUpdateEvent } from '@/hooks/useEvents';
import { useFeatureFlags } from '@/hooks/useFeatureFlags';
import { useFileUpload } from '@/hooks/useFileUpload/useFileUpload';
import { topicService } from '@/services/topicService';
import type { Topic } from '@/types/topic.types';
import type { Event, EventDetailUI, EventUI } from '@/types/event.types';
import { EventTypeSelector } from '@/components/organizer/EventTypeSelector/EventTypeSelector';
import { AiAssistDrawer } from './AiAssistDrawer';
import { TopicSelectionOverlay } from './topicOverlay/TopicSelectionOverlay';
import {
  createEventSchema,
  normalizeEventType,
  transformDatesForApi,
  getChangedFields,
  type EventFormData,
  type PartialEventFormData,
} from '@/components/organizer/EventManagement/eventFormSchema';
 
interface EventInfoTabProps {
  event: Event | EventDetailUI;
  eventCode: string;
}
 
/** Date-time ISO → YYYY-MM-DD for date inputs. */
function toDateInput(value?: string): string {
  Iif (!value) return '';
  const d = new Date(value);
  return Number.isNaN(d.getTime()) ? '' : d.toISOString().slice(0, 10);
}
 
export const EventInfoTab: React.FC<EventInfoTabProps> = ({ event, eventCode }) => {
  const { t } = useTranslation('events');
  const { aiContentEnabled } = useFeatureFlags();
  const updateEvent = useUpdateEvent();
 
  const [aiDrawerOpen, setAiDrawerOpen] = useState(false);
  const [topicOverlayOpen, setTopicOverlayOpen] = useState(false);
  const [themeImageUploadId, setThemeImageUploadId] = useState<string | undefined>();
  const [topic, setTopic] = useState<Topic | null>(null);
  const [saved, setSaved] = useState(false);
  const [saveError, setSaveError] = useState<string | null>(null);
 
  // Theme image: a custom full-width preview with overlay actions (AI / replace /
  // delete), driven by the shared presigned-upload hook.
  const fileInputRef = useRef<HTMLInputElement>(null);
  const [previewUrl, setPreviewUrl] = useState<string | null>(null);
  const [imageRemoved, setImageRemoved] = useState(false);
  const { uploadFile, isUploading } = useFileUpload({
    uploadEndpoint: '/logos/presigned-url',
    maxFileSize: 5 * 1024 * 1024,
    allowedTypes: ['image/png', 'image/jpeg', 'image/svg+xml'],
    onUploadSuccess: (data) => {
      setThemeImageUploadId(data.uploadId);
      setPreviewUrl(data.tempFileUrl ?? null);
      setImageRemoved(false);
      setSaved(false);
    },
    onUploadError: (err) => setSaveError(err.message),
  });
  const shownImage = imageRemoved ? null : (previewUrl ?? event.themeImageUrl ?? null);
 
  const ev = event as EventUI;
  const initialValues = useMemo<EventFormData>(
    () => ({
      eventNumber: ev.eventNumber ?? 0,
      title: event.title ?? '',
      description: event.description ?? '',
      date: toDateInput(ev.date),
      registrationDeadline: toDateInput(ev.registrationDeadline),
      venueName: ev.venueName ?? '',
      venueAddress: ev.venueAddress ?? '',
      venueCapacity: ev.venueCapacity ?? 0,
      workflowState: ev.workflowState,
      eventType: normalizeEventType(ev.eventType),
    }),
    [event, ev]
  );
 
  const schema = useMemo(() => createEventSchema(t), [t]);
  const {
    control,
    handleSubmit,
    reset,
    setValue,
    formState: { errors, isDirty },
  } = useForm<EventFormData>({
    // z.coerce.number() infers `unknown` for inputs, so the resolver type needs a cast
    // (same pattern as EventForm).
    resolver: zodResolver(schema) as unknown as Resolver<EventFormData>,
    defaultValues: initialValues,
    mode: 'onBlur',
  });
 
  useEffect(() => {
    reset(initialValues);
  }, [initialValues, reset]);
 
  // Resolve the selected topic for the chip.
  useEffect(() => {
    const code = event.topicCode;
    Iif (!code) {
      setTopic(null);
      return;
    }
    topicService
      .getTopicById(code)
      .then(setTopic)
      .catch(() => setTopic(null));
  }, [event.topicCode]);
 
  const onSave = handleSubmit(async (data) => {
    setSaved(false);
    setSaveError(null);
    const changed: PartialEventFormData = getChangedFields(data, initialValues);
    const payload = transformDatesForApi(changed);
    Iif (imageRemoved) {
      // The PATCH endpoint clears the theme image when themeImageUploadId is blank.
      (payload as Record<string, unknown>).themeImageUploadId = '';
    } else if (IthemeImageUploadId) {
      (payload as Record<string, unknown>).themeImageUploadId = themeImageUploadId;
    }
    Iif (Object.keys(payload).length === 0) return;
    try {
      await updateEvent.mutateAsync({ eventCode, data: payload });
      setThemeImageUploadId(undefined);
      setImageRemoved(false);
      setSaved(true);
    } catch (e) {
      setSaveError(
        e instanceof Error ? e.message : t('errors.saveFailed', 'Could not save. Please try again.')
      );
    }
  });
 
  const dirty = isDirty || !!themeImageUploadId || imageRemoved;
 
  const cardSx = { p: 2 } as const;
 
  return (
    <Stack spacing={2} data-testid="event-info-tab">
      {/* Two columns (per prototype #p-details): identity on the left, topic + when/where on the right. */}
      <Box
        sx={{
          display: 'grid',
          gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' },
          gap: 2,
          alignItems: 'start',
        }}
      >
        {/* LEFT column */}
        <Stack spacing={2}>
          {/* Theme image card — full-width banner with overlay actions */}
          <Paper variant="outlined" sx={{ overflow: 'hidden' }}>
            <Box sx={{ position: 'relative' }} data-testid="info-theme-image">
              {shownImage ? (
                <Box
                  component="img"
                  src={shownImage}
                  alt={t('form.themeImageAlt', 'Event theme image')}
                  sx={{ width: '100%', height: 180, objectFit: 'cover', display: 'block' }}
                />
              ) : (
                <Box
                  onClick={() => fileInputRef.current?.click()}
                  sx={{
                    width: '100%',
                    height: 180,
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                    bgcolor: 'action.hover',
                    cursor: 'pointer',
                  }}
                >
                  <Typography variant="body2" color="text.secondary">
                    {isUploading
                      ? t('eventPage.details.uploadingImage', 'Uploading…')
                      : t('eventPage.details.uploadImage', 'Click to upload a theme image')}
                  </Typography>
                </Box>
              )}
 
              {/* Top-right: replace */}
              <Tooltip title={t('eventPage.details.replaceImage', 'Replace image')}>
                <span style={{ position: 'absolute', top: 8, right: 8 }}>
                  <IconButton
                    size="small"
                    sx={{ bgcolor: 'background.paper', '&:hover': { bgcolor: 'background.paper' } }}
                    onClick={() => fileInputRef.current?.click()}
                    disabled={isUploading}
                    data-testid="info-replace-theme"
                  >
                    <ReplaceIcon fontSize="small" />
                  </IconButton>
                </span>
              </Tooltip>
 
              {/* Bottom-left: AI-generate */}
              {aiContentEnabled && (
                <Tooltip title={t('eventPage.details.aiGenerateImage', '✨ Generate (AI)')}>
                  <IconButton
                    size="small"
                    sx={{
                      position: 'absolute',
                      bottom: 8,
                      left: 8,
                      bgcolor: 'background.paper',
                      '&:hover': { bgcolor: 'background.paper' },
                    }}
                    onClick={() => setAiDrawerOpen(true)}
                    data-testid="info-ai-theme"
                  >
                    <AutoAwesome fontSize="small" />
                  </IconButton>
                </Tooltip>
              )}
 
              {/* Bottom-right: delete badge */}
              {shownImage && (
                <Tooltip title={t('form.removeThemeImage', 'Remove image')}>
                  <IconButton
                    size="small"
                    color="error"
                    sx={{
                      position: 'absolute',
                      bottom: 8,
                      right: 8,
                      bgcolor: 'background.paper',
                      '&:hover': { bgcolor: 'background.paper' },
                    }}
                    onClick={() => {
                      setImageRemoved(true);
                      setPreviewUrl(null);
                      setThemeImageUploadId('');
                      setSaved(false);
                    }}
                    data-testid="info-delete-theme"
                  >
                    <DeleteIcon fontSize="small" />
                  </IconButton>
                </Tooltip>
              )}
 
              <input
                ref={fileInputRef}
                type="file"
                hidden
                accept="image/png,image/jpeg,image/svg+xml"
                onChange={(e) => {
                  const file = e.target.files?.[0];
                  if (file) void uploadFile(file);
                  e.target.value = '';
                }}
              />
            </Box>
          </Paper>
 
          {/* Event details card */}
          <Paper variant="outlined" sx={cardSx}>
            <Typography variant="subtitle1" gutterBottom>
              {t('eventPage.details.heading', 'Event details')}
            </Typography>
            <Stack spacing={2}>
              <Controller
                name="title"
                control={control}
                render={({ field }) => (
                  <TextField
                    {...field}
                    label={t('form.title', 'Title')}
                    fullWidth
                    error={!!errors.title}
                    helperText={errors.title?.message}
                    inputProps={{ 'data-testid': 'info-title-field' }}
                  />
                )}
              />
              <Controller
                name="description"
                control={control}
                render={({ field }) => (
                  <TextField
                    {...field}
                    label={t('form.description', 'Description')}
                    fullWidth
                    multiline
                    minRows={4}
                    error={!!errors.description}
                    helperText={errors.description?.message}
                    inputProps={{ 'data-testid': 'info-description-field' }}
                  />
                )}
              />
              {aiContentEnabled && (
                <Box>
                  <Button
                    size="small"
                    startIcon={<AutoAwesome />}
                    onClick={() => setAiDrawerOpen(true)}
                    data-testid="info-ai-assist"
                  >
                    {t(
                      'eventPage.details.aiGenerateDescription',
                      '✨ Generate description with AI'
                    )}
                  </Button>
                </Box>
              )}
            </Stack>
          </Paper>
        </Stack>
 
        {/* RIGHT column */}
        <Stack spacing={2}>
          {/* Topic card */}
          <Paper variant="outlined" sx={cardSx}>
            <Typography variant="subtitle1" gutterBottom>
              {t('eventPage.overview.selectedTopic', 'Selected Topic')}
            </Typography>
            <Stack direction="row" spacing={1} alignItems="center" flexWrap="wrap">
              {topic || event.topicCode ? (
                <Chip
                  icon={<TopicIcon />}
                  label={topic?.title ?? event.topicCode}
                  color="primary"
                  variant="outlined"
                />
              ) : (
                <Typography variant="body2" color="text.secondary">
                  {t('eventPage.details.noTopic', 'No topic selected yet')}
                </Typography>
              )}
              <Button
                size="small"
                variant="outlined"
                onClick={() => setTopicOverlayOpen(true)}
                data-testid="info-change-topic"
              >
                {t('eventPage.details.changeTopic', 'Change topic')}
              </Button>
            </Stack>
          </Paper>
 
          {/* When & where card */}
          <Paper variant="outlined" sx={cardSx}>
            <Typography variant="subtitle1" gutterBottom>
              {t('eventPage.details.whenWhere', 'When & where')}
            </Typography>
            <Stack spacing={2}>
              {/* Date + registration deadline on one line */}
              <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
                <Controller
                  name="date"
                  control={control}
                  render={({ field }) => (
                    <TextField
                      {...field}
                      type="date"
                      label={t('form.eventDate', 'Event date')}
                      InputLabelProps={{ shrink: true }}
                      error={!!errors.date}
                      helperText={errors.date?.message}
                      fullWidth
                      inputProps={{ 'data-testid': 'info-date-field' }}
                    />
                  )}
                />
                <Controller
                  name="registrationDeadline"
                  control={control}
                  render={({ field }) => (
                    <TextField
                      {...field}
                      type="date"
                      label={t('form.registrationDeadline', 'Registration deadline')}
                      InputLabelProps={{ shrink: true }}
                      error={!!errors.registrationDeadline}
                      helperText={errors.registrationDeadline?.message}
                      fullWidth
                      inputProps={{ 'data-testid': 'info-deadline-field' }}
                    />
                  )}
                />
              </Stack>
 
              {/* Event type + capacity on one line */}
              <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2} alignItems="flex-start">
                <Box sx={{ flex: 1, width: '100%' }}>
                  <Controller
                    name="eventType"
                    control={control}
                    render={({ field }) => (
                      <EventTypeSelector
                        value={normalizeEventType(field.value)}
                        onChange={field.onChange}
                      />
                    )}
                  />
                </Box>
                <Controller
                  name="venueCapacity"
                  control={control}
                  render={({ field }) => (
                    <TextField
                      {...field}
                      type="number"
                      label={t('form.capacity', 'Venue capacity')}
                      error={!!errors.venueCapacity}
                      helperText={errors.venueCapacity?.message}
                      fullWidth
                      inputProps={{ 'data-testid': 'info-capacity-field' }}
                    />
                  )}
                />
              </Stack>
 
              <Controller
                name="venueName"
                control={control}
                render={({ field }) => (
                  <TextField
                    {...field}
                    label={t('form.venue', 'Venue name')}
                    error={!!errors.venueName}
                    helperText={errors.venueName?.message}
                    fullWidth
                    inputProps={{ 'data-testid': 'info-venue-name-field' }}
                  />
                )}
              />
              <Controller
                name="venueAddress"
                control={control}
                render={({ field }) => (
                  <TextField
                    {...field}
                    label={t('form.venueAddress', 'Venue address')}
                    error={!!errors.venueAddress}
                    helperText={errors.venueAddress?.message}
                    fullWidth
                    inputProps={{ 'data-testid': 'info-venue-address-field' }}
                  />
                )}
              />
            </Stack>
          </Paper>
        </Stack>
      </Box>
 
      {saveError && (
        <Alert severity="error" data-testid="info-save-error">
          {saveError}
        </Alert>
      )}
      {saved && (
        <Alert severity="success" onClose={() => setSaved(false)} data-testid="info-saved">
          {t('eventPage.details.saved', 'Changes saved')}
        </Alert>
      )}
 
      <Box>
        <Button
          variant="contained"
          onClick={onSave}
          disabled={!dirty || updateEvent.isPending}
          startIcon={updateEvent.isPending ? <CircularProgress size={16} /> : undefined}
          data-testid="info-save-button"
        >
          {t('eventPage.details.save', 'Save changes')}
        </Button>
      </Box>
 
      <AiAssistDrawer
        eventCode={eventCode}
        open={aiDrawerOpen}
        onClose={() => setAiDrawerOpen(false)}
        onDescriptionGenerated={(text: string) =>
          setValue('description', text, { shouldDirty: true })
        }
        onImageGenerated={() => setAiDrawerOpen(false)}
      />
 
      <TopicSelectionOverlay
        open={topicOverlayOpen}
        eventCode={eventCode}
        currentTopicCode={event.topicCode ?? undefined}
        onClose={() => setTopicOverlayOpen(false)}
      />
    </Stack>
  );
};
 
export default EventInfoTab;