import { useCallback, useMemo } from "react";
import { IoInformationCircleOutline } from "react-icons/io5";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { useDropzone } from "react-dropzone";
import { HiOutlineUpload } from "react-icons/hi";
import { MdClose } from "react-icons/md";
import { t } from "@/utils";
import CustomImage from "@/components/Common/CustomImage";
const EditComponentThree = ({
uploadedImages,
setUploadedImages,
OtherImages,
setOtherImages,
handleImageSubmit,
handleGoBack,
setDeleteImagesId,
}) => {
const onDrop = useCallback((acceptedFiles) => {
if (acceptedFiles.length == 0) {
toast.error(t("wrongFile"));
} else {
setUploadedImages(acceptedFiles);
}
}, []);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: {
"image/jpeg": [".jpeg", ".jpg"],
"image/png": [".png"],
},
multiple: false,
});
const files = useMemo(() => {
if (typeof uploadedImages === "string") {
return (
{t("uploadedImage")}
);
} else {
return (
uploadedImages?.map((file, index) => (
{file.name}
{Math.round(file.size / 1024)} KB
)) || []
);
}
}, [uploadedImages]);
const removeImage = (index) => {
if (typeof uploadedImages === "string") {
setUploadedImages([]);
} else {
setUploadedImages((prevImages) =>
prevImages?.filter((_, i) => i !== index)
);
}
};
const onOtherDrop = useCallback(
(acceptedFiles) => {
const currentFilesCount = OtherImages.length; // Number of files already uploaded
const remainingSlots = 5 - currentFilesCount; // How many more files can be uploaded
if (remainingSlots === 0) {
// Show error if the limit has been reached
toast.error(t("imageLimitExceeded"));
return;
}
if (acceptedFiles.length > remainingSlots) {
// Show error if the number of new files exceeds the remaining slots
toast.error(
t("youCanUpload") + " " + remainingSlots + " " + t("moreImages")
);
return;
}
// Add the new files to the state
setOtherImages((prevImages) => [...prevImages, ...acceptedFiles]);
},
[OtherImages]
);
const {
getRootProps: getRootOtheProps,
getInputProps: getInputOtherProps,
isDragActive: isDragOtherActive,
} = useDropzone({
onDrop: onOtherDrop,
accept: {
"image/jpeg": [".jpeg", ".jpg"],
"image/png": [".png"],
},
multiple: true,
});
const removeOtherImage = (index, file) => {
setOtherImages((prevImages) => prevImages.filter((_, i) => i !== index));
setDeleteImagesId((prevIds) => {
const newId = file?.id;
if (prevIds) {
return `${prevIds},${newId}`;
} else {
return `${newId}`;
}
});
};
const filesOther = useMemo(
() =>
OtherImages &&
OtherImages?.map((file, index) => (
{
(file?.name || file?.size) &&
{file.name}
{Math.round(file.size / 1024)} KB
}
)),
[OtherImages]
);
return (
{t("mainPicture")}
0 ? "none" : "" }}
>
{isDragActive ? (
{t("dropFiles")}
) : (
{t("dragFiles")}
{t("or")}
{t("upload")}
)}
{files}
{t("otherPicture")}
{t("maxOtherImages")}
= 5 ? "none" : "" }}
>
{isDragOtherActive ? (
{t("dropFiles")}
) : (
{t("dragFiles")}
{t("or")}
{t("upload")}
)}
{filesOther}
);
};
export default EditComponentThree;