Files
nahodka-front/src/components/PostDetailModal.jsx
2026-10-09 01:21:14 +03:00

74 lines
2.3 KiB
JavaScript

import { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { API_URL } from "../config/authConstants";
import { apiFetch } from "../api/apiClient";
import { useAuthContext } from "../context/AuthContext";
import PostOnFeed from "../pages/PostOnFeed";
import CloseIcon from "./icons/CloseIcon";
export default function PostDetailModal({ postId, onClose, onLocationClick, onAuthRequired }) {
const { t } = useTranslation();
const { user } = useAuthContext();
const [post, setPost] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
if (!postId) return;
let cancelled = false;
const fetchPost = async () => {
setLoading(true);
setError(null);
try {
const res = await apiFetch(`${API_URL}/api/v1/posts/detail/${postId}`);
if (!res.ok) throw new Error("Failed to load post");
const data = await res.json();
if (!cancelled) {
setPost({ ...data, type: data.type ? data.type.toLowerCase() : "loss" });
}
} catch (e) {
if (!cancelled) setError(e.message);
} finally {
if (!cancelled) setLoading(false);
}
};
fetchPost();
return () => { cancelled = true; };
}, [postId]);
const isOwner = post && user && (post.authorId === user.id || post.userId === user.id);
return (
<div className="f-modal-bg" onClick={(e) => e.target === e.currentTarget && onClose()}>
<button type="button" className="f-close-btn f-post-detail-close" onClick={onClose} aria-label={t("common.close")}>
<CloseIcon />
</button>
<div className="f-modal f-post-detail-modal" onClick={(e) => e.stopPropagation()}>
{loading && (
<div className="f-post-detail-status">
<div className="f-spinner" />
</div>
)}
{error && (
<div className="f-post-detail-status f-post-detail-error">
{t("common.error")}
</div>
)}
{post && (
<PostOnFeed
ad={post}
isOwner={isOwner}
onLocationClick={onLocationClick}
onAuthRequired={onAuthRequired}
compact
/>
)}
</div>
</div>
);
}