diff --git a/client/src/components/ImagePicker.tsx b/client/src/components/ImagePicker.tsx index 76c03dc..4bbbfd9 100644 --- a/client/src/components/ImagePicker.tsx +++ b/client/src/components/ImagePicker.tsx @@ -4,8 +4,9 @@ import { Icon } from "@iconify/react"; import { AppState, actionCreators } from "../services/redux"; import { bindActionCreators } from "redux"; import { useDispatch, useSelector } from "react-redux"; +import { toastWarn } from "src/services/toast"; -const ImagePicker = (): JSX.Element => { +const ReturnImagePicker = (): JSX.Element => { const dispatch = useDispatch(); const imagePickerState = useSelector( (state: AppState) => state.imagePickerReducer @@ -21,6 +22,28 @@ const ImagePicker = (): JSX.Element => { document.createElement("img") ); imageElement.current.className = "ImagePicker_Content_Preview_ImgElement"; + const inputFileElement: React.MutableRefObject = + useRef(null); + + const getFile = () => { + if ( + inputFileElement.current?.files !== null && + imagePreviewH1Element.current !== null + ) { + const file = inputFileElement.current?.files[0]; + imageElement.current.src = URL.createObjectURL(file!); + imagePreviewH1Element.current.replaceWith(imageElement.current); + } + if ( + inputFileElement.current?.files !== null && + inputFileElement.current?.files.length !== undefined && + inputFileElement.current?.files.length > 0 && + isImageUrl + ) { + toastWarn("Please select rather imageUrl or imageFile"); + setImageUrl(""); + } + }; useEffect(() => { if (imagePickerState.openedImagePicker === true) { @@ -52,68 +75,94 @@ const ImagePicker = (): JSX.Element => { imageElement.current.src = imageUrl; imagePreviewH1Element.current.replaceWith(imageElement.current); } + if ( + inputFileElement.current?.files !== null && + inputFileElement.current?.files.length !== undefined && + inputFileElement.current?.files.length > 0 + ) { + toastWarn("Please select rather imageUrl or imageFile"); + setImageUrl(""); + } setIsImageUrl(true); }; img.onerror = () => { - if (imagePreviewH1Element !== null) { + if ( + inputFileElement.current?.files !== null && + inputFileElement.current?.files?.length !== 0 && + imagePreviewH1Element.current !== null + ) { + const file = inputFileElement.current?.files[0]; + imageElement.current.src = URL.createObjectURL(file!); + imagePreviewH1Element.current.replaceWith(imageElement.current); + } else if ( + imagePreviewH1Element.current !== null && + inputFileElement.current?.files?.length === 0 + ) { imageElement.current.replaceWith( imagePreviewH1Element.current as Node ); } + setIsImageUrl(false); }; } catch (err) {} }, [imageUrl]); + console.log(isImageUrl); return ( <> - {imagePickerState.openedImagePicker ? ( -
-
-
- { - setImageUrl(e.target.value); - }} - value={imageUrl} - /> -

- NOTE : Consider using image url rather then uploading files - because cloud database have limited Storage -

-
+
+
+
+ { + setImageUrl(e.target.value); + }} + value={imageUrl} + /> +

+ NOTE : Consider using image url rather then uploading files + because cloud database have limited Storage +

+
-
-
- - -
- +
+
+ +
-
-

Preview

-
- +
+
+

Preview

+
+
- ) : ( - "" - )} +
); }; +const ImagePicker = () => { + const imagePickerState = useSelector( + (state: AppState) => state.imagePickerReducer + ); + return <>{imagePickerState.openedImagePicker ? : ""}; +}; + export default ImagePicker;