Files
nahodka-front/src/components/PostDetailModal.jsx
SlimusMinus 33bd002f98 Show which listing each chat is about
The chat list and header show the listing's type, city and closed/deleted
status; the header info button opens the full listing in PostDetailModal
with a "show on map" action. Deleted or unpublished listings are marked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 21:17:58 +03:00

105 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
/**
* Модалка с полным объявлением, догружаемым по id.
*
* compact — компактная карточка (без адреса и даты), как при открытии с карты;
* hideContact — без кнопки «Написать» (модалка открыта из самого чата);
* fallbackTitle — что показать над сообщением, если объявление удалено или скрыто;
* footer(post) — дополнительные действия под карточкой.
*
* @param {{
* postId: any, onClose: () => void,
* onLocationClick?: (ad: any) => void, onAuthRequired?: () => void,
* compact?: boolean, hideContact?: boolean,
* fallbackTitle?: string, footer?: (post: any) => any,
* }} props
*/
export default function PostDetailModal({
postId, onClose, onLocationClick, onAuthRequired,
compact = true, hideContact = false, fallbackTitle, footer,
}) {
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/${encodeURIComponent(postId)}`);
// 404 — объявление удалено или снято с публикации (модерация, отклонено)
if (res.status === 404) throw new Error("notFound");
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 === "notFound" && (
<div className="f-post-detail-status f-post-detail-gone" role="status">
{fallbackTitle && <div className="f-post-detail-gone-title">{fallbackTitle}</div>}
<div className="f-post-detail-gone-text">{t("chat.postUnavailable")}</div>
</div>
)}
{error && error !== "notFound" && (
<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={compact}
hideContact={hideContact}
/>
{footer?.(post)}
</>
)}
</div>
</div>
);
}