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

86.95% Statements 20/23
75% Branches 9/12
75% Functions 6/8
90.9% Lines 20/22

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                            45x 915x   915x 80x       915x                     447x     447x 447x   447x   447x   447x   447x   447x 38x                       447x                   121x                                                                                         447x       1341x 1341x                           45x                            
import { Box, Stack } from "@mui/material";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { styled } from "@theme/v2/Provider";
import ImageSection from "./ImageSection";
import useCheckFormType from "@sections/PayBuilder/components/hooks/useCheckFormType";
import HFRichInput from "@shared/HFInputs/HFRichInput/HFRichInput";
import { convertFromRaw, convertToRaw } from "draft-js";
import { useMemo } from "react";
import { usePayBuilderContext } from "@sections/PayBuilder/provider/PayBuilderContext";
import { WithPlaceholder } from "@sections/PayBuilder/components/loaders/WithPlaceholder";
import { DateAndLocation } from "../DateAndLocation";
import { safeParse } from "@utils/index";
 
export const useWysiwygData = (value: any) => {
  const JSONData = safeParse(value);
 
  const defaultEditorState = useMemo(
    () => (value && JSONData ? convertFromRaw(JSONData) : undefined),
    [JSONData, value],
  );
 
  return defaultEditorState;
};
 
function About({
  isEdit,
  isFromADB,
}: {
  isEdit?: boolean;
  isFromADB?: boolean;
}) {
  const { methods, parsedValues, isPaymentFormInfosLoading, data } =
    usePayBuilderForm();
  const {
    screenStates: { isMobileDevice },
  } = usePayBuilderContext();
  const { setValue, watch } = methods;
 
  const assetPosition = watch("About.assetPosition");
 
  const { isFundraiser, isSweepstake, isEvent } = useCheckFormType();
 
  const defaultEditorState = useWysiwygData(parsedValues.description);
 
  const defaultContentState = useWysiwygData(data.description);
 
  const assetTabs = useMemo(() => {
    return [
      {
        label: isMobileDevice ? "Top" : "Left",
        value: "left",
      },
      {
        label: isMobileDevice ? "Bottom" : "Right",
        value: "right",
      },
    ];
  }, [isMobileDevice]);
 
  const sections = [
    {
      content: (
        <HFGiveInput
          maxLength={100}
          name="About.heading"
          label="Title"
          placeholder="Title"
          hidePlaceholder
          onValidate={() => {
            methods.trigger("About.heading");
          }}
        />
      ),
      config: { isLoading: isPaymentFormInfosLoading, width: "100%" },
    },
    {
      content: (
        <HFRichInput
          value={defaultEditorState}
          defaultValue={defaultContentState}
          options={[
            "headings",
            "alignments",
            "fontSize",
            "colors",
            "ordered-items",
            "image",
          ]}
          onChange={(editorState) => {
            setValue(
              "About.description",
              JSON.stringify(convertToRaw(editorState.getCurrentContent())),
            );
          }}
        />
      ),
      config: {
        isLoading: isPaymentFormInfosLoading,
        width: "100%",
        childrenStyle: { maxWidth: 0, display: "none" },
      },
    },
    {
      content: <ImageSection />,
      config: {
        isLoading: isPaymentFormInfosLoading,
        width: "100%",
        height: "199px",
      },
 
      isHidden: !!isFromADB,
    },
  ];
 
  return (
    <Box pb="20px">
      <Stack gap="24px">
        {sections.map(({ content, config, isHidden }, index) => {
          Iif (isHidden) return null;
          return (
            <WithPlaceholder key={index} index={index} {...config}>
              {content}
            </WithPlaceholder>
          );
        })}
        {isEvent && <DateAndLocation />}
      </Stack>
    </Box>
  );
}
 
export default About;
 
const TextArea = styled(HFGiveInput)(() => {
  return {
    "& .MuiInputBase-root": {
      padding: "0 !important",
    },
    "& .MuiInputBase-input": {
      height: "150px",
      padding: "10px !important",
    },
    "& textarea": {
      resize: "none",
    },
  };
});