This repository has been archived on 2026-08-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
onlypets/frontend/app/components/PostCard/PostCard.tsx
T

243 lines
5.8 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import CommentModal from '../CommentModal/CommentModal';
type Comment = {
id: number;
author: string;
text: string;
};
type Media = {
type: 'image' | 'video';
url: string;
aspect: '1:1' | '9:16';
};
type PostCardProps = {
petName: string;
petType: 'Hund' | 'Katze';
ownerName?: string;
text: string;
createdAt: string;
media?: Media;
likes: number;
likedByCurrentUser?: boolean;
commentsCount: number;
commentsList: Comment[];
};
// 🔐 Simulierter eingeloggter User
const CURRENT_USER_ID = 1;
export default function PostCard({
petName,
petType,
ownerName,
text,
createdAt,
media,
likes,
likedByCurrentUser,
commentsCount,
commentsList,
}: PostCardProps) {
const [showComments, setShowComments] = useState(false);
const [showMediaFullscreen, setShowMediaFullscreen] = useState(false);
const [localLikes, setLocalLikes] = useState(likes);
const [liked, setLiked] = useState(!!likedByCurrentUser);
const isSquare = media?.aspect === '1:1';
// ⛔ Scroll sperren bei Vollbild
useEffect(() => {
if (showMediaFullscreen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
}, [showMediaFullscreen]);
return (
<>
<article
style={{
border: '1px solid #e5e5e5',
borderRadius: '16px',
backgroundColor: 'white',
overflow: 'hidden',
}}
>
{/* HEADER */}
<header style={{ padding: '1rem', display: 'grid', gap: '0.2rem' }}>
<div style={{ fontWeight: 800, fontSize: '1.1rem' }}>
{petName}{' '}
<span style={{ fontWeight: 500, color: '#666' }}>
({petType})
</span>
</div>
<div style={{ fontSize: '0.8rem', color: '#777' }}>
{createdAt}
{ownerName && (
<>
{' · '}
<span style={{ fontStyle: 'italic' }}>
verwaltet von {ownerName}
</span>
</>
)}
</div>
</header>
{/* MEDIA */}
{media && (
<div
onClick={() => setShowMediaFullscreen(true)}
style={{
width: '100%',
aspectRatio: isSquare ? '1 / 1' : '9 / 16',
backgroundColor: '#000',
cursor: 'pointer',
}}
>
{media.type === 'image' && (
<img
src={media.url}
alt={petName}
style={{
width: '100%',
height: '100%',
objectFit: 'cover',
}}
/>
)}
{media.type === 'video' && (
<video
src={media.url}
muted
style={{
width: '100%',
height: '100%',
objectFit: 'cover',
}}
/>
)}
</div>
)}
{/* TEXT */}
<div style={{ padding: '1rem', fontSize: '0.95rem' }}>
{text}
</div>
{/* FOOTER */}
<footer
style={{
padding: '0 1rem 1rem',
display: 'flex',
gap: '1.2rem',
fontSize: '0.85rem',
}}
>
<button
type="button"
onClick={() => {
setLiked(!liked);
setLocalLikes((prev) => (liked ? prev - 1 : prev + 1));
console.log('LIKE', {
byUser: CURRENT_USER_ID,
pet: petName,
});
}}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
fontWeight: 600,
color: liked ? '#d32f2f' : '#555',
}}
>
❤️ {localLikes}
</button>
<button
type="button"
onClick={() => setShowComments(true)}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
fontWeight: 600,
color: '#2E7D32',
}}
>
💬 {commentsCount} Kommentare
</button>
</footer>
</article>
{/* 💬 Kommentare */}
{showComments && (
<CommentModal
comments={commentsList}
onClose={() => setShowComments(false)}
/>
)}
{/* 🖼️ MEDIA FULLSCREEN */}
{showMediaFullscreen && media && (
<div
onClick={() => setShowMediaFullscreen(false)}
style={{
position: 'fixed',
inset: 0,
backgroundColor: 'rgba(0,0,0,0.9)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
zIndex: 2000,
cursor: 'zoom-out',
}}
>
<div
onClick={(e) => e.stopPropagation()}
style={{
maxWidth: '95vw',
maxHeight: '95vh',
}}
>
{media.type === 'image' && (
<img
src={media.url}
alt={petName}
style={{
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
}}
/>
)}
{media.type === 'video' && (
<video
src={media.url}
controls
autoPlay
style={{
maxWidth: '100%',
maxHeight: '100%',
}}
/>
)}
</div>
</div>
)}
</>
);
}