([]);
+ 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 (
-
- {app.isLoggedIn() ? (
-
- ) : (
-
- )}
-
{
+ 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 (
+
- {maxPage ? (
-
-
+ );
+ }
+
+ return (
+
);
}
@@ -1313,6 +1398,15 @@ function CommentTile({ comment }: { comment: Comment }) {
)}
+
+ {comment.images.map((image) => (
+
+ ))}
+
{app.user?.id === comment.user.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([]);
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")}