All files / src/features/TransactionPanel/components/Customer TransactionCustomer.tsx

100% Statements 31/31
73.17% Branches 30/41
100% Functions 11/11
100% Lines 29/29

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265                                  52x                                   52x         96x   96x 1x 1x             96x 40x 40x 40x 600x         96x           96x   96x   192x           192x                     96x           96x       96x                                                                                                                                                                 52x       52x 340x   96x               52x 96x                             52x         96x                                                     96x                   96x                          
import { constructAddress } from "@features/TransactionPanel/utils";
import { Box, Grid, Stack } from "@mui/material";
import { CaretRightIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { CustomTheme } from "@theme/v2/palette.interface";
import { styled, useAppTheme } from "@theme/v2/Provider";
import CustomerLocation from "features/MerchantPortal/Customer/Modals/components/CustomerLocation";
import { toCountryName } from "hooks/helpers";
import { useCallback, useMemo } from "react";
import { Customer, CustomerAddress } from "types/customer.types";
import { SummaryContainerBase } from "../atoms";
 
type Props = {
  data: any;
  setIsCustomerPanelOpen: (isOpen: boolean, customer?: Customer) => void;
  isDisputePanel?: boolean;
};
const mergeFields = [
  "firstName",
  "lastName",
  "email",
  "billingAddressLine1",
  "billingAddressLine2",
  "billingAddressCity",
  "billingAddressState",
  "billingAddressZip",
  "billingAddressCountry",
  "shippingAddressLine1",
  "shippingAddressLine2",
  "shippingAddressCity",
  "shippingAddressState",
  "shippingAddressZip",
  "shippingAddressCountry",
] as const;
 
const TransactionCustomer = ({
  data,
  setIsCustomerPanelOpen,
  isDisputePanel = false,
}: Props) => {
  const theme = useAppTheme();
 
  const handleCustomerClick = () => {
    Eif (setIsCustomerPanelOpen) {
      setIsCustomerPanelOpen(true, {
        ...data?.customer,
        merchAccID: data?.customer?.userAccID,
      });
    }
  };
 
  const snapshot = useMemo(() => {
    const rawSnapshot = data?.customerReceiptSnapshot ?? {};
    const fallback = data?.customer ?? {};
    return Object.fromEntries(
      mergeFields.map((key) => [key, rawSnapshot?.[key] ?? fallback?.[key]]),
    ) as Record<(typeof mergeFields)[number], string | undefined>;
  }, [data?.customerReceiptSnapshot, data?.customer]);
 
  const customerName =
    [data?.customer?.firstName, data?.customer?.lastName]
      .filter(Boolean)
      .join(" ") ||
    [snapshot?.firstName, snapshot?.lastName].filter(Boolean).join(" ") ||
    "";
 
  const customerEmail = data?.customer?.email || snapshot?.email || "";
 
  const createAddress = useCallback(
    (identifier: "billing" | "shipping") => {
      const newAddressObj = {
        ...snapshot,
        billingAddressCountry: toCountryName(snapshot?.billingAddressCountry),
        shippingAddressCountry: toCountryName(snapshot?.shippingAddressCountry),
      } as unknown as Record<string, string>;
 
      return constructAddress(newAddressObj, identifier, [
        "line1",
        "line2",
        "city",
        "stateZip",
        "country",
      ]);
    },
    [snapshot],
  );
 
  const address = {
    add1: createAddress("shipping").address,
    add2: createAddress("billing").address,
  };
 
  const ipProfileSnapshot =
    data?.ipProfileSnapshot ||
    data?.originalTransaction?.ipProfileSnapshot ||
    {};
 
  return (
    <Stack spacing={isDisputePanel ? "16px" : "21px"}>
      <Stack
        flex={1}
        spacing={1}
        direction={"row"}
        py={isDisputePanel ? 0 : "8px"}
      >
        <GiveText variant="bodyM">Customer</GiveText>
      </Stack>
      <SummaryContainer>
        <ClickableCustomerInfo
          name={customerName}
          email={customerEmail}
          onClick={handleCustomerClick}
        />
        <DetailsContainer theme={theme} isHideBorder={false}>
          {(snapshot?.billingAddressLine1 ||
            snapshot?.shippingAddressLine1) && (
            <Grid container spacing={2} alignItems="center" pb={2}>
              {/* Delivery Address */}
              {snapshot?.billingAddressLine1 && (
                <>
                  <Grid item xs={6}>
                    <GiveText variant="bodyS" color="secondary">
                      {address.add1 === "Not Provided" ? "Billing" : "Delivery"}{" "}
                      Address
                    </GiveText>
                  </Grid>
                  <Grid item xs={6}>
                    <Stack spacing={0.5}>
                      <GiveText variant="bodyS">
                        {address.add1 === "Not Provided"
                          ? address.add2
                          : address.add1}
                      </GiveText>
                    </Stack>
                  </Grid>
                </>
              )}
 
              {snapshot?.shippingAddressLine1 && (
                <>
                  {/* Shipping Address */}
                  <Grid item xs={6}>
                    <GiveText variant="bodyS" color="secondary">
                      {address.add2 === "Not Provided" ? "Delivery" : "Billing"}{" "}
                      Address
                    </GiveText>
                  </Grid>
                  <Grid item xs={6}>
                    <Stack spacing={0.5}>
                      <GiveText variant="bodyS">
                        {address.add2 === "Not Provided"
                          ? address.add1
                          : address.add2}
                      </GiveText>
                    </Stack>
                  </Grid>
                </>
              )}
            </Grid>
          )}
          <Box pt={0}>
            <CustomerLocation
              hideTitle={true}
              address={
                getTransactionAddress(
                  data?.customerReceiptSnapshot,
                ) as CustomerAddress
              }
              lastKnownLatitude={ipProfileSnapshot?.ipInfo?.latitude}
              lastKnownLongitude={ipProfileSnapshot?.ipInfo?.longitude}
            />
          </Box>
        </DetailsContainer>
      </SummaryContainer>
    </Stack>
  );
};
 
const SummaryContainer = styled(SummaryContainerBase)({
  padding: "0 20px",
});
 
const DetailsContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isHideBorder",
})(
  ({ theme, isHideBorder }: { theme: CustomTheme; isHideBorder: boolean }) => ({
    padding: "16px 0",
    borderTop: isHideBorder
      ? "none"
      : `1px solid ${theme.palette?.border?.primary}`,
  }),
);
 
