All files / widgets/WalletSelector/mobile WalletSelector.native.tsx

86.36% Statements 19/22
81.81% Branches 9/11
70% Functions 7/10
86.36% Lines 19/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 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                          1x                   5x 5x             5x   5x   5x         5x 1x                                   5x 5x 5x                   5x                                           5x 5x             9x   9x                                       5x 5x                                           5x                            
import { cn } from '@sb/libs';
import { WalletUiMessage } from '@sb/types';
import { FC, ReactNode, useMemo } from 'react';
import { WalletSelectorProps } from '../WalletSelector.types';
import { Platform, ScrollView, Text, View } from 'react-native';
import { useDeviceType } from '@sb/hooks/Utilities/useDeviceType';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import { useWalletSelector } from '@sb/api/hooks/Wallet/useWalletSelector';
import { WarningFilled } from '@sb/ui/components/atoms/Icons/mobile/Icons.native';
import { CheckBox } from '@sb/ui/components/atoms/CheckBox/mobile/CheckBox.native';
import { Account } from '@sb/ui/components/molecules/Account/mobile/Account.native';
import { DialogBox } from '@sb/ui/components/molecules/DialogBox/mobile/DialogBox.native';
 
export const WalletSelector: FC<WalletSelectorProps> = ({
  isOpen,
  onClose,
  accounts,
  className,
  onAccountSelect,
  onCheckBoxChange,
  isCheckBoxChecked,
  showDisclaimer = false,
}) => {
  const { themedColors } = useThemeColors();
  const { isTablet, orientation } = useDeviceType();
  const {
    handleCheckBoxClick,
    handleOnAccountClick,
    clearAllHookMessages,
    handleOnWalletActivate,
    useWalletSelectorMessage,
  } = useWalletSelector(onClose, onAccountSelect, onCheckBoxChange);
 
  const walletMessage: WalletUiMessage | null = useWalletSelectorMessage ?? null;
 
  const walletSelectorClasses = cn('bg-primary rounded-none pt-0 mt-0 w-full h-full', className, {
    'max-w-[50%] pb-10': isTablet && orientation === 'landscape',
    'pb-12': Platform.OS === 'android',
  });
 
  const Disclaimer = (): ReactNode => {
    return (
      <View
        role="alert"
        style={{ borderColor: themedColors.colorAccent }}
        className="flex-row items-start gap-2 rounded-lg border-2 px-2 py-4"
      >
        <WarningFilled color={themedColors.colorAccent} size={22} />
        <Text
          style={{ color: themedColors.colorText_Main }}
          className="w-[90%] text-base font-normal"
        >
          Wusstest du, dass du Geld von deiner Kundenkarte auf dein Online-Konto einzahlen kannst?
          Wähle "Online-Konto" aus und geh zur Einzahlung.
        </Text>
      </View>
    );
  };
 
  const Header = (): ReactNode => {
    const checkBoxLabel = (): ReactNode => (
      <View className="ml-3">
        <Text style={{ color: themedColors.colorText_Main }} className="font-Bold text-lg">
          Beim nächsten Login nicht mehr anzeigen.
        </Text>
        <Text style={{ color: themedColors.colorText_Main }} className="text-lg/6 font-normal">
          Das letzte verwendete Konto wird bei der nächsten Anmeldung wiederverwendet.
        </Text>
      </View>
    );
 
    return (
      <View className="mb-4 flex flex-col gap-6 px-2 py-1">
        <Text style={{ color: themedColors.colorText_Main }} className="font-Bold text-2xl">
          Mit welchem Konto möchtest du dich einloggen?
        </Text>
 
        {showDisclaimer && <Disclaimer />}
 
        <CheckBox
          id="show-popup-on-next-login"
          variant="secondary"
          label={checkBoxLabel()}
          checked={isCheckBoxChecked}
          onChange={handleCheckBoxClick}
          className="text-text-main text-xs"
          data-testid="show-popup-on-next-login"
          aria-label="Remember account selection for next login"
        />
      </View>
    );
  };
 
  const AccountSelector = (): ReactNode => {
    return (
      <View className="px-3">
        {accounts.length === 0 ? (
          <Text style={{ color: themedColors.colorText_Main }}>Keine Konten gefunden!</Text>
        ) : (
          <View className="gap-6">
            {accounts.map((account) => {
              const { _id, isActive } = account.wallet;
 
              return (
                <Account
                  {...account}
                  id={_id}
                  key={_id}
                  className="col-span-1"
                  showDepositButton={false}
                  showActivateButton={!isActive}
                  showDefaultHighlighting={true}
                  onAccountClick={() => handleOnAccountClick(_id)}
                  onWalletActivate={() => handleOnWalletActivate(_id)}
                />
              );
            })}
          </View>
        )}
      </View>
    );
  };
 
  const walletSelectorContent = useMemo((): ReactNode => {
    return (
      <ScrollView showsVerticalScrollIndicator={false}>
        {walletMessage && (
          <DialogBox
            id="wallet-message-response"
            closable={true}
            showCloseIcon={true}
            isOpen={!!walletMessage}
            showFooterButtons={false}
            backdropClickClose={true}
            title={walletMessage.title}
            content={walletMessage.message}
            variant={walletMessage.variant}
            onClose={() => clearAllHookMessages()}
          />
        )}
        <Header />
        <AccountSelector />
      </ScrollView>
    );
  }, [walletMessage, accounts, orientation]);
 
  return (
    <DialogBox
      id="native-wallet-selector"
      size="large"
      isOpen={isOpen}
      closable={false}
      onClose={onClose}
      showCloseIcon={false}
      backdropClickClose={false}
      content={walletSelectorContent}
      className={walletSelectorClasses}
    />
  );
};