All files / src/sections/PayBuilder/Forms/About ImageSection.tsx

100% Statements 30/30
86.2% Branches 25/29
100% Functions 8/8
100% Lines 27/27

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                                          467x 467x 467x 467x 467x 467x 467x 467x 467x 1x   1x 1x     467x   2x       2x 2x     467x 467x     467x 467x   467x 467x                 9x                                                                                                 45x 2380x     467x                                               45x                        
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { Box, Stack } from "@mui/material";
import { PencilSimpleIcon, PlusIcon } from "@phosphor-icons/react";
import { useSignals } from "@preact/signals-react/runtime";
import { HeroImage } from "@sections/PayBuilder/components/products/Hero/HeroImage";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { isEmpty } from "lodash";
import { useState } from "react";
import { useCheckImgExistence } from "./useCheckImgExistence";
import useCheckFormType from "@sections/PayBuilder/components/hooks/useCheckFormType";
import ImageModalNew from "@sections/PayBuilder/Forms/About/ImageModalNew";
import { useImageSource } from "@sections/PayBuilder/Forms/About/utils";
import {
  getYoutubeVideoThumbnail,
  useGetRedesignedFormTypes,
} from "@sections/PayBuilder/utils";
 
function ImageSection() {
  useSignals();
  const { methods, parsedValues } = usePayBuilderForm();
  const selectedImage = methods.watch("About.selectedImage");
  const [isImageModalOpen, setIsImageModalOpen] = useState(false);
  const onClose = () => setIsImageModalOpen(false);
  const { palette } = useAppTheme();
  const { isFundraiser, formType } = useCheckFormType();
  const redesignedFormTypes = useGetRedesignedFormTypes();
  const handleSelect = (item?: MediaItem, shouldClose = true) => {
    methods.setValue("About.selectedImage", isEmpty(item) ? ({} as any) : item);
    // This resets selected video url, so selected image is displayed
    methods.setValue("About.selectedVideoURL", null);
    Eif (shouldClose) onClose();
  };
 
  const handleSaveVideo = (url: string) => {
    const normalizedUrl =
      url.startsWith("http://") || url.startsWith("https://")
        ? url
        : `https://${url}`;
 
    methods.setValue("About.selectedVideoURL", normalizedUrl);
    onClose();
  };
 
  const { resizeType } = parsedValues?.selectedImage || {};
  const { isLoadingExistence } = useCheckImgExistence({
    storedId: parsedValues?.selectedImage?.id,
  });
  const { data } = useImageSource(parsedValues?.selectedImage);
  const videoURL = methods.watch("About.selectedVideoURL");
 
  const isRedesigned = redesignedFormTypes.includes(formType);
  return (
    <>
      <Box>
        <GiveText fontSize="14px" fontWeight={400} color="primary">
          Image or Video (Optional)
        </GiveText>
 
        <Container
          isImageSelected={Boolean(selectedImage?.URL)}
          onClick={() => !isLoadingExistence && setIsImageModalOpen(true)}
          data-testid="image-element"
          width={isRedesigned ? "100%" : isFundraiser ? "233px" : "188px"}
        >
          {selectedImage?.URL || videoURL ? (
            <>
              <HeroImage
                src={videoURL ? getYoutubeVideoThumbnail(videoURL) : data}
                canvasData={{
                  resizeType: videoURL ? "fill" : resizeType,
                }}
                borderRadius={1}
                height="167px"
              />
 
              <Overlay className="overlay">
                {isLoadingExistence ? (
                  <LoadingSpinner
                    color={palette.icon?.["remain-light"]}
                    sx={{ height: "32px" }}
                  />
                ) : (
                  <PencilSimpleIcon
                    fill={palette.icon?.["remain-light"]}
                    size="32px"
                  />
                )}
              </Overlay>
            </>
          ) : (
            <PlusIcon fill="#000000" size="32px" />
          )}
        </Container>
      </Box>
      <ImageModalNew
        handleSelect={handleSelect}
        open={isImageModalOpen}
        onClose={onClose}
        selectedImage={selectedImage}
        defaultTitle="Add Image or Video"
        showTabs
        handleSaveVideo={handleSaveVideo}
      />
    </>
  );
}
 
export default ImageSection;
 
const Container = styled(Stack, {
  shouldForwardProp: (prop) => prop !== "isImageSelected",
})<{ isImageSelected: boolean; width: string }>(
  ({ theme, isImageSelected, width }) => {
    return {
      width: width,
      position: "relative",
      height: "100px",
      backgroundColor: theme.palette.primitive?.transparent["darken-5"],
      marginTop: "12px",
      alignItems: "center",
      justifyContent: "center",
      borderRadius: "8px",
      ...(!isImageSelected && {
        border: `1px dashed ${theme.palette.border?.tertiary}`,
      }),
      "&:hover .overlay": {
        opacity: 1,
      },
      cursor: "pointer",
      overflow: "hidden",
      [theme.breakpoints.down("sm")]: {
        width: "100%",
      },
    };
  },
);
 
const Overlay = styled(Stack)(({ theme }) => ({
  position: "absolute",
  backgroundColor: "#00000066",
  top: 0,
  left: 0,
  bottom: 0,
  right: 0,
  justifyContent: "center",
  alignItems: "center",
  opacity: 0,
  zIndex: 2,
}));