const getTransactionAddress = (customerReceiptSnapshot: any) => {
  return {
    line1: customerReceiptSnapshot?.billingAddressLine1,
    line2: customerReceiptSnapshot?.billingAddressLine2,
    city: customerReceiptSnapshot?.billingAddressCity,
    state: customerReceiptSnapshot?.billingAddressState,
    zip: customerReceiptSnapshot?.billingAddressZip,
    country: customerReceiptSnapshot?.billingAddressCountry,
  };
};
 
type ClickableCustomerInfoProps = {
  name: string;
  email: string;
  onClick?: () => void;
};
const ClickableCustomerInfo = ({
  name,
  email,
  onClick,
}: ClickableCustomerInfoProps) => {
  return (
    <Container onClick={onClick}>
      <Stack spacing={0.5}>
        {Boolean(name) && (
          <GiveText
            variant="bodyS"
            color="primary"
            sx={{ wordBreak: "break-all" }}
          >
            {name}
          </GiveText>
        )}
        <GiveText
          variant="bodyS"
          color="secondary"
          sx={{ wordBreak: "break-all" }}
        >
          {email}
        </GiveText>
      </Stack>
      <CaretWrapper>
        <CaretRightIcon size={20} color="gray" />
      </CaretWrapper>
    </Container>
  );
};
 
const Container = styled(Box)(({ theme }) => ({
  display: "flex",
  justifyContent: "space-between",
  alignItems: "center",
  padding: "16px 16px 16px 0px",
  borderRadius: theme.customs?.radius?.medium,
  backgroundColor: theme.palette.background.paper,
  cursor: "pointer",
}));
 
const CaretWrapper = styled(Box)(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  padding: "4px",
  borderRadius: "4px",
  transition: "background-color 0.2s ease",
  "&:hover": {
    backgroundColor: "rgba(0, 0, 0, 0.05)", // subtle highlight on Caret only
  },
}));
 
export default TransactionCustomer;