import { useEffect, useRef, useState } from 'react'; import { BarChart3, Check, LockKeyhole, Users } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Checkbox } from '@/components/ui/checkbox'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import ConfirmDialog from '@/components/ConfirmDialog'; import { notesApi } from '@/api/notes'; import { personLabel } from '@/components/planning/utils'; export default function PollCard({ note, onChange }) { const poll = note.poll; const [selected, setSelected] = useState(poll?.my_option_ids || []); const [busy, setBusy] = useState(false); const pending = useRef(false); const [error, setError] = useState(''); const [notice, setNotice] = useState(''); const ownKey = JSON.stringify(poll?.my_option_ids || []); useEffect(() => { setSelected(JSON.parse(ownKey)); }, [ownKey]); if (!poll) return

Les données du sondage sont indisponibles.

; const required = poll.vote_mode === 'single' ? 1 : poll.vote_mode === 'exact' ? poll.exact_choices : null; const closed = poll.is_closed || Boolean(poll.closes_at && new Date(poll.closes_at) <= new Date()); const canVote = poll.can_vote && !closed; const valid = required === null ? selected.length > 0 : selected.length === required; const changed = [...selected].sort((a, b) => a - b).join(',') !== [...poll.my_option_ids].sort((a, b) => a - b).join(','); const toggle = (id) => { setError(''); setNotice(''); setSelected((current) => poll.vote_mode === 'single' ? [id] : current.includes(id) ? current.filter((item) => item !== id) : [...current, id]); }; const mutate = async (action) => { if (pending.current) return; pending.current = true; setBusy(true); setError(''); setNotice(''); try { const updated = action === 'vote' ? await notesApi.vote(note.id, selected) : await notesApi.closePoll(note.id); onChange(updated); setNotice(action === 'vote' ? 'Ton vote est enregistré.' : 'Sondage clôturé.'); } catch (e) { setError(e.message); } finally { pending.current = false; setBusy(false); } }; return
{poll.is_anonymous ? : }{poll.is_anonymous ? 'Anonyme' : 'Noms visibles'}{closed ? 'Clôturé' : required === 1 ? '1 choix' : required ? `${required} choix exactement` : 'Choix multiples'}
{(() => { const choices = poll.options.map((option) =>
); return poll.vote_mode === 'single' ? toggle(Number(value))}>{choices} :
{choices}
; })()}

{poll.total_voters} participant{poll.total_voters > 1 ? 's' : ''} · part des votants ayant choisi chaque réponse

{poll.closes_at &&

Clôture : {new Date(poll.closes_at).toLocaleString('fr-FR')}

}{canVote &&

{required ? `${selected.length} / ${required} choix sélectionné(s).` : `${selected.length} choix sélectionné(s).`} Tu peux modifier ton vote jusqu’à la clôture.

}{!canVote && !closed &&

Ce sondage s’adresse à d’autres personnes. Tu peux consulter les résultats.

}
{error &&

{error}

}{notice &&

{notice}

}
{canVote && }{note.can_edit && !closed && mutate('close')}>}
; }