mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Enhance comment functionality with image support and validation.
This commit is contained in:
53
frontend/src/components/image.tsx
Normal file
53
frontend/src/components/image.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import { Image } from "../network/models.ts";
|
||||
import { network } from "../network/network.ts";
|
||||
|
||||
export function SquareImage({ image }: { image: Image }) {
|
||||
let cover = false;
|
||||
const imgAspectRatio = image.width / image.height;
|
||||
if (imgAspectRatio > 0.8 && imgAspectRatio < 1.2) {
|
||||
cover = true;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className="aspect-square bg-base-200 rounded-lg cursor-pointer"
|
||||
onClick={() => {
|
||||
const dialog = document.getElementById(
|
||||
`image-dialog-${image.id}`,
|
||||
) as HTMLDialogElement;
|
||||
dialog.showModal();
|
||||
}}
|
||||
>
|
||||
<img
|
||||
src={network.getImageUrl(image.id)}
|
||||
alt={"image"}
|
||||
className={`w-full h-full rounded-lg ${cover ? "object-cover" : "object-contain"}`}
|
||||
/>
|
||||
</div>
|
||||
<dialog id={`image-dialog-${image.id}`} className="modal">
|
||||
<div
|
||||
className={"w-screen h-screen flex items-center justify-center"}
|
||||
onClick={() => {
|
||||
const dialog = document.getElementById(
|
||||
`image-dialog-${image.id}`,
|
||||
) as HTMLDialogElement;
|
||||
dialog.close();
|
||||
}}
|
||||
>
|
||||
<img
|
||||
src={network.getImageUrl(image.id)}
|
||||
alt={"image"}
|
||||
className={`object-contain max-w-screen max-h-screen modal-box`}
|
||||
style={{
|
||||
padding: 0,
|
||||
margin: 0,
|
||||
backgroundColor: "transparent",
|
||||
boxShadow: "none",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</dialog>
|
||||
</>
|
||||
);
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
export default function showToast({
|
||||
message,
|
||||
type,
|
||||
parent,
|
||||
}: {
|
||||
message: string;
|
||||
type?: "success" | "error" | "warning" | "info";
|
||||
parent?: HTMLElement | null;
|
||||
}) {
|
||||
type = type || "info";
|
||||
const div = document.createElement("div");
|
||||
@@ -13,7 +15,11 @@ export default function showToast({
|
||||
<span>${message}</span>
|
||||
</div>
|
||||
</div>`;
|
||||
document.body.appendChild(div);
|
||||
if (parent) {
|
||||
parent.appendChild(div);
|
||||
} else {
|
||||
document.body.appendChild(div);
|
||||
}
|
||||
setTimeout(() => {
|
||||
div.remove();
|
||||
}, 3000);
|
||||
|
@@ -111,6 +111,7 @@ export interface Comment {
|
||||
content: string;
|
||||
created_at: string;
|
||||
user: User;
|
||||
images: Image[];
|
||||
}
|
||||
|
||||
export interface CommentWithResource {
|
||||
|
@@ -925,11 +925,12 @@ class Network {
|
||||
async createComment(
|
||||
resourceID: number,
|
||||
content: string,
|
||||
images: number[],
|
||||
): Promise<Response<any>> {
|
||||
try {
|
||||
const response = await axios.postForm(
|
||||
const response = await axios.post(
|
||||
`${this.apiBaseUrl}/comments/${resourceID}`,
|
||||
{ content },
|
||||
{ content, images },
|
||||
);
|
||||
return response.data;
|
||||
} catch (e: any) {
|
||||
@@ -941,11 +942,12 @@ class Network {
|
||||
async updateComment(
|
||||
commentID: number,
|
||||
content: string,
|
||||
images: number[],
|
||||
): Promise<Response<any>> {
|
||||
try {
|
||||
const response = await axios.putForm(
|
||||
const response = await axios.put(
|
||||
`${this.apiBaseUrl}/comments/${commentID}`,
|
||||
{ content },
|
||||
{ content, images },
|
||||
);
|
||||
return response.data;
|
||||
} catch (e: any) {
|
||||
|
@@ -26,12 +26,14 @@ import {
|
||||
MdAdd,
|
||||
MdArrowDownward,
|
||||
MdArrowUpward,
|
||||
MdClose,
|
||||
MdOutlineArticle,
|
||||
MdOutlineComment,
|
||||
MdOutlineDataset,
|
||||
MdOutlineDelete,
|
||||
MdOutlineDownload,
|
||||
MdOutlineEdit,
|
||||
MdOutlineImage,
|
||||
MdOutlineOpenInNew,
|
||||
} from "react-icons/md";
|
||||
import { app } from "../app.ts";
|
||||
@@ -45,6 +47,7 @@ import Button from "../components/button.tsx";
|
||||
import Badge, { BadgeAccent } from "../components/badge.tsx";
|
||||
import Input, { TextArea } from "../components/input.tsx";
|
||||
import { useAppContext } from "../components/AppContext.tsx";
|
||||
import { SquareImage } from "../components/image.tsx";
|
||||
|
||||
export default function ResourcePage() {
|
||||
const params = useParams();
|
||||
@@ -1131,23 +1134,45 @@ function UpdateFileInfoDialog({ file }: { file: RFile }) {
|
||||
|
||||
function Comments({ resourceId }: { resourceId: number }) {
|
||||
const [page, setPage] = useState(1);
|
||||
|
||||
const [maxPage, setMaxPage] = useState(0);
|
||||
|
||||
const [listKey, setListKey] = useState(0);
|
||||
|
||||
const [commentContent, setCommentContent] = useState("");
|
||||
|
||||
const [isLoading, setLoading] = useState(false);
|
||||
|
||||
const { t } = useTranslation();
|
||||
|
||||
const reload = useCallback(() => {
|
||||
setPage(1);
|
||||
setMaxPage(0);
|
||||
setListKey((prev) => prev + 1);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<CommentInput resourceId={resourceId} reload={reload} />
|
||||
<CommentsList
|
||||
resourceId={resourceId}
|
||||
page={page}
|
||||
maxPageCallback={setMaxPage}
|
||||
key={listKey}
|
||||
/>
|
||||
{maxPage ? (
|
||||
<div className={"w-full flex justify-center"}>
|
||||
<Pagination page={page} setPage={setPage} totalPages={maxPage} />
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function CommentInput({
|
||||
resourceId,
|
||||
reload,
|
||||
}: {
|
||||
resourceId: number;
|
||||
reload: () => void;
|
||||
}) {
|
||||
const [commentContent, setCommentContent] = useState("");
|
||||
const [isLoading, setLoading] = useState(false);
|
||||
const [images, setImages] = useState<File[]>([]);
|
||||
const { t } = useTranslation();
|
||||
|
||||
const sendComment = async () => {
|
||||
if (isLoading) {
|
||||
return;
|
||||
@@ -1160,9 +1185,25 @@ function Comments({ resourceId }: { resourceId: number }) {
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
const res = await network.createComment(resourceId, commentContent);
|
||||
const imageIds: number[] = [];
|
||||
for (const image of images) {
|
||||
const res = await network.uploadImage(image);
|
||||
if (res.success) {
|
||||
imageIds.push(res.data!);
|
||||
} else {
|
||||
showToast({ message: res.message, type: "error" });
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const res = await network.createComment(
|
||||
resourceId,
|
||||
commentContent,
|
||||
imageIds,
|
||||
);
|
||||
if (res.success) {
|
||||
setCommentContent("");
|
||||
setImages([]);
|
||||
showToast({
|
||||
message: t("Comment created successfully"),
|
||||
type: "success",
|
||||
@@ -1174,45 +1215,89 @@ function Comments({ resourceId }: { resourceId: number }) {
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
{app.isLoggedIn() ? (
|
||||
<div className={"mt-4 mb-6 textarea w-full p-4 h-40 flex flex-col"}>
|
||||
<textarea
|
||||
placeholder={t("Write down your comment")}
|
||||
className={"w-full resize-none grow"}
|
||||
value={commentContent}
|
||||
onChange={(e) => setCommentContent(e.target.value)}
|
||||
/>
|
||||
<div className={"flex flex-row-reverse"}>
|
||||
<button
|
||||
onClick={sendComment}
|
||||
className={`btn btn-primary h-8 text-sm mx-2 ${commentContent === "" && "btn-disabled"}`}
|
||||
>
|
||||
{isLoading ? (
|
||||
<span className={"loading loading-spinner loading-sm"}></span>
|
||||
) : null}
|
||||
Submit
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<InfoAlert
|
||||
message={t("You need to log in to comment")}
|
||||
className={"my-4 alert-info"}
|
||||
/>
|
||||
)}
|
||||
<CommentsList
|
||||
resourceId={resourceId}
|
||||
page={page}
|
||||
maxPageCallback={setMaxPage}
|
||||
key={listKey}
|
||||
const handleAddImage = () => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = "image/*";
|
||||
input.multiple = true;
|
||||
input.onchange = (e) => {
|
||||
const files = (e.target as HTMLInputElement).files;
|
||||
if ((files?.length ?? 0) + images.length > 9) {
|
||||
showToast({
|
||||
message: t("You can only upload up to 9 images"),
|
||||
type: "error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (files) {
|
||||
setImages((prev) => [...prev, ...Array.from(files)]);
|
||||
}
|
||||
};
|
||||
input.click();
|
||||
};
|
||||
|
||||
if (!app.isLoggedIn()) {
|
||||
return (
|
||||
<InfoAlert
|
||||
message={t("You need to log in to comment")}
|
||||
className={"my-4 alert-info"}
|
||||
/>
|
||||
{maxPage ? (
|
||||
<div className={"w-full flex justify-center"}>
|
||||
<Pagination page={page} setPage={setPage} totalPages={maxPage} />
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={"mt-4 mb-6 textarea w-full p-4 flex flex-col"}>
|
||||
<textarea
|
||||
placeholder={t("Write down your comment")}
|
||||
className={"w-full resize-none grow h-40"}
|
||||
value={commentContent}
|
||||
onChange={(e) => setCommentContent(e.target.value)}
|
||||
/>
|
||||
{images.length > 0 && (
|
||||
<div
|
||||
className={
|
||||
"grid grid-cols-3 sm:grid-cols-5 md:grid-cols-6 lg:grid-cols-8 gap-2 my-2"
|
||||
}
|
||||
>
|
||||
{images.map((image, index) => (
|
||||
<div key={index} className={"relative"}>
|
||||
<img
|
||||
src={URL.createObjectURL(image)}
|
||||
alt={`comment-image-${index}`}
|
||||
className={"rounded-lg aspect-square object-cover"}
|
||||
/>
|
||||
<button
|
||||
className={
|
||||
"btn btn-xs btn-circle btn-error absolute top-1 right-1"
|
||||
}
|
||||
onClick={() => {
|
||||
setImages((prev) => prev.filter((_, i) => i !== index));
|
||||
}}
|
||||
>
|
||||
<MdClose size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
)}
|
||||
<div className={"flex"}>
|
||||
<button
|
||||
className={"btn btn-ghost btn-sm btn-circle"}
|
||||
onClick={handleAddImage}
|
||||
>
|
||||
<MdOutlineImage size={18} />
|
||||
</button>
|
||||
<span className={"grow"} />
|
||||
<button
|
||||
onClick={sendComment}
|
||||
className={`btn btn-primary h-8 text-sm mx-2 ${commentContent === "" && "btn-disabled"}`}
|
||||
>
|
||||
{isLoading ? (
|
||||
<span className={"loading loading-spinner loading-sm"}></span>
|
||||
) : null}
|
||||
{t("Submit")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1313,6 +1398,15 @@ function CommentTile({ comment }: { comment: Comment }) {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div
|
||||
className={
|
||||
"grid grid-cols-3 sm:grid-cols-5 md:grid-cols-6 lg:grid-cols-8 gap-2 p-2"
|
||||
}
|
||||
>
|
||||
{comment.images.map((image) => (
|
||||
<SquareImage key={image.id} image={image} />
|
||||
))}
|
||||
</div>
|
||||
{app.user?.id === comment.user.id && (
|
||||
<div className={"flex flex-row-reverse"}>
|
||||
<DeleteCommentDialog commentId={comment.id} />
|
||||
@@ -1397,13 +1491,33 @@ function EditCommentDialog({ comment }: { comment: Comment }) {
|
||||
const [content, setContent] = useState(comment.content);
|
||||
const { t } = useTranslation();
|
||||
const reload = useContext(context);
|
||||
const [existingImages, setExistingImages] = useState(comment.images);
|
||||
const [newImages, setNewImages] = useState<File[]>([]);
|
||||
|
||||
const handleUpdate = async () => {
|
||||
if (isLoading) {
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
const res = await network.updateComment(comment.id, content);
|
||||
const imageIds: number[] = [];
|
||||
for (const existingImage of existingImages) {
|
||||
imageIds.push(existingImage.id);
|
||||
}
|
||||
for (const newImage of newImages) {
|
||||
const res = await network.uploadImage(newImage);
|
||||
if (res.success) {
|
||||
imageIds.push(res.data!);
|
||||
} else {
|
||||
showToast({
|
||||
message: res.message,
|
||||
type: "error",
|
||||
parent: document.getElementById(`dialog_box`),
|
||||
});
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const res = await network.updateComment(comment.id, content, imageIds);
|
||||
const dialog = document.getElementById(
|
||||
`edit_comment_dialog_${comment.id}`,
|
||||
) as HTMLDialogElement;
|
||||
@@ -1415,7 +1529,11 @@ function EditCommentDialog({ comment }: { comment: Comment }) {
|
||||
});
|
||||
reload();
|
||||
} else {
|
||||
showToast({ message: res.message, type: "error" });
|
||||
showToast({
|
||||
message: res.message,
|
||||
type: "error",
|
||||
parent: document.getElementById(`dialog_box`),
|
||||
});
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
@@ -1435,13 +1553,90 @@ function EditCommentDialog({ comment }: { comment: Comment }) {
|
||||
{t("Edit")}
|
||||
</button>
|
||||
<dialog id={`edit_comment_dialog_${comment.id}`} className="modal">
|
||||
<div className="modal-box">
|
||||
<div className="modal-box" id={"dialog_box"}>
|
||||
<h3 className="font-bold text-lg">{t("Edit Comment")}</h3>
|
||||
<TextArea
|
||||
label={t("Content")}
|
||||
value={content}
|
||||
onChange={(e) => setContent(e.target.value)}
|
||||
/>
|
||||
<div className={"flex flex-col my-2"}>
|
||||
<p className={"text-sm font-bold mb-2"}>{t("Images")}</p>
|
||||
<div className={"grid grid-cols-4 sm:grid-cols-5 gap-2"}>
|
||||
{existingImages.map((image) => (
|
||||
<div key={image.id} className={"relative"}>
|
||||
<SquareImage image={image} />
|
||||
<button
|
||||
className={
|
||||
"btn btn-xs btn-circle btn-error absolute top-1 right-1"
|
||||
}
|
||||
onClick={() => {
|
||||
setExistingImages((prev) =>
|
||||
prev.filter((i) => i.id !== image.id),
|
||||
);
|
||||
}}
|
||||
>
|
||||
<MdClose size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
{newImages.map((image, index) => (
|
||||
<div key={index} className={"relative"}>
|
||||
<img
|
||||
src={URL.createObjectURL(image)}
|
||||
alt={`comment-image-${index}`}
|
||||
className={"rounded-lg aspect-square object-cover"}
|
||||
/>
|
||||
<button
|
||||
className={
|
||||
"btn btn-xs btn-circle btn-error absolute top-1 right-1"
|
||||
}
|
||||
onClick={() => {
|
||||
setNewImages((prev) =>
|
||||
prev.filter((_, i) => i !== index),
|
||||
);
|
||||
}}
|
||||
>
|
||||
<MdClose size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className={"flex"}>
|
||||
<button
|
||||
className={"btn btn-sm mt-2"}
|
||||
onClick={() => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = "image/*";
|
||||
input.multiple = true;
|
||||
input.onchange = (e) => {
|
||||
const files = (e.target as HTMLInputElement).files;
|
||||
if (
|
||||
(files?.length ?? 0) +
|
||||
existingImages.length +
|
||||
newImages.length >
|
||||
9
|
||||
) {
|
||||
showToast({
|
||||
message: t("You can only upload up to 9 images"),
|
||||
type: "error",
|
||||
parent: document.getElementById(`dialog_box`),
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (files) {
|
||||
setNewImages((prev) => [...prev, ...Array.from(files)]);
|
||||
}
|
||||
};
|
||||
input.click();
|
||||
}}
|
||||
>
|
||||
<MdAdd size={18} />
|
||||
{t("Add")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="modal-action">
|
||||
<form method="dialog">
|
||||
<button className="btn btn-ghost">{t("Close")}</button>
|
||||
@@ -1450,7 +1645,7 @@ function EditCommentDialog({ comment }: { comment: Comment }) {
|
||||
{isLoading ? (
|
||||
<span className={"loading loading-spinner loading-sm"}></span>
|
||||
) : null}
|
||||
{t("Update")}
|
||||
{t("Submit")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user