All files / src/sections/PayBuilder PayBuilder.tsx

95.65% Statements 22/23
83.33% Branches 15/18
100% Functions 5/5
95.45% Lines 21/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                                          736x 736x 59x 22x   22x         736x                       136x   131x 131x 131x 131x 131x   131x 60x         131x 1x     130x 23x   107x 107x                                                                                 43x                        
import MasqueradeSnackbar from "@components/Merchants/Masquerade/MasqueradeSnackbar";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { Grid, styled } from "@mui/material";
import { ThemeWrapper } from "@theme/v2/Provider";
import { useEffect } from "react";
import { useLocation, useNavigate, useSearchParams } from "react-router-dom";
import ProductsCustomerView from "./components/ProductsCustomerView";
import { useGetPublicForm } from "./components/hooks/useGetPublicForm";
import { TFormStatus, TFormStatusEnum } from "./components/products/types";
import PayBuilderFormProvider, {
  usePayBuilderForm,
} from "./provider/PayBuilderFormProvider";
import { ResponsiveViewSwitcher } from "./provider/Switcher";
import { Product_payload_type } from "./provider/provider.type";
import { generateFormData } from "./utils";
import { PayBuilderContext } from "./provider/PayBuilderContext";
import NotFoundPageState from "@common/EmptyState/NotFoundPageState";
import { BottomSheetModal } from "./Checkout/wrappers/BottomSheet/WithNiceModal";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
 
function WithResetFormData({ data, children }: { children: any; data: any }) {
  const { methods } = usePayBuilderForm();
  useEffect(() => {
    if (data) {
      Eif (methods) {
        // needed just for preview.In the customer view, what form do we clear?
        methods.reset(generateFormData(data as Partial<Product_payload_type>));
      }
    }
  }, []);
 
  return children;
}
 
function PayBuilder() {
  const {
    data,
    isLoading,
    isAuthenticated,
    notFound,
    formId,
    isADBPaymentFormLoading,
    userCookie,
  } = useGetPublicForm();
 
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const location = useLocation();
  const isEdit = searchParams.get("edit") === "true";
  const isBuilderPath = location.pathname.includes("pay_product_builder");
 
  useEffect(() => {
    Iif (!isAuthenticated && !formId && !userCookie) {
      navigate("/login", { replace: true });
    }
  }, [isAuthenticated, formId]);
 
  if (notFound) {
    return <NotFoundPageState showGoHome={isAuthenticated} />;
  }
 
  if ((Boolean(formId) && isLoading) || isADBPaymentFormLoading) {
    return <LoadingSpinner />; // TODO: PUT SKELETONS
  }
  const showBackToPortalButton = true;
  return (
    <>
      <MasqueradeSnackbar
        formType={data?.typeName}
        show={showBackToPortalButton}
        formId={formId}
      />
      <ThemeWrapper isNewThemeOnly>
        <PayBuilderContext
          displayStatus={data?.publishedStatus as TFormStatus}
          isLaunchVisible={!isEdit}
        >
          <GiveMotionWrapper delay={70}>
            <PayBuilderFormProvider>
              <WithResetFormData data={data}>
                {!isBuilderPath ? (
                  <>
                    <ProductsCustomerView
                      showBackToPortalButton={showBackToPortalButton}
                    />
                    <BottomSheetModal />
                  </>
                ) : (
                  <Container container>
                    <ResponsiveViewSwitcher>
                      <ResponsiveViewSwitcher.BuilderForm />
                      <ResponsiveViewSwitcher.FormPreview />
                    </ResponsiveViewSwitcher>
                  </Container>
                )}
              </WithResetFormData>
            </PayBuilderFormProvider>
          </GiveMotionWrapper>
        </PayBuilderContext>
      </ThemeWrapper>
    </>
  );
}
 
export default PayBuilder;
 
const Container = styled(Grid)(({ theme }) => ({
  width: "100%",
  margin: 0,
  padding: 0,
  overflow: "hidden",
  "& > div": {
    flex: 1,
  },
  "& > div:nth-of-type(2)": {
    maxWidth: "none",
  },
}));