Przeglądaj źródła

Modify method and props name

https://youtrack.weseek.co.jp/issue/GW-7876
- Rename isShouldCrop props to showCropOption
- Add null to imageRef type
- Modify convertBase64ToBlob method
- Rename crop method to save and fix comment
- Add method to handle close modal
- Set imageRef to null and setIsCropImage true on close modal
(Crop Image modal showing previous image when close modal / cancel)
- Move close modal to ImageCropModal component
- Adjust prop and method  usage
Mudana-Grune 3 lat temu
rodzic
commit
bd4706817e

+ 3 - 9
packages/app/src/components/Admin/Customize/CustomizeLogoSetting.tsx

@@ -80,11 +80,6 @@ const CustomizeLogoSetting = (): JSX.Element => {
     }
     }
   }, [t]);
   }, [t]);
 
 
-  // Clear uploadedLogoSrc and close modal
-  const onModalCloseHandler = useCallback(() => {
-    setIsImageCropModalShow(false);
-    setUploadLogoSrc(null);
-  }, []);
 
 
   const onCropCompleted = useCallback(async(croppedImage) => {
   const onCropCompleted = useCallback(async(croppedImage) => {
     try {
     try {
@@ -99,8 +94,7 @@ const CustomizeLogoSetting = (): JSX.Element => {
       setRetrieveError(err);
       setRetrieveError(err);
       throw new Error('Failed to upload brand logo');
       throw new Error('Failed to upload brand logo');
     }
     }
-    onModalCloseHandler();
-  }, [t, onModalCloseHandler]);
+  }, [t]);
 
 
   return (
   return (
     <React.Fragment>
     <React.Fragment>
@@ -176,10 +170,10 @@ const CustomizeLogoSetting = (): JSX.Element => {
       <ImageCropModal
       <ImageCropModal
         isShow={isImageCropModalShow}
         isShow={isImageCropModalShow}
         src={uploadLogoSrc}
         src={uploadLogoSrc}
-        onModalClose={onModalCloseHandler}
+        onModalClose={() => setIsImageCropModalShow(false)}
         onCropCompleted={onCropCompleted}
         onCropCompleted={onCropCompleted}
         isCircular={false}
         isCircular={false}
-        isShouldCrop={false}
+        showCropOption={false}
       />
       />
     </React.Fragment>
     </React.Fragment>
   );
   );

+ 22 - 18
packages/app/src/components/Common/ImageCropModal.tsx

@@ -35,15 +35,15 @@ type Props = {
   onModalClose: () => void,
   onModalClose: () => void,
   onCropCompleted: (res: any) => void,
   onCropCompleted: (res: any) => void,
   isCircular: boolean,
   isCircular: boolean,
-  isShouldCrop: boolean
+  showCropOption: boolean
 }
 }
 const ImageCropModal: FC<Props> = (props: Props) => {
 const ImageCropModal: FC<Props> = (props: Props) => {
 
 
   const {
   const {
-    isShow, src, onModalClose, onCropCompleted, isCircular, isShouldCrop,
+    isShow, src, onModalClose, onCropCompleted, isCircular, showCropOption,
   } = props;
   } = props;
 
 
-  const [imageRef, setImageRef] = useState<HTMLImageElement>();
+  const [imageRef, setImageRef] = useState<HTMLImageElement | null>(null);
   const [cropOptions, setCropOtions] = useState<CropOptions>(null);
   const [cropOptions, setCropOtions] = useState<CropOptions>(null);
   const [isCropImage, setIsCropImage] = useState<boolean>(true);
   const [isCropImage, setIsCropImage] = useState<boolean>(true);
   const { t } = useTranslation();
   const { t } = useTranslation();
@@ -97,27 +97,31 @@ const ImageCropModal: FC<Props> = (props: Props) => {
 
 
   // Convert base64 Image to blob
   // Convert base64 Image to blob
   const convertBase64ToBlob = async(base64Image: string) => {
   const convertBase64ToBlob = async(base64Image: string) => {
-    const parts = base64Image.split(';base64,');
-    const imageType = parts[0].split(':')[1];
-    const decodedData = window.atob(parts[1]);
-    const uInt8Array = new Uint8Array(decodedData.length);
-    for (let i = 0; i < decodedData.length; ++i) {
-      uInt8Array[i] = decodedData.charCodeAt(i);
-    }
-    return new Blob([uInt8Array], { type: imageType });
+    const base64Response = await fetch(base64Image);
+    const blob = await base64Response.blob();
+    return blob;
+  };
+
+
+  // Clear image and set isImageCrop true on modal close
+  const onModalCloseHandler = async() => {
+    setImageRef(null);
+    setIsCropImage(true);
+    onModalClose();
   };
   };
 
 
-  const crop = async() => {
-    // crop immages
+  // Crop and save image
+  const save = async() => {
     if (imageRef && cropOptions?.width && cropOptions.height) {
     if (imageRef && cropOptions?.width && cropOptions.height) {
       const result = isCropImage ? await getCroppedImg(imageRef, cropOptions) : await convertBase64ToBlob(imageRef.src);
       const result = isCropImage ? await getCroppedImg(imageRef, cropOptions) : await convertBase64ToBlob(imageRef.src);
       onCropCompleted(result);
       onCropCompleted(result);
     }
     }
+    onModalCloseHandler();
   };
   };
 
 
   return (
   return (
-    <Modal isOpen={isShow} toggle={onModalClose}>
-      <ModalHeader tag="h4" toggle={onModalClose} className="bg-info text-light">
+    <Modal isOpen={isShow} toggle={onModalCloseHandler}>
+      <ModalHeader tag="h4" toggle={onModalCloseHandler} className="bg-info text-light">
         {t('crop_image_modal.image_crop')}
         {t('crop_image_modal.image_crop')}
       </ModalHeader>
       </ModalHeader>
       <ModalBody className="my-4">
       <ModalBody className="my-4">
@@ -131,7 +135,7 @@ const ImageCropModal: FC<Props> = (props: Props) => {
         <button type="button" className="btn btn-outline-danger rounded-pill mr-auto" onClick={reset}>
         <button type="button" className="btn btn-outline-danger rounded-pill mr-auto" onClick={reset}>
           {t('crop_image_modal.reset')}
           {t('crop_image_modal.reset')}
         </button>
         </button>
-        { !isShouldCrop && (
+        { !showCropOption && (
           <div className="mr-auto">
           <div className="mr-auto">
             <div className="custom-control custom-switch ">
             <div className="custom-control custom-switch ">
               <input
               <input
@@ -148,10 +152,10 @@ const ImageCropModal: FC<Props> = (props: Props) => {
           </div>
           </div>
         )
         )
         }
         }
-        <button type="button" className="btn btn-outline-secondary rounded-pill mr-2" onClick={onModalClose}>
+        <button type="button" className="btn btn-outline-secondary rounded-pill mr-2" onClick={onModalCloseHandler}>
           {t('crop_image_modal.cancel')}
           {t('crop_image_modal.cancel')}
         </button>
         </button>
-        <button type="button" className="btn btn-outline-primary rounded-pill" onClick={crop}>
+        <button type="button" className="btn btn-outline-primary rounded-pill" onClick={save}>
           { isCropImage ? t('crop_image_modal.crop') : t('crop_image_modal.save') }
           { isCropImage ? t('crop_image_modal.crop') : t('crop_image_modal.save') }
         </button>
         </button>
       </ModalFooter>
       </ModalFooter>

+ 1 - 3
packages/app/src/components/Me/ProfileImageSettings.tsx

@@ -53,8 +53,6 @@ const ProfileImageSettings = (): JSX.Element => {
       // eslint-disable-next-line @typescript-eslint/no-explicit-any
       // eslint-disable-next-line @typescript-eslint/no-explicit-any
       setUploadedPictureSrc((response as any).attachment.filePathProxied);
       setUploadedPictureSrc((response as any).attachment.filePathProxied);
 
 
-      // close modal
-      setShowImageCropModal(false);
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
@@ -160,7 +158,7 @@ const ProfileImageSettings = (): JSX.Element => {
         onModalClose={() => setShowImageCropModal(false)}
         onModalClose={() => setShowImageCropModal(false)}
         onCropCompleted={cropCompletedHandler}
         onCropCompleted={cropCompletedHandler}
         isCircular
         isCircular
-        isShouldCrop
+        showCropOption
       />
       />
 
 
       <div className="row my-3">
       <div className="row my-3">