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>
This commit is contained in:
@@ -6,7 +6,25 @@ import { useAuthContext } from "../context/AuthContext";
|
||||
import PostOnFeed from "../pages/PostOnFeed";
|
||||
import CloseIcon from "./icons/CloseIcon";
|
||||
|
||||
export default function PostDetailModal({ postId, onClose, onLocationClick, onAuthRequired }) {
|
||||
/**
|
||||
* Модалка с полным объявлением, догружаемым по 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);
|
||||
@@ -22,6 +40,8 @@ export default function PostDetailModal({ postId, onClose, onLocationClick, onAu
|
||||
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) {
|
||||
@@ -52,20 +72,31 @@ export default function PostDetailModal({ postId, onClose, onLocationClick, onAu
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
{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
|
||||
/>
|
||||
<>
|
||||
<PostOnFeed
|
||||
ad={post}
|
||||
isOwner={isOwner}
|
||||
onLocationClick={onLocationClick}
|
||||
onAuthRequired={onAuthRequired}
|
||||
compact={compact}
|
||||
hideContact={hideContact}
|
||||
/>
|
||||
{footer?.(post)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,14 +1,45 @@
|
||||
export default function ChatHeader({ title, onBack, avatar }) {
|
||||
/**
|
||||
* Шапка чата. С onInfo аватар и заголовок объявления кликабельны, а справа
|
||||
* есть кнопка «ⓘ» — обе открывают карточку объявления, к которому относится чат.
|
||||
*/
|
||||
export default function ChatHeader({ title, onBack, avatar, subtitle, onInfo, infoLabel }) {
|
||||
const main = (
|
||||
<>
|
||||
{avatar !== undefined && (
|
||||
<div className="chat-header-avatar">{avatar}</div>
|
||||
)}
|
||||
<div className="chat-header-text">
|
||||
<div className="chat-header-title">{title}</div>
|
||||
{subtitle}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="chat-header">
|
||||
<button className="chat-back-btn" onClick={onBack}>
|
||||
←
|
||||
</button>
|
||||
{avatar !== undefined && (
|
||||
<div className="chat-header-avatar">{avatar}</div>
|
||||
{onInfo ? (
|
||||
<button type="button" className="chat-header-post" onClick={onInfo} title={infoLabel}>
|
||||
{main}
|
||||
</button>
|
||||
) : (
|
||||
<div className="chat-header-post">{main}</div>
|
||||
)}
|
||||
{onInfo ? (
|
||||
<button type="button" className="chat-header-info-btn" onClick={onInfo}
|
||||
aria-label={infoLabel} title={infoLabel}>
|
||||
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||||
strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
||||
<circle cx="12" cy="12" r="10" />
|
||||
<line x1="12" y1="16" x2="12" y2="11" />
|
||||
<line x1="12" y1="8" x2="12.01" y2="8" />
|
||||
</svg>
|
||||
</button>
|
||||
) : (
|
||||
<div />
|
||||
)}
|
||||
<div className="chat-header-title">{title}</div>
|
||||
<div />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { getChatDisplayInfo } from "../../utils/chatDisplay";
|
||||
import TrashIcon from "../icons/TrashIcon";
|
||||
import ChatPostMeta from "./ChatPostMeta";
|
||||
|
||||
export default function ChatListItem({ chat, post, unreadMap, t, onOpen, onDelete }) {
|
||||
const { title, imgSrc, emoji, unreadCount, preview } = getChatDisplayInfo(chat, post, unreadMap, t);
|
||||
@@ -18,6 +19,7 @@ export default function ChatListItem({ chat, post, unreadMap, t, onOpen, onDelet
|
||||
</div>
|
||||
<div className="chat-list-info">
|
||||
<div className="chat-list-name">{title}</div>
|
||||
<ChatPostMeta post={post} t={t} />
|
||||
<div className="chat-list-preview">
|
||||
{preview}
|
||||
</div>
|
||||
|
||||
38
src/components/chat/ChatPostInfo.test.jsx
Normal file
38
src/components/chat/ChatPostInfo.test.jsx
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* По какому объявлению чат: подпись в списке/шапке (тип, город, статус)
|
||||
* и кнопка в шапке, открывающая карточку. Удалённое объявление помечается.
|
||||
*/
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import i18n from "../../i18n";
|
||||
import ChatPostMeta from "./ChatPostMeta";
|
||||
import ChatHeader from "./ChatHeader";
|
||||
import { cachePostsFromChats, getPostForChat } from "../../stores/chatPostStore";
|
||||
|
||||
const t = i18n.t.bind(i18n);
|
||||
|
||||
beforeAll(() => i18n.changeLanguage("ru"));
|
||||
|
||||
test("подпись показывает тип, город и закрытый статус", () => {
|
||||
render(<ChatPostMeta post={{ type: "FOUND", status: "CLOSED", city: "Москва" }} t={t} />);
|
||||
expect(screen.getByText("Находка")).toBeTruthy();
|
||||
expect(screen.getByText("Москва")).toBeTruthy();
|
||||
expect(screen.getByText("Закрыто")).toBeTruthy();
|
||||
});
|
||||
|
||||
test("удалённое объявление сохраняет прежний заголовок и помечается", () => {
|
||||
cachePostsFromChats([{ id: 501, postId: 9, post: { id: 9, title: "Шляпа", images: [] } }]);
|
||||
cachePostsFromChats([{ id: 501, postId: 9, post: null }]);
|
||||
|
||||
const post = getPostForChat(501);
|
||||
expect(post).toMatchObject({ id: 9, title: "Шляпа", removed: true });
|
||||
|
||||
render(<ChatPostMeta post={post} t={t} />);
|
||||
expect(screen.getByText("Объявление удалено")).toBeTruthy();
|
||||
});
|
||||
|
||||
test("кнопка в шапке открывает объявление", () => {
|
||||
const onInfo = jest.fn();
|
||||
render(<ChatHeader title="Шляпа" onBack={() => {}} onInfo={onInfo} infoLabel="Объявление этого чата" />);
|
||||
fireEvent.click(screen.getByLabelText("Объявление этого чата"));
|
||||
expect(onInfo).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
34
src/components/chat/ChatPostMeta.jsx
Normal file
34
src/components/chat/ChatPostMeta.jsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import { POST_STATUS_CLOSED } from "../../config/postConstants";
|
||||
|
||||
/**
|
||||
* Короткая подпись объявления, к которому относится чат: тип, город, статус.
|
||||
* Помогает различать чаты по похожим объявлениям (три «шляпы» в списке).
|
||||
* Тип и статус бэкенд отдаёт в верхнем регистре (FOUND, CLOSED).
|
||||
*/
|
||||
export default function ChatPostMeta({ post, t, className = "" }) {
|
||||
if (!post) return null;
|
||||
|
||||
if (post.removed) {
|
||||
return (
|
||||
<div className={`chat-post-meta ${className}`}>
|
||||
<span className="chat-post-tag chat-post-tag--removed">{t("chat.postRemoved")}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const type = String(post.type || "").toLowerCase();
|
||||
const isClosed = String(post.status || "").toUpperCase() === POST_STATUS_CLOSED;
|
||||
if (!type && !post.city && !isClosed) return null;
|
||||
|
||||
return (
|
||||
<div className={`chat-post-meta ${className}`}>
|
||||
{type && (
|
||||
<span className={`chat-post-tag chat-post-tag--${type === "found" ? "found" : "loss"}`}>
|
||||
{type === "found" ? t("postCard.found") : t("postCard.loss")}
|
||||
</span>
|
||||
)}
|
||||
{post.city && <span className="chat-post-city">{post.city}</span>}
|
||||
{isClosed && <span className="chat-post-tag chat-post-tag--closed">{t("chat.postClosed")}</span>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user