All files / src/shared/ProfileImage/EditProfileImageModal EditProfileImageModal.tsx

85.18% Statements 46/54
87.09% Branches 27/31
66.66% Functions 6/9
89.79% Lines 44/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                            2x                                         2x   50x                       50x   50x   50x 50x 50x 50x   50x   50x 2x 1x 1x 1x     50x         50x         50x 4x 4x 4x 4x   2x       1x 1x 1x         50x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x     50x 1x                           50x                                                                                                 2x               2x                        
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import {
  AcceptAllowedImagesTypes,
  MAX_UPLOAD_SIZE,
} from "@hooks/upload-api/uploadHooks";
import { useIsValidFile } from "@hooks/upload-api/useIsValidFile";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { GiveThumbnailProps } from "@shared/Thumbnail/GiveThumbnail";
import { isFunction } from "lodash";
import { GIVE_CONFIRMATION_POP_UP } from "modals/modal_names";
import { useState } from "react";
import EditProfileImageHeader from "./components/EditProfileImageHeader";
import EditUploadedImage from "./components/EditUploadedImage";
import ImagePreview from "./components/ImagePreview";
import useHEICConversion, {
  compressImage,
} from "@components/UploadFile/hooks/useHEICConversion";
import { useCheckDocumentProcessing } from "@redux/slices/uploadProgressSlice";
import { createBlobFromBase64 } from "@utils/helpers";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
 
const SUBTITLE = `Image file must not exceed a size of ${MAX_UPLOAD_SIZE.label}.`;
 
export type TActions = {
  onUpload?: (file: File) => void;
  onDelete?: () => void;
  canUpload?: boolean;
  canDelete: boolean;
};
 
interface EditProfileImageModalProps {
  title?: string;
  defaultImageURL?: string;
  thumbnailType?: GiveThumbnailProps["type"];
  rounded?: boolean;
  subTitle: string;
  actions: TActions;
  onLocalUpload?: (file: File) => boolean;
  shouldBeRounded?: boolean;
  aspectRatio?: number;
}
 
export const EditProfileImageModal = NiceModal.create(
  (props: EditProfileImageModalProps) => {
    const isConvertingImage = useCheckDocumentProcessing(
      UploadDocumentTypes.profileImage,
    );
    const {
      title = "Profile Image",
      defaultImageURL,
      thumbnailType = "image",
      subTitle,
      actions,
      shouldBeRounded,
      onLocalUpload = () => false,
      aspectRatio,
    } = props;
 
    const { onUpload, onDelete, canUpload, canDelete } = actions;
 
    const { open, onClose } = useNiceModal();
    const { isValidFile } = useIsValidFile();
    const [image, setImage] = useState(defaultImageURL);
    const [isEditorVisible, setIsEditorVisible] = useState(false);
 
    const { convertAndUploadHEICFiles } = useHEICConversion();
 
    const handleCancel = () => {
      if (isConvertingImage) return;
      setImage(defaultImageURL);
      onClose();
      setIsEditorVisible(false);
    };
 
    const handleResetImage = () => {
      setImage(defaultImageURL);
      setIsEditorVisible(false);
    };
 
    const handleDelete = () => {
      if (onDelete) onDelete();
      onClose();
    };
 
    const hasPickedAFile = (e: React.ChangeEvent<HTMLInputElement>) => {
      e.preventDefault();
      const file = e.target.files?.[0];
      const allowedTypes = Object.keys(AcceptAllowedImagesTypes);
      if (!file || !isValidFile({ file, allowedTypes })) return;
 
      convertAndUploadHEICFiles({
        files: [file],
        documentType: UploadDocumentTypes.profileImage,
        onComplete: (files: File[]) => {
          const [file] = files;
          setIsEditorVisible(true);
          setImage(URL.createObjectURL(file));
        },
      });
    };
 
    const handleUpdateImage = async (url: HTMLCanvasElement) => {
      Iif (!url) return;
      setIsEditorVisible(false);
      const dataUrl = url.toDataURL("image/jpeg");
      setImage(dataUrl);
      const blob = createBlobFromBase64(dataUrl);
 
      Iif (!blob) return;
 
      const fileName = "image.jpeg";
      const mimeType = "image/jpeg";
      const file = new File([blob], fileName, { type: mimeType });
      const compressedFile = await compressImage(file);
      isFunction(onUpload) && onUpload(compressedFile ?? file);
      onClose();
    };
 
    const handleDeleteImage = () => {
      NiceModal.show(GIVE_CONFIRMATION_POP_UP, {
        modalType: "delete",
        title: "Remove Profile Image",
        description:
          "Are you sure you want to remove the profile image? Profile image help others easily recognize you.",
        customSubmitBtnText: "Yes, Remove",
        actions: {
          handleSuccess: {
            onClick: handleDelete,
          },
        },
      });
    };
 
    return (
      <GiveBaseModal
        open={open}
        width={isEditorVisible ? "732px" : "480px"}
        height="fit-content"
        onClose={handleCancel}
        closeIconProps={{
          bgColor: "solidWhite",
        }}
        customHeader={
          <EditProfileImageHeader
            title={isEditorVisible ? `Crop ${title}` : title}
            description={isEditorVisible ? undefined : SUBTITLE}
            onClick={handleCancel}
          />
        }
        showFooter={false}
        customDialogStyle={
          isEditorVisible ? editDialogStyle : previewDialogStyle
        }
      >
        {isEditorVisible ? (
          <EditUploadedImage
            image={image}
            handleResetImage={handleResetImage}
            handleUpdateImage={handleUpdateImage}
            handleDelete={handleDeleteImage}
            canDelete={canDelete}
            aspectRatio={aspectRatio}
          />
        ) : (
          <ImagePreview
            imageUrl={defaultImageURL}
            thumbnailType={thumbnailType}
            title={subTitle}
            shouldBeRounded={shouldBeRounded}
            hasPickedAFile={hasPickedAFile}
            handleDelete={handleDeleteImage}
            canUpload={canUpload}
            canDelete={canDelete && !!defaultImageURL}
            disabled={isConvertingImage}
            aspectRatio={aspectRatio}
          />
        )}
      </GiveBaseModal>
    );
  },
);
 
const editDialogStyle = {
  "& .MuiDialog-paper": {
    "& .MuiDialogContent-root": {
      padding: "84px 0px 16px",
    },
  },
};
 
const previewDialogStyle = {
  "& .MuiDialog-paper": {
    "& .MuiDialogTitle-root": {
      height: "68px",
      borderBottom: "none",
      padding: "20px 20px 0 ",
    },
  },
  "& .MuiDialogContent-root": {
    padding: "84px 20px 20px 20px !important",
  },
};