All files / src/components/attendee SpeakerSelfNominatePanel.tsx

100% Statements 35/35
86.66% Branches 26/30
100% Functions 9/9
100% Lines 33/33

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                                                                              3x     3x 3x 3x               6x                         3x       235x 235x 235x 235x 235x 235x   235x 4x     1x 1x 1x           3x 1x           235x 1x     234x   235x 235x 235x   235x                                     6x 6x 6x                                                   4x 4x                     96x                         122x                                                    
/**
 * SpeakerSelfNominatePanel — Story 7.2 "I Could Speak on That".
 *
 * An "I could speak on that" button on an upcoming-event card. Visible only to logged-in users
 * (the caller also gates on topic-set + published). Clicking opens a small form (title + abstract)
 * that self-nominates the attendee as a speaker FOR THAT event — the card supplies the eventCode,
 * so no event picker is needed and multiple simultaneous open events each get their own button.
 *
 * Tailwind-only by design: the upcoming-event card lives on the public (no-MUI) homepage. The form
 * lives in a Radix Dialog (rendered in a portal) so its clicks don't bubble to the card's <Link>;
 * the trigger button stops propagation + prevents default for the same reason.
 *
 * Identity (name + company) is auto-filled server-side from the attendee's profile — the body
 * carries only the proposed talk. One self-nomination per attendee per event (the backend rejects
 * a repeat with 409 `DUPLICATE_SELF_NOMINATION`); we render the already-done state on success or on
 * that duplicate error. The backend reuses 409 for a second condition — `SELF_NOMINATION_NOT_ALLOWED`
 * (topic unset / event unpublished, e.g. a race after the card rendered) — so we key on
 * `details.code`, NOT the bare status, and surface the generic error for the not-allowed case.
 */
 
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useMutation } from '@tanstack/react-query';
import { Mic } from 'lucide-react';
import { isAxiosError } from 'axios';
import { useAuth } from '@/hooks/useAuth/useAuth';
import { Button } from '@/components/public/ui/button';
import { Input } from '@/components/public/ui/input';
import { Textarea } from '@/components/public/ui/textarea';
import { Label } from '@/components/public/ui/label';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
} from '@/components/public/ui/dialog';
import { selfNominateSpeaker } from '@/services/api/speakerNominationApi';
 
const MIN_TITLE = 5;
// 200 to match the canonical session content title — the pitch is carried verbatim into the
// session at promote (ADR-012 / Story 7.2), so it must fit session_content_history.title.
const MAX_TITLE = 200;
const MIN_ABSTRACT = 10;
const MAX_ABSTRACT = 5000;
 
/**
 * A 409 from the backend is ambiguous by status alone: `DUPLICATE_SELF_NOMINATION` (already
 * nominated → already-done) vs `SELF_NOMINATION_NOT_ALLOWED` (topic unset / unpublished → a
 * real, transient error). Key on `details.code` so we only flip to the permanent already-done
 * state for an actual duplicate.
 */
const isDuplicateSelfNomination = (err: unknown): boolean =>
  isAxiosError(err) &&
  err.response?.status === 409 &&
  (err.response.data as { details?: { code?: string } } | undefined)?.details?.code ===
    'DUPLICATE_SELF_NOMINATION';
 
interface SpeakerSelfNominatePanelProps {
  /** The event this nomination targets (supplied by the card). */
  eventCode: string;
  /** Optional topic name shown in the dialog for context. */
  topicName?: string;
}
 
