All files / src/sections/PayBuilder FormPreview.tsx

92% Statements 23/25
54.54% Branches 12/22
100% Functions 6/6
92% Lines 23/25

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                                    533x 531x 531x   531x   531x   531x   531x                                             12x         531x       511x   20x           12x         531x             12x               956x 956x           956x       956x       956x         956x         12x 2124x 531x                              
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { ThemeWrapper, styled } from "@theme/v2/Provider";
import { BaseMainContainer } from "./components/BaseMainContainer";
import { MobileLeftPanelMainHeader } from "./components/MobileLeftPanelMainHeader";
import { usePayBuilderContext } from "./provider/PayBuilderContext";
import { usePayBuilderForm } from "./provider/PayBuilderFormProvider";
import PreviewWrapper from "./components/PreviewWrapper";
import useMasqueradeReducer from "@hooks/Reducers/useMasqueradeReducer";
import { getTextColorsBasedOnBackground } from "./utils";
import useCombineThemes from "@theme/v2/hooks/useCombineThemes";
import { rgbToHex } from "@mui/material";
import ProductPreviewView from "./components/ProductPreviewView";
import InvoicePreviewView from "./components/InvoicePreview/InvoicePreviewView";
import useCheckFormType from "./components/hooks/useCheckFormType";
import { PAYBUILDER_WHITE_BACKGROUND } from "./consts";
import CheckoutPreviewWrapper from "./components/CheckoutPreviewWrapper";
 
function FormPreview() {
  const { isMediumView } = useCustomThemeV2();
  const { previewStep, activeItem } = usePayBuilderContext();
  const { methods } = usePayBuilderForm();
 
  const { masqueradeOffset } = useMasqueradeReducer();
  const PREVIEW_WRAPPER_TEXT =
    "As you create your form, this preview will show how it will appear to others";
 
  const background = methods.watch().Style.background;
 
  return (
    <Container
      masqueradeOffset={masqueradeOffset}
      xs={isMediumView ? 12 : 8}
      item
    >
      {isMediumView && activeItem.id === "checkout_configuration"
        ? null
        : isMediumView && <MobileLeftPanelMainHeader />}
 
      <PreviewWrapper
        background={background}
        previewText={PREVIEW_WRAPPER_TEXT}
      >
        <ActiveViewWithCustomTheme previewStep={previewStep} />
      </PreviewWrapper>
    </Container>
  );
}
type ContainerProps = {
  masqueradeOffset?: number;
};
 
const ActiveView = ({
  type,
}: {
  type: "ProductPreviewView" | "CheckoutFormPreview" | "InvoicePreviewView";
}) => {
  switch (type) {
    case "InvoicePreviewView":
      return <InvoicePreviewView />;
    case "ProductPreviewView":
      return <ProductPreviewView />;
    case "CheckoutFormPreview":
      return <CheckoutPreviewWrapper withColumns />;
    default:
      return null;
  }
};
 
const ActiveViewWithCustomTheme = ({
  previewStep,
}: {
  previewStep: "ProductPreviewView" | "CheckoutFormPreview";
}) => {
  return (
    <DarkWithCustomTheme>
      <ActiveView type={previewStep} />
    </DarkWithCustomTheme>
  );
};
 
export const DarkWithCustomTheme = ({
  children,
  overRideCustomThemeBG = false,
}: {
  children: any;
  overRideCustomThemeBG?: boolean;
}) => {
  //The same has to be done for Cutomer view(public payment form)
  const { methods } = usePayBuilderForm();
  const { isInvoice } = useCheckFormType();
  /* In invoice preview email view, we don't want background color to be affecting the preview.
     This is the reason for isInvoice check, but for payment page preview, background color is needed,
     this is the reason for overRideCustomThemeBG check.
  */
  const backgroundColor =
    isInvoice && !overRideCustomThemeBG
      ? PAYBUILDER_WHITE_BACKGROUND
      : methods.watch().Style.background;
 
  const { luminance } = getTextColorsBasedOnBackground(
    rgbToHex(backgroundColor).toUpperCase(),
  );
 
  const combineThemes = useCombineThemes(
    luminance < 0.5 ? "dark" : "light",
    false,
  );
 
  return (
    <ThemeWrapper combineThemesIOC={combineThemes}>{children}</ThemeWrapper>
  );
};
 
const Container = styled(BaseMainContainer, {
  shouldForwardProp: (prop) => prop !== "masqueradeOffset",
})<ContainerProps>(({ theme, masqueradeOffset = 0 }) => ({
  width: "100%",
  maxHeight: "100%",
  height: "100vh",
  overflowY: "auto",
  alignItems: "center",
  paddingTop: masqueradeOffset > 0 ? `${masqueradeOffset}px` : undefined,
  maxWidth: "100%",
 
  [theme.breakpoints.down("md")]: {
    minWidth: "100%",
  },
}));
 
export default FormPreview;