All files / src/pages/Login/modals GiveAccountSelectionModal.tsx

100% Statements 14/14
100% Branches 12/12
100% Functions 6/6
100% Lines 13/13

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                              2x                               16x   16x 6x   4x 1x 2x   1x   3x         16x                                                                                 24x         1x                                                               20x                            
import { useEffect } from "react";
import NiceModal from "@ebay/nice-modal-react";
import { Box, Stack } from "@mui/material";
import { useAccountSelectionModal } from "@pages/Login/modals/hooks/useAccountSelectionModal";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { styled } from "@theme/v2/Provider";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { addSizeToImage } from "@utils/image.helpers";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
import { ThumbnailType } from "@shared/Thumbnail/getIconType";
import GiveText from "@shared/Text/GiveText";
import GiveButton from "@shared/Button/GiveButton";
import { CheckIcon } from "@phosphor-icons/react";
import { capitalizeFirstLetter } from "@utils/index";
 
const GiveAccountSelectionModal = NiceModal.create(
  ({
    userAccID,
    preSelectedAccount,
  }: {
    userAccID: number;
    preSelectedAccount: any;
  }) => {
    const {
      modal,
      accountList,
      handleModalClose,
      continueHandler,
      chooseAccount,
      selectedAccountId,
      isLoadingAccounts,
    } = useAccountSelectionModal({ userAccID });
 
    useEffect(() => {
      if (accountList?.length > 0 && !isLoadingAccounts) {
        // if coming from mobile and has preselected account -> select it by default
        if (preSelectedAccount) {
          const selectedAccountIndex = accountList.findIndex(
            (acc) => acc.id === preSelectedAccount?.id,
          );
          chooseAccount(selectedAccountIndex);
        } else {
          chooseAccount(0);
        }
      }
    }, [accountList?.length]);
 
    return (
      <GiveBaseModal
        open={modal.visible}
        onClose={handleModalClose}
        title="Account Selection"
        width="480px"
        height="auto"
        customContentPadding="84px 0px 20px 12px"
        customDialogStyle={{
          "& .MuiDialogContent-root": {
            overflowY: { xs: "auto", sm: "hidden" },
          },
        }}
        showFooter
        buttons={
          <Box
            sx={{
              display: "flex",
              direction: "row",
              justifyContent: "flex-end",
            }}
          >
            <GiveButton
              label="Continue"
              size="large"
              variant="filled"
              disabled={!selectedAccountId}
              onClick={continueHandler}
            />
          </Box>
        }
      >
        <Stack
          sx={{
            paddingRight: "10px",
            maxHeight: "485px",
            overflow: "auto",
            marginBottom: "55px",
          }}
        >
          {accountList?.map((account, index) => {
            return (
              <StyledAccountItem
                data-testid="account_selection_modal_login"
                key={index}
                onClick={() => {
                  !preSelectedAccount && chooseAccount(index);
                }}
              >
                <GiveThumbnail
                  imageUrl={
                    account.img ? addSizeToImage(account.img, "thumb") : ""
                  }
                  size="small_plus"
                  type={account.merchType.toLowerCase() as ThumbnailType}
                />
                <Stack gap="4px">
                  <GiveTruncateText variant="bodyS" lineClamp={1}>
                    {account.name}
                  </GiveTruncateText>
                  <GiveText variant="bodyXS" color="secondary">
                    {capitalizeFirstLetter(account.merchType)}
                  </GiveText>
                </Stack>
                {account.selected && (
                  <Box sx={{ marginLeft: "auto" }}>
                    <CheckIcon size={20} />
                  </Box>
                )}
              </StyledAccountItem>
            );
          })}
        </Stack>
      </GiveBaseModal>
    );
  },
);
 
const StyledAccountItem = styled(Box)(({ theme }) => ({
  display: "flex",
  flexDirection: "row",
  alignItems: "center",
  padding: "8px",
  gap: "16px",
  cursor: "pointer",
  borderRadius: "12px",
  "&:hover": {
    backgroundColor: theme.palette.primitive?.transparent["darken-5"],
  },
}));
 
export default GiveAccountSelectionModal;