export const SpeakerSelfNominatePanel = ({
  eventCode,
  topicName,
}: SpeakerSelfNominatePanelProps) => {
  const { t } = useTranslation('common');
  const { isAuthenticated } = useAuth();
  const [open, setOpen] = useState(false);
  const [title, setTitle] = useState('');
  const [abstract, setAbstract] = useState('');
  const [done, setDone] = useState(false);
 
  const mutation = useMutation({
    mutationFn: () =>
      selfNominateSpeaker(eventCode, { sessionTitle: title.trim(), abstract: abstract.trim() }),
    onSuccess: () => {
      setDone(true);
      setTitle('');
      setAbstract('');
    },
    onError: (err) => {
      // Only a DUPLICATE_SELF_NOMINATION 409 means already-nominated → already-done. A
      // SELF_NOMINATION_NOT_ALLOWED 409 (topic unset / unpublished) is a real error and falls
      // through to the generic error message.
      if (isDuplicateSelfNomination(err)) {
        setDone(true);
      }
    },
  });
 
  // Login gate: the button is for logged-in attendees only.
  if (!isAuthenticated) {
    return null;
  }
 
  const titleValid = title.trim().length >= MIN_TITLE && title.trim().length <= MAX_TITLE;
  const abstractValid =
    abstract.trim().length >= MIN_ABSTRACT && abstract.trim().length <= MAX_ABSTRACT;
  const canSubmit = titleValid && abstractValid && !mutation.isPending;
  const isDuplicateError = mutation.isError && isDuplicateSelfNomination(mutation.error);
 
  return (
    <div data-testid={`self-nominate-panel-${eventCode}`}>
      {done ? (
        <p
          className="text-sm text-green-400 flex items-center gap-2"
          role="status"
          data-testid="self-nominate-done"
        >
          <Mic className="h-4 w-4" />
          {t('attendee.selfNominate.alreadyDone')}
        </p>
      ) : (
        <Button
          type="button"
          variant="outline"
          className="w-full"
          data-testid="self-nominate-button"
          onClick={(e) => {
            // The card is a <Link> — don't navigate when opening the form.
            e.preventDefault();
            e.stopPropagation();
            setOpen(true);
          }}
        >
          <Mic className="h-4 w-4 mr-2" />
          {t('attendee.selfNominate.button')}
        </Button>
      )}
 
      <Dialog open={open} onOpenChange={setOpen}>
        <DialogContent data-testid="self-nominate-dialog">
          <DialogHeader>
            <DialogTitle>{t('attendee.selfNominate.heading')}</DialogTitle>
            <DialogDescription>
              {topicName
                ? t('attendee.selfNominate.introWithTopic', { topic: topicName })
                : t('attendee.selfNominate.intro')}
            </DialogDescription>
          </DialogHeader>
 
          {done ? (
            <p className="text-sm text-green-400" role="status" data-testid="self-nominate-success">
              {t('attendee.selfNominate.success')}
            </p>
          ) : (
            <form
              onSubmit={(e) => {
                e.preventDefault();
                Eif (canSubmit) mutation.mutate();
              }}
              className="space-y-4"
            >
              <div className="space-y-1.5">
                <Label htmlFor="self-nominate-title">{t('attendee.selfNominate.titleLabel')}</Label>
                <Input
                  id="self-nominate-title"
                  data-testid="self-nominate-title"
                  value={title}
                  maxLength={MAX_TITLE}
                  onChange={(e) => setTitle(e.target.value)}
                  placeholder={t('attendee.selfNominate.titlePlaceholder')}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor="self-nominate-abstract">
                  {t('attendee.selfNominate.abstractLabel')}
                </Label>
                <Textarea
                  id="self-nominate-abstract"
                  data-testid="self-nominate-abstract"
                  value={abstract}
                  maxLength={MAX_ABSTRACT}
                  onChange={(e) => setAbstract(e.target.value)}
                  placeholder={t('attendee.selfNominate.abstractPlaceholder')}
                  rows={4}
                />
              </div>
              {mutation.isError && (
                <p className="text-sm text-red-400" role="alert" data-testid="self-nominate-error">
                  {isDuplicateError
                    ? t('attendee.selfNominate.alreadyDone')
                    : t('attendee.selfNominate.error')}
                </p>
              )}
              <Button type="submit" disabled={!canSubmit} data-testid="self-nominate-submit">
                {mutation.isPending
                  ? t('attendee.selfNominate.submitting')
                  : t('attendee.selfNominate.submit')}
              </Button>
            </form>
          )}
        </DialogContent>
      </Dialog>
    </div>
  );
};
 
export default SpeakerSelfNominatePanel;