import React, { useState } from 'react'; import { ArrowUp, CheckCircle2 } from 'lucide-react'; import { Answer } from '../types'; import { useAuth } from '../context/AuthContext'; import { forumService } from '../services/forum'; import MarkdownContent from './MarkdownContent'; interface Props { answer: Answer; isQuestionAuthor: boolean; onAccept?: () => void; } const AnswerCard: React.FC = ({ answer, isQuestionAuthor, onAccept }) => { const [votes, setVotes] = useState(answer.votes); const [hasVoted, setHasVoted] = useState(answer.userVote === 1); const [isVoting, setIsVoting] = useState(false); const [isAccepted, setIsAccepted] = useState(answer.isAccepted); const { isAuthenticated } = useAuth(); const handleVote = async () => { if (!isAuthenticated) return; if (isVoting) return; setIsVoting(true); try { const result = await forumService.voteAnswer(answer.id, 1); setVotes(result.votes); setHasVoted(!hasVoted); } catch (err) { console.error('Failed to vote:', err); } finally { setIsVoting(false); } }; const handleAccept = async () => { if (!isQuestionAuthor) return; try { await forumService.acceptAnswer(answer.id); setIsAccepted(true); if (onAccept) onAccept(); } catch (err) { console.error('Failed to accept answer:', err); } }; return (
{/* Vote Section */}
{votes} {isAccepted && (
)}
{/* Content */}
Répondu le {answer.createdAt} {isQuestionAuthor && !isAccepted && ( )}
{answer.author.name} {answer.author.name} {answer.author.role === 'MENTOR' ? 'Mentor' : 'Étudiant'}
); }; export default AnswerCard;