Новые изменения
This commit is contained in:
parent
6f727aae7b
commit
b5b31f8a88
9 changed files with 600 additions and 166 deletions
|
|
@ -20,7 +20,8 @@ interface ApiComment {
|
|||
expand?: {
|
||||
user?: {
|
||||
id: string;
|
||||
firstName: string;
|
||||
firstName?: string;
|
||||
name?: string;
|
||||
email: string;
|
||||
avatar?: string;
|
||||
};
|
||||
|
|
@ -35,6 +36,7 @@ interface ToastMessage {
|
|||
export default function Comments(props: CommentsProps) {
|
||||
const [isAuthenticated, setIsAuthenticated] = createSignal(false);
|
||||
const [currentUser, setCurrentUser] = createSignal<{
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
avatar?: string;
|
||||
|
|
@ -42,8 +44,8 @@ export default function Comments(props: CommentsProps) {
|
|||
const [isLoading, setIsLoading] = createSignal(true);
|
||||
const [comments, setComments] = createSignal<CommentWithReplies[]>([]);
|
||||
const [replyTo, setReplyTo] = createSignal<string | null>(null);
|
||||
const [commentAuthors, setCommentAuthors] = createSignal<Record<string, string>>({});
|
||||
const [toastVisible, setToastVisible] = createSignal<string | null>(null);
|
||||
const [editingComment, setEditingComment] = createSignal<string | null>(null);
|
||||
const [editContent, setEditContent] = createSignal("");
|
||||
const [toast, setToast] = createSignal<ToastMessage | null>(null);
|
||||
|
||||
const showToast = (message: ToastMessage): void => {
|
||||
|
|
@ -63,6 +65,7 @@ export default function Comments(props: CommentsProps) {
|
|||
if (data.authenticated && data.user) {
|
||||
setIsAuthenticated(true);
|
||||
setCurrentUser({
|
||||
id: data.user.id,
|
||||
name: data.user.name || "Пользователь",
|
||||
email: data.user.email,
|
||||
avatar: data.user.avatar,
|
||||
|
|
@ -91,14 +94,6 @@ export default function Comments(props: CommentsProps) {
|
|||
|
||||
const data = await response.json();
|
||||
|
||||
const authors: Record<string, string> = {};
|
||||
data.items.forEach((comment: ApiComment) => {
|
||||
if (comment.user) {
|
||||
authors[comment.id] = comment.user;
|
||||
}
|
||||
});
|
||||
setCommentAuthors(authors);
|
||||
|
||||
const commentsWithReplies: CommentWithReplies[] = await Promise.all(
|
||||
data.items.map(async (comment: ApiComment) => {
|
||||
const repliesResponse = await fetch(
|
||||
|
|
@ -149,6 +144,7 @@ export default function Comments(props: CommentsProps) {
|
|||
await loadComments();
|
||||
} catch (error) {
|
||||
console.error("[Comments] Ошибка создания комментария:", error);
|
||||
showToast({ type: "error", message: "Не удалось отправить комментарий" });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -183,33 +179,76 @@ export default function Comments(props: CommentsProps) {
|
|||
}
|
||||
};
|
||||
|
||||
const handleReplyClick = (commentId: string) => {
|
||||
const checkAuthAndReply = async () => {
|
||||
try {
|
||||
const response = await fetch("/api/auth/me", {
|
||||
credentials: "include",
|
||||
});
|
||||
const data = await response.json();
|
||||
const currentUserId = data.user?.id;
|
||||
const commentAuthorId = commentAuthors()[commentId];
|
||||
const handleEdit = async (commentId: string, data: { content: string }) => {
|
||||
try {
|
||||
const response = await fetch(`/api/comments/${commentId}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
content: data.content,
|
||||
}),
|
||||
});
|
||||
|
||||
if (commentAuthorId && currentUserId === commentAuthorId) {
|
||||
setToastVisible(commentId);
|
||||
setTimeout(() => setToastVisible(null), 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
setReplyTo(replyTo() === commentId ? null : commentId);
|
||||
} catch (error) {
|
||||
console.error("[handleReplyClick] Ошибка проверки автора:", error);
|
||||
setReplyTo(replyTo() === commentId ? null : commentId);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error || "Failed to update comment");
|
||||
}
|
||||
};
|
||||
|
||||
checkAuthAndReply();
|
||||
showToast({ type: "success", message: "Комментарий обновлен!" });
|
||||
await loadComments();
|
||||
setEditingComment(null);
|
||||
setEditContent("");
|
||||
} catch (error) {
|
||||
console.error("[Comments] Ошибка обновления комментария:", error);
|
||||
showToast({ type: "error", message: "Не удалось обновить комментарий" });
|
||||
}
|
||||
};
|
||||
|
||||
const Avatar = (props: { author: string; avatar?: string; size?: "sm" | "md" }) => {
|
||||
const handleDelete = async (commentId: string) => {
|
||||
try {
|
||||
const response = await fetch(`/api/comments/${commentId}`, {
|
||||
method: "DELETE",
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error || "Failed to delete comment");
|
||||
}
|
||||
|
||||
showToast({ type: "success", message: "Комментарий удален!" });
|
||||
await loadComments();
|
||||
setEditingComment(null);
|
||||
} catch (error) {
|
||||
console.error("[Comments] Ошибка удаления комментария:", error);
|
||||
showToast({ type: "error", message: "Не удалось удалить комментарий" });
|
||||
}
|
||||
};
|
||||
|
||||
const handleReplyClick = (commentId: string) => {
|
||||
const currentUserId = currentUser()?.id;
|
||||
const comment = comments().find(c => c.id === commentId);
|
||||
const commentUserId = comment?.user;
|
||||
|
||||
if (commentUserId && currentUserId === commentUserId) {
|
||||
showToast({ type: "error", message: "Нельзя ответить на свой комментарий" });
|
||||
return;
|
||||
}
|
||||
|
||||
setReplyTo(replyTo() === commentId ? null : commentId);
|
||||
};
|
||||
|
||||
const handleEditClick = (commentId: string, content: string) => {
|
||||
setEditingComment(commentId);
|
||||
setEditContent(content);
|
||||
};
|
||||
|
||||
const Avatar = (props: {
|
||||
author: string;
|
||||
avatar?: string;
|
||||
size?: "sm" | "md";
|
||||
}) => {
|
||||
const size = props.size || "md";
|
||||
const sizeClasses = { sm: "w-8 h-8 text-sm", md: "w-12 h-12 text-lg" };
|
||||
|
||||
|
|
@ -217,12 +256,18 @@ export default function Comments(props: CommentsProps) {
|
|||
<Show
|
||||
when={props.avatar}
|
||||
fallback={
|
||||
<div class={`${sizeClasses[size]} rounded-full bg-linear-to-br from-blue-400 to-blue-600 flex items-center justify-center text-white font-bold shrink-0`}>
|
||||
<div
|
||||
class={`${sizeClasses[size]} rounded-full bg-gradient-to-br from-yellow-400 to-yellow-600 flex items-center justify-center text-white font-bold shrink-0`}
|
||||
>
|
||||
{props.author.charAt(0)}
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<img src={props.avatar} alt={props.author} class={`${sizeClasses[size]} rounded-full object-cover shrink-0`} />
|
||||
<img
|
||||
src={props.avatar}
|
||||
alt={props.author}
|
||||
class={`${sizeClasses[size]} rounded-full object-cover shrink-0`}
|
||||
/>
|
||||
</Show>
|
||||
);
|
||||
};
|
||||
|
|
@ -238,13 +283,21 @@ export default function Comments(props: CommentsProps) {
|
|||
});
|
||||
};
|
||||
|
||||
const isCommentOwner = (commentUserId?: string) => {
|
||||
return currentUser()?.id === commentUserId;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Show when={toast()}>
|
||||
{(t) => (
|
||||
<div class={`fixed bottom-4 right-4 px-6 py-3 rounded-xl shadow-lg transition-all duration-300 z-50 ${
|
||||
t().type === "success" ? "bg-green-500 text-white" : "bg-red-500 text-white"
|
||||
}`}>
|
||||
<div
|
||||
class={`fixed bottom-4 right-4 px-6 py-3 rounded-xl shadow-lg transition-all duration-300 z-50 ${
|
||||
t().type === "success"
|
||||
? "bg-green-500 text-white"
|
||||
: "bg-red-500 text-white"
|
||||
}`}
|
||||
>
|
||||
{t().message}
|
||||
</div>
|
||||
)}
|
||||
|
|
@ -254,7 +307,9 @@ export default function Comments(props: CommentsProps) {
|
|||
<div class="max-w-4xl mx-auto mt-12 pt-8 border-t border-gray-200">
|
||||
<div class="flex items-center gap-3 mb-8">
|
||||
<h3 class="text-2xl font-bold text-gray-900">Комментарии</h3>
|
||||
<span class="px-3 py-1 bg-blue-100 text-blue-700 text-sm font-medium rounded-full">0</span>
|
||||
<span class="px-3 py-1 bg-blue-100 text-blue-700 text-sm font-medium rounded-full">
|
||||
0
|
||||
</span>
|
||||
</div>
|
||||
<div class="text-center py-12">
|
||||
<div class="inline-block animate-spin rounded-full h-12 w-12 border-4 border-blue-600 border-t-transparent"></div>
|
||||
|
|
@ -266,42 +321,129 @@ export default function Comments(props: CommentsProps) {
|
|||
<div class="max-w-4xl mx-auto mt-12 pt-8 border-t border-gray-200">
|
||||
<div class="flex items-center gap-3 mb-8">
|
||||
<h3 class="text-2xl font-bold text-gray-900">Комментарии</h3>
|
||||
<span class="px-3 py-1 bg-blue-100 text-blue-700 text-sm font-medium rounded-full">{comments().length}</span>
|
||||
<span class="px-3 py-1 bg-blue-100 text-blue-700 text-sm font-medium rounded-full">
|
||||
{comments().length}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<Show when={comments().length > 0} fallback={
|
||||
<div class="text-center py-12 text-gray-500">
|
||||
<p>Пока нет комментариев. Будьте первым!</p>
|
||||
</div>
|
||||
}>
|
||||
<Show
|
||||
when={comments().length > 0}
|
||||
fallback={
|
||||
<div class="text-center py-12 text-gray-500">
|
||||
<p>Пока нет комментариев. Будьте первым!</p>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div class="space-y-6 mb-10">
|
||||
<For each={comments()}>
|
||||
{(comment) => (
|
||||
<div class="bg-white rounded-2xl p-6 shadow-sm border border-gray-100">
|
||||
<div class="flex items-start gap-4">
|
||||
<Avatar author={comment.expand?.user?.firstName || "Аноним"} avatar={comment.expand?.user?.avatar} />
|
||||
<Avatar
|
||||
author={comment.expand?.user?.firstName || comment.expand?.user?.name || "Аноним"}
|
||||
avatar={comment.expand?.user?.avatar}
|
||||
/>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex items-center gap-2 mb-2 flex-wrap">
|
||||
<span class="font-semibold text-gray-900">{comment.expand?.user?.firstName || "Аноним"}</span>
|
||||
<span class="font-semibold text-gray-900">
|
||||
{comment.expand?.user?.firstName || comment.expand?.user?.name || "Аноним"}
|
||||
</span>
|
||||
<Show when={comment.is_verified}>
|
||||
<svg class="w-4 h-4 text-blue-500" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
|
||||
<svg
|
||||
class="w-4 h-4 text-blue-600"
|
||||
fill="currentColor"
|
||||
viewBox="0 0 20 20"
|
||||
>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
|
||||
clip-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</Show>
|
||||
<span class="text-sm text-gray-400 ml-auto">{formatDate(comment.created)}</span>
|
||||
<span class="text-sm text-gray-400 ml-auto">
|
||||
{formatDate(comment.created)}
|
||||
</span>
|
||||
</div>
|
||||
<p class="text-gray-700 leading-relaxed mb-3">{comment.content}</p>
|
||||
<div class="flex items-center gap-1">
|
||||
<button onClick={() => handleReplyClick(comment.id)} class="text-sm text-blue-600 hover:text-blue-800 font-medium flex items-center gap-1 transition-colors hover:cursor-pointer">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6" />
|
||||
<Show
|
||||
when={editingComment() === comment.id}
|
||||
fallback={
|
||||
<p class="text-gray-700 leading-relaxed mb-3">
|
||||
{comment.content}
|
||||
</p>
|
||||
}
|
||||
>
|
||||
<CommentForm
|
||||
isEdit={true}
|
||||
initialContent={editContent()}
|
||||
onSubmit={(data) => handleEdit(comment.id, data)}
|
||||
onCancel={() => {
|
||||
setEditingComment(null);
|
||||
setEditContent("");
|
||||
}}
|
||||
onDelete={() => handleDelete(comment.id)}
|
||||
user={currentUser()}
|
||||
/>
|
||||
</Show>
|
||||
<div class="flex items-center gap-1 flex-wrap">
|
||||
<button
|
||||
onClick={() => handleReplyClick(comment.id)}
|
||||
class="text-sm text-blue-600 hover:text-blue-600-dark font-medium flex items-center gap-1 transition-colors hover:cursor-pointer"
|
||||
>
|
||||
<svg
|
||||
class="w-4 h-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"
|
||||
/>
|
||||
</svg>
|
||||
{replyTo() === comment.id ? "Отменить" : "Ответить"}
|
||||
</button>
|
||||
<Show when={toastVisible() === comment.id}>
|
||||
<span class="text-xs text-yellow-700 bg-yellow-100 px-2 py-1 rounded-md whitespace-nowrap">
|
||||
Нельзя ответить на свой комментарий
|
||||
</span>
|
||||
<Show when={isCommentOwner(comment.user)}>
|
||||
<button
|
||||
onClick={() => handleEditClick(comment.id, comment.content)}
|
||||
class="text-sm text-gray-400 hover:text-blue-600 font-medium flex items-center gap-1 transition-colors hover:cursor-pointer ml-2"
|
||||
>
|
||||
<svg
|
||||
class="w-4 h-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
|
||||
/>
|
||||
</svg>
|
||||
Редактировать
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDelete(comment.id)}
|
||||
class="text-sm text-gray-400 hover:text-red-500 font-medium flex items-center gap-1 transition-colors hover:cursor-pointer ml-2"
|
||||
>
|
||||
<svg
|
||||
class="w-4 h-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
Удалить
|
||||
</button>
|
||||
</Show>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -309,7 +451,12 @@ export default function Comments(props: CommentsProps) {
|
|||
|
||||
<Show when={replyTo() === comment.id}>
|
||||
<div class="mt-4 ml-16 pl-4 border-l-2 border-blue-200">
|
||||
<CommentForm isReply={true} onSubmit={(data) => handleReply(comment.id, data)} onCancel={() => setReplyTo(null)} user={currentUser()} />
|
||||
<CommentForm
|
||||
isReply={true}
|
||||
onSubmit={(data) => handleReply(comment.id, data)}
|
||||
onCancel={() => setReplyTo(null)}
|
||||
user={currentUser()}
|
||||
/>
|
||||
</div>
|
||||
</Show>
|
||||
|
||||
|
|
@ -318,18 +465,36 @@ export default function Comments(props: CommentsProps) {
|
|||
<For each={comment.replies}>
|
||||
{(reply) => (
|
||||
<div class="flex items-start gap-3 pl-4 border-l-2 border-gray-200">
|
||||
<Avatar author={reply.expand?.user?.firstName || "Аноним"} avatar={reply.expand?.user?.avatar} size="sm" />
|
||||
<Avatar
|
||||
author={reply.expand?.user?.firstName || reply.expand?.user?.name || "Аноним"}
|
||||
avatar={reply.expand?.user?.avatar}
|
||||
size="sm"
|
||||
/>
|
||||
<div class="flex-1 bg-gray-50 rounded-xl p-4">
|
||||
<div class="flex items-center gap-2 mb-1 flex-wrap">
|
||||
<span class="font-semibold text-gray-900 text-sm">{reply.expand?.user?.firstName || "Аноним"}</span>
|
||||
<span class="font-semibold text-gray-900 text-sm">
|
||||
{reply.expand?.user?.firstName || reply.expand?.user?.name || "Аноним"}
|
||||
</span>
|
||||
<Show when={reply.is_verified}>
|
||||
<svg class="w-3 h-3 text-blue-500" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
|
||||
<svg
|
||||
class="w-3 h-3 text-blue-600"
|
||||
fill="currentColor"
|
||||
viewBox="0 0 20 20"
|
||||
>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
|
||||
clip-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</Show>
|
||||
<span class="text-xs text-gray-400 ml-auto">{formatDate(reply.created)}</span>
|
||||
<span class="text-xs text-gray-400 ml-auto">
|
||||
{formatDate(reply.created)}
|
||||
</span>
|
||||
</div>
|
||||
<p class="text-gray-700 text-sm leading-relaxed">{reply.content}</p>
|
||||
<p class="text-gray-700 text-sm leading-relaxed">
|
||||
{reply.content}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue