All files / src/sections/PayBuilder/Forms/DateAndLocation LocationSection.tsx

75% Statements 18/24
66.66% Branches 14/21
50% Functions 6/12
73.91% Lines 17/23

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 181 182 183 184 185 186 187 188 189 190 191 192 193                                      45x 45x 133x 133x 133x   133x 133x             133x   133x 133x 133x 944x           133x 133x                       18x     18x                                                                                                                                                                                                                       2x                                                
import { useMemo } from "react";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { Location_position } from "@sections/PayBuilder/provider/provider.type";
import ErrorCatcher from "@common/Error/ErrorCatcher";
import { GoogleMaps } from "@services/google-api";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import useLoadGoogleScript from "@hooks/google-api/useLoadGoogleScript";
import { Stack } from "@mui/material";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import GiveMapsAutocomplete from "@shared/GoogleMapsAutocomplete/GiveMapsAutocomplete";
import { HFCheckbox } from "@shared/HFInputs/HFGiveCheckbox/HFGiveCheckbox";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import useCountryCodes from "@hooks/common/useCountryCodes";
import { getUSNames } from "@utils/country_dial_codes";
import GiveText from "@shared/Text/GiveText";
 
interface LocationSectionProps {
  locationPosition: Location_position;
}
const prefix = "https://";
const LocationSection = ({ locationPosition }: LocationSectionProps) => {
  const { methods } = usePayBuilderForm();
  const { setValue, watch, trigger } = methods;
  const values = watch();
 
  const { isLoaded, loadError } = useLoadGoogleScript();
  const handleLocationChange = (value: string) => {
    setValue("DateLocation.locationShortAddress", value, {
      shouldDirty: true,
      shouldValidate: true,
    });
  };
 
  const { selectCountriesList } = useCountryCodes();
 
  const locationString = values.DateLocation?.locationShortAddress;
  const isManualAddress = values.DateLocation.isManualAddress;
  const countryOptions = useMemo(() => {
    return getUSNames().map((state) => ({
      value: state,
      label: state,
    }));
  }, []);
  const isCountryUS =
    methods.watch("DateLocation.locationAddress.country") === "US";
  return (
    <>
      {locationPosition === "online" && (
        <HFGiveInput
          name="DateLocation.locationURL"
          placeholder={""}
          leftContent={
            <GiveText variant="bodyS" color="secondary">
              {prefix}
            </GiveText>
          }
          onValidate={() => {
            trigger("DateLocation.locationURL");
          }}
          handleNormalizeInput={(value: string) => {
            return value.replace("http://", "https://").replace("https://", "");
          }}
        />
      )}
      {locationPosition === "onsite" && (
        <ErrorCatcher errorID="GooglePlacesAutocomplete">
          <GiveMapsAutocomplete
            disabled={isManualAddress}
            initialValue={values.DateLocation?.locationShortAddress}
            onValueChange={handleLocationChange}
            isLoaded={isLoaded}
            error={Boolean(
              methods.formState.errors.DateLocation?.locationShortAddress,
            )}
            errorText={
              methods.formState.errors.DateLocation?.locationShortAddress
                ?.message
            }
          />
        </ErrorCatcher>
      )}
      {locationPosition === "onsite" &&
        !isManualAddress &&
        values.DateLocation.locationShortAddress && (
          <Stack direction="column" gap={2}>
            {!isLoaded ? (
              <LoadingSpinner />
            ) : (
              <ErrorCatcher errorID="GoogleMaps">
                <GoogleMaps
                  address={locationString ?? undefined}
                  isLoaded={isLoaded}
                  containerStyle={{ height: "170px" }}
                />
              </ErrorCatcher>
            )}
          </Stack>
        )}
 
      {locationPosition === "onsite" && (
        <HFCheckbox
          name="DateLocation.isManualAddress"
          label="Enter address manually"
        />
      )}
 
      {isManualAddress && locationPosition === "onsite" && (
        <Stack py="16px" gap="16px">
          <HFGiveSelect
            name="DateLocation.locationAddress.country"
            label="Country/Region"
            placeholder="Select a country"
            options={selectCountriesList}
            contextualMenuProps={{
              useVirtualList: true,
            }}
          />
          <HFGiveInput
            name="DateLocation.locationAddress.line1"
            label="Address"
            onValidate={() => {
              trigger("DateLocation.locationAddress.line1");
            }}
          />
 
          <HFGiveInput
            name="DateLocation.locationAddress.line2"
            label="Apartment, Suite etc (Optional)"
            onValidate={() => {
              trigger("DateLocation.locationAddress.line2");
            }}
          />
          <Stack direction="row" gap="16px">
            <HFGiveInput
              name="DateLocation.locationAddress.city"
              label="City"
              onValidate={() => {
                trigger("DateLocation.locationAddress.city");
              }}
            />
            {isCountryUS ? (
              <HFGiveSelect
                name="DateLocation.locationAddress.state"
                label="State"
                options={countryOptions}
                onChange={(e) => {
                  methods.setValue(
                    "DateLocation.locationAddress.state",
                    e.target.value,
                    { shouldValidate: true },
                  );
                }}
              />
            ) : (
              <HFGiveInput
                name="DateLocation.locationAddress.state"
                label="State"
                onValidate={() => {
                  trigger("DateLocation.locationAddress.state");
                }}
              />
            )}
 
            <HFGiveInput
              name="DateLocation.locationAddress.zip"
              label="Zip code"
              type={isCountryUS ? "number" : "text"}
              onChange={(e: any) => {
                methods.setValue(
                  "DateLocation.locationAddress.zip",
                  e.target.value,
                  {
                    shouldValidate: true,
                  },
                );
              }}
              error={Boolean(
                methods.formState.errors.DateLocation?.locationAddress?.zip,
              )}
              helperText={
                methods.formState.errors.DateLocation?.locationAddress?.zip
                  ?.message
              }
            />
          </Stack>
        </Stack>
      )}
    </>
  );
};
 
export default LocationSection;