All files / src/sections/PayBuilder/Forms/Styling/components LogoUploader.tsx

66.66% Statements 20/30
34.37% Branches 11/32
50% Functions 3/6
66.66% Lines 20/30

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                          45x 21x 21x 21x 21x   21x   21x 6x   6x     6x           21x         21x                 21x                     21x   21x               21x     21x   21x   21x                                                                                             45x                        
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import GiveText from "@shared/Text/GiveText";
import { Stack } from "@mui/material";
import GiveTabs from "@shared/Tabs/GiveTabs";
import { SyntheticEvent, useEffect, useState } from "react";
import { convertToRaw } from "draft-js";
import { useWysiwygData } from "../../About/About";
import HFSimpleRichInput from "@shared/HFInputs/HFRichInput/HFSimpleRichInput";
import { CustomSlider } from "@pages/NewAdvancedBuilder/components/Slider";
import { DEFAULT_LOGO_HEIGHT } from "@sections/PayBuilder/constants";
import { isUrl } from "@sections/PayBuilder/helpers";
import LogoSection from "../LogoSection";
 
export const LogoUploader = () => {
  const { methods } = usePayBuilderForm();
  const { setValue, getValues, watch } = methods;
  const logo = watch("Style.logo");
  const logoHeight = Number(getValues("Style.logo.logoSize"));
 
  const [logoTypeState, setLogoTypeState] = useState<"text" | "image">("text");
 
  useEffect(() => {
    if (logo) {
      const type =
        typeof logo === "object" && "URL" in logo && isUrl(logo.URL)
          ? "image"
          : "text";
      setLogoTypeState(type);
    } else E{
      setLogoTypeState("text");
    }
  }, [logo]);
 
  const handleTabClick = (newType: string) => {
    const type = newType as "text" | "image";
    setLogoTypeState(type);
  };
 
  const handleTextChange = (editorState: any) => {
    if (logoTypeState === "text") {
      setValue(
        "Style.logo",
        JSON.stringify(convertToRaw(editorState.getCurrentContent())),
      );
    }
  };
 
  const onCommitHeight = (
    _: Event | SyntheticEvent<Element, Event>,
    value: number | number[],
  ) => {
    if (typeof value === "number") {
      if (typeof logo === "object" && logo !== null && "URL" in logo) {
        setValue("Style.logo.logoSize", value);
      }
    }
  };
  function getInitialValue() {
    Iif (typeof logo === "string") {
      return logo;
    } else Iif (
      typeof logo === "object" &&
      "URL" in logo &&
      !isUrl(logo.URL) &&
      typeof logo.URL === "string"
    ) {
      return logo.URL;
    } else {
      return null;
    }
  }
  const defaultEditorState = useWysiwygData(getInitialValue());
 
  const checkLogo = typeof logo === "object" && isUrl(logo.URL);
 
  return (
    <>
      <Stack direction="row" justifyContent="space-between" alignItems="center">
        <GiveText mb="12px" color="primary" fontSize="14px">
          Add Logo (Optional)
        </GiveText>
 
        <GiveTabs
          selected={logoTypeState}
          containerSx={{
            width: "245px",
            justifyContent: "space-around",
            padding: "4px",
          }}
          tabSx={{ flex: 1, justifyContent: "center" }}
          items={logoTabs}
          onClick={handleTabClick}
          type="segmented"
        />
      </Stack>
 
      {logoTypeState === "text" ? (
        <HFSimpleRichInput
          value={defaultEditorState}
          options={["fontFamily", "fontSize", "colors"]}
          onChange={handleTextChange}
        />
      ) : (
        <>
          <LogoSection />
          {checkLogo && (
            <CustomSlider
              defaultValue={logoHeight || DEFAULT_LOGO_HEIGHT}
              maxValue={94}
              minValue={24}
              title="Logo Size"
              onChangeCommitted={onCommitHeight}
              showHelperText={true}
              showValue={false}
            />
          )}
        </>
      )}
    </>
  );
};
 
const logoTabs = [
  {
    label: "Text",
    value: "text",
  },
  {
    label: "Image",
    value: "image",
  },
];
 
// isUrl moved to ../../helpers.ts to break circular dependencies