All files / src/features/GiveOnboarding/pages/Business/Details index.tsx

66.66% Statements 12/18
91.3% Branches 21/23
33.33% Functions 2/6
70.58% Lines 12/17

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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                                                              3x 3x 3x 3x 3x     3x 3x                                                                     3x             3x 1x                     3x                                                                                                                                                       9x    
import { StepViewContainer } from "@features/GiveOnboarding/container/StepViewContainer";
import { useFormStep } from "@features/GiveOnboarding/hooks/useFormStep";
import { IStep } from "@features/GiveOnboarding/types";
import {
  BusinessDetails,
  IBusinessDetailsFormValues,
} from "@features/GiveOnboarding/types/form";
import { yupResolver } from "@hookform/resolvers/yup";
import React, { forwardRef, useEffect } from "react";
import { businessDetailsSchema } from "./schema";
import WebsiteInput from "@features/Merchants/MerchantSidePanel/components/WebsiteInput/WebsiteInput";
import { FormProvider, SubmitHandler } from "react-hook-form";
import { HFGiveTelephone } from "@shared/HFInputs/HFGiveTelephone/HFGiveTelephone";
import HFGiveCustomAmount from "@shared/HFInputs/HFGiveCustomAmount/HFGiveCustomAmount";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import {
  BILLING_DESCRIPTOR_MAX_LENGTH,
  MAX_AMOUNT_TARGET,
} from "@constants/constants";
import GiveText from "@shared/Text/GiveText";
import { businessDetailsDefaultValues } from "./consts";
import MasterCard from "@assets/images/PaymentCards/mastercard.svg";
import GiveStatementPreview from "@shared/GiveStatementPreview";
import { usePatchBusinessDetailsMutation } from "@features/GiveOnboarding/mutations";
import { useGetCurrentMerchantId } from "@hooks/common";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import { toNumber } from "@features/GiveOnboarding/utils";
import { addSizeToImage } from "@utils/image.helpers";
import { useAccountSelection } from "@pages/Login/modals/hooks/useAccountSelectionModal";
import { PagesContainer } from "../../pages.atoms";
import { useQueryClient } from "react-query";
import { QKEY_GET_MERCHANT_BY_ID } from "@constants/queryKeys";
 
interface Props {
  data_key: IStep;
  data?: BusinessDetails;
}
 
function BusinessDetailsComponent({ data, data_key }: Props, ref: any) {
  const { merchantId, img } = useGetCurrentMerchantId();
  const { accountList } = useAccountSelection();
  const account = accountList.find((el) => el.id === merchantId);
  const avatarImage = img || addSizeToImage(account?.img || "", "thumb") || "";
  const { mutateAsync } = usePatchBusinessDetailsMutation({
    merchantId,
  });
  const queryClient = useQueryClient();
  const handleSubmitDetails = async (
    formData: IBusinessDetailsFormValues | any,
    onParentValid?: SubmitHandler<IBusinessDetailsFormValues>,
    onParentInvalid?: (error: any) => void,
  ) => {
    const {
      website_url,
      merchant_phone_number,
      average_ticket_amount,
      high_ticket_amount,
      estimated_annual_revenue,
      billingDescriptor,
      business_social_media,
    } = formData?.data || {};
    await mutateAsync(
      {
        websiteURL: `https://${website_url}`,
        servicePhoneNumber: merchant_phone_number,
        averageTicketAmount: toNumber(average_ticket_amount),
        highTicketAmount: toNumber(high_ticket_amount),
        annualCreditCardSalesVolume: toNumber(estimated_annual_revenue),
        billingDescriptor: billingDescriptor,
        socialMediaURL: `https://${business_social_media}`,
      },
      {
        onSuccess(data) {
          onParentValid?.(formData.data);
          queryClient.invalidateQueries([QKEY_GET_MERCHANT_BY_ID, merchantId]);
        },
        onError: (error: any) => {
          onParentInvalid?.({ type: "api", error });
        },
      },
    );
  };
  const { methods } = useFormStep<IBusinessDetailsFormValues>(ref, {
    data_key: data_key,
    resolver: yupResolver(businessDetailsSchema),
    defaultValues: businessDetailsDefaultValues,
    onSubmitStepData: handleSubmitDetails,
  });
 
  useEffect(() => {
    methods.reset({
      website_url: data?.website_url || "",
      business_social_media: data?.business_social_media || "",
      estimated_annual_revenue: data?.estimated_annual_revenue || "",
      average_ticket_amount: data?.average_ticket_amount || "",
      high_ticket_amount: data?.high_ticket_amount || "",
      billingDescriptor: data?.billingDescriptor || "",
      merchant_phone_number: data?.merchant_phone_number || "",
    });
  }, [data]);
 
  return (
    <FormProvider {...methods}>
      <PagesContainer>
        <WebsiteInput
          name="website_url"
          label={"Business Website"}
          showCopy={false}
        />
 
        <WebsiteInput
          name="business_social_media"
          label="Business Social Media"
          showCopy={false}
        />
 
        <HFGiveTelephone
          label="Merchant Phone Number"
          name="merchant_phone_number"
          fullWidth
        />
        <HFGiveCustomAmount
          label="Estimated Annual Revenue"
          name={"estimated_annual_revenue"}
          placeholder="0"
          fixedDecimalScale
          decimalScale={2}
          currency="usd"
          bounded={true}
          max={MAX_AMOUNT_TARGET}
        />
        <HFGiveCustomAmount
          label="Average Ticket Amount"
          name={"average_ticket_amount"}
          placeholder="0"
          currency="usd"
          bounded={true}
          max={MAX_AMOUNT_TARGET}
          fixedDecimalScale
          decimalScale={2}
          min={0}
        />
        <HFGiveCustomAmount
          label="High Ticket Amount"
          name={"high_ticket_amount"}
          placeholder="0"
          min={0}
          currency="usd"
          bounded={true}
          max={MAX_AMOUNT_TARGET}
          fixedDecimalScale
          decimalScale={2}
        />
        <GiveDivider my="0" />
        <HFGiveInput
          name="billingDescriptor"
          label="Billing Descriptor"
          fullWidth
          defaultValue={data?.billingDescriptor}
          maxLength={BILLING_DESCRIPTOR_MAX_LENGTH}
          leftContent={
            <GiveText color="secondary" variant="bodyS">
              {data?.billingDescriptorPrefix || "GIV*"}
            </GiveText>
          }
        />
        <GiveStatementPreview
          merchantName={data?.billingDescriptor}
          cardSrc={MasterCard}
          date={data?.date}
          amount={data?.amount}
          imageUrl={avatarImage}
        />
      </PagesContainer>
    </FormProvider>
  );
}
BusinessDetailsComponent.displayName = "BusinessDetails";
export default forwardRef(BusinessDetailsComponent);