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

100% Statements 33/33
58.33% Branches 7/12
100% Functions 15/15
100% Lines 33/33

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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301                                                1x               5x           5x   5x 5x                 8x                           8x                                                                                           5x 5x 5x 5x                 5x 10x 2x                       10x     5x             5x                       5x 5x                                           5x   5x 8x   8x 16x 48x 48x                                           48x       48x     16x 24x                                                                   8x     16x                                                 5x                                      
import { FC, ReactNode } from 'react';
import { useAccountTransactions } from '@sb/hooks';
import { Platform, Text, View } from 'react-native';
import { Button } from '@sb/ui/components/atoms/Button/mobile';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import { ListGroup } from '@sb/ui/components/atoms/ListGroup/mobile';
import { Accordion } from '@sb/ui/components/molecules/Accordion/mobile';
import { TransactionScrollView } from './Components/TransactionScrollView';
import { AccordionNativeProps } from '@sb/ui/components/molecules/Accordion';
import { Separator } from '@sb/ui/components/atoms/Separator/mobile/Separator.native';
import { cn, parseFormattedNativeContent, formatCurrency } from '@sb/libs/utils.native';
import { DialogBox } from '@sb/ui/components/molecules/DialogBox/mobile/DialogBox.native';
import { AccountSummaryProps, IconVariant, TransactionEntry } from '../AccountSummary.types';
import {
  BettingSlip,
  CashoutRotate,
  Gift,
  Graph,
  Minus,
  Plus,
  TransferDeposit,
  TransferWithdraw,
} from '@sb/ui/components/atoms/Icons/mobile';
 
export const AccountSummary: FC<AccountSummaryProps> = ({
  isOpen,
  account,
  onClose,
  className,
  openAnimation = 'slide',
  onBackdropClickClose = false,
}) => {
  const { themedColors } = useThemeColors();
  const {
    losses,
    earnings,
    accountTransactions,
    accountTotal: totalBalance,
  } = useAccountTransactions(account);
 
  const WalletSummary = (): ReactNode => {
    return (
      <View style={{ backgroundColor: themedColors.alternateBackground }} className="gap-5 p-3">
        <Text style={{ color: themedColors.colorPrimary }} className="font-Bold mt-1 text-lg">
          Deine Übersicht der letzten 30 Tage
        </Text>
 
        {/* Earnings */}
        <View className="gap-1.5">
          {earnings.map((earning, index) => (
            <View key={index} className="w-full flex-row justify-between">
              <Text style={{ color: themedColors.colorPrimary }} className="font-normal">
                {earning.transaction}
              </Text>
              <Text style={{ color: themedColors.colorPrimary }} className="font-Bold">
                {formatCurrency(earning.amount)}
              </Text>
            </View>
          ))}
        </View>
 
        {/* Losses */}
        <View className="gap-1.5">
          {losses.map((loss, index) => (
            <View key={index} className="w-full flex-row justify-between">
              <Text style={{ color: themedColors.colorPrimary }} className="font-normal">
                {loss.transaction}
              </Text>
              <Text style={{ color: themedColors.colorPrimary }} className="font-Bold">
                {formatCurrency(loss.amount)}
              </Text>
            </View>
          ))}
        </View>
 
        <Separator
          orientation="horizontal"
          id="wallet-summary-separator"
          color={themedColors.colorPrimary}
          style={{ width: '100%', opacity: 0.3 }}
        />
 
        <View className="w-full flex-row items-center justify-between">
          <Text style={{ color: themedColors.colorPrimary }} className="font-Bold">
            Gewinn/Verlust
          </Text>
          <Text style={{ color: themedColors.colorPrimary }} className="font-Bold">
            {formatCurrency(totalBalance)}
          </Text>
        </View>
 
        <View className="my-4 w-full items-center">
          <Text style={{ color: themedColors.colorPrimary }} className="text-sm font-normal">
            Glücksspiel kann süchtig machen.
          </Text>
          <Text
            style={{ color: themedColors.colorPrimary }}
            className="text-center text-sm font-normal"
          >
            {parseFormattedNativeContent(
              'Spiele verantwortungsbewusst. Mehr Information findest du <underline>hier</underline>.',
              'underline',
              'underline'
            )}
          </Text>
        </View>
      </View>
    );
  };
 
  const WalletTransactions = (): ReactNode => {
    const accordionItems: AccordionNativeProps['items'] = ['Kontobewegungen'].map((title) => {
      const AccordionLabel = (): ReactNode => {
        return (
          <View className="flex-row items-center gap-4">
            <Graph color={themedColors.colorPrimary} size={10} />
            <Text style={{ color: themedColors.colorPrimary }} className="text-lg font-normal">
              {title}
            </Text>
          </View>
        );
      };
      const AccordionContent = (): ReactNode => {
        const NoTransactionLayout = (): ReactNode => {
          return (
            <View className="w-full items-center">
              <Text
                style={{ color: themedColors.colorPrimary }}
                className="pb-6 pt-2 text-center font-normal"
              >
                Keine Transaktionen verfügbar.
              </Text>
            </View>
          );
        };
 
        return accountTransactions.length == 0 ? <NoTransactionLayout /> : <TransactionsLayout />;
      };
 
      return {
        className: '!p-0',
        label: <AccordionLabel />,
        content: <AccordionContent />,
      };
    });
 
    return (
      <View style={{ backgroundColor: themedColors.alternateBackground }} className="px-4">
        <Accordion
          variant="primary"
          items={accordionItems}
          headerClassName="bg-alternate-background py-4"
          icon={<Plus color={themedColors.colorPrimary} />}
          activeIcon={<Minus color={themedColors.colorPrimary} />}
        />
      </View>
    );
  };
  const SummaryFooter = (): ReactNode => {
    return (
      <View
        className={`w-full p-4`}
        style={{
          paddingBottom: Platform.OS === 'ios' ? 40 : 20,
          backgroundColor: themedColors.alternateBackground,
        }}
        // Accessibility
        accessible={true}
        accessibilityLabel={'Habe ich zur Kenntnis genommen'}
        accessibilityHint={'Click to close account summary modal'}
      >
        <Button
          label="Habe ich zur Kenntnis genommen"
          onPress={onClose}
          variant="secondary"
          textStyle="uppercase py-1.5 tracking-wide font-Bold"
        />
      </View>
    );
  };
 
  const dialogBoxClasses = cn('bg-primary', className);
 
  const TransactionsLayout = (): ReactNode => {
    const paddingLeft = 'pl-2';
 
    const RenderTransactions = (transactions: TransactionEntry[]): ReactNode[] => {
      const RenderTransactionIcon = (icon: IconVariant) => {
        const iconColor = themedColors.colorPrimary;
        const iconMap = {
          gift: { icon: <Gift size={20} color={iconColor} />, label: 'Gift' },
          betting: { icon: <BettingSlip size={20} color={iconColor} />, label: 'Betting' },
          cashout: { icon: <CashoutRotate size={20} color={iconColor} />, label: 'Cashout' },
          increase: {
            icon: <TransferDeposit size={20} color={iconColor} />,
            label: 'Deposit',
          },
          decrease: {
            icon: <TransferWithdraw size={20} color={iconColor} />,
            label: 'Withdrawal',
          },
          debit: {
            icon: <Plus size={10} color={iconColor} />,
            label: 'Deposit',
          },
          credit: {
            icon: <Minus size={10} color={iconColor} />,
            label: 'Withdrawal',
          },
        };
 
        const { icon: transactionIcon, label } = iconMap[icon] || {
          icon: <Plus size={20} color={iconColor} />,
          label: 'Transaction',
        };
        return <View aria-label={label}>{transactionIcon}</View>;
      };
 
      return transactions.map((transaction, index) => (
        <View key={index} className="flex flex-row items-center p-3">
          <View className="flex-row items-center gap-4">
            {RenderTransactionIcon(transaction.icon)}
            <View>
              <Text style={{ color: themedColors.colorPrimary }} className="font-normal">
                {transaction.time}
              </Text>
              <View className="flex flex-row items-center gap-1">
                {transaction.descriptionIcon &&
                  RenderTransactionIcon(transaction.descriptionIcon as IconVariant)}
                <Text
                  style={{ color: themedColors.colorPrimary }}
                  className="font-Bold text-lg tracking-wide"
                >
                  {transaction.description}
                </Text>
                {transaction.additionalInfo && (
                  <Text style={{ color: themedColors.colorPrimary }} className="font-normal">
                    {transaction.additionalInfo}
                  </Text>
                )}
              </View>
            </View>
          </View>
          <View className="ml-auto flex-row items-center gap-2">
            {RenderTransactionIcon(transaction.type as IconVariant)}
            <Text style={{ color: themedColors.colorPrimary }} className="font-Bold text-lg">
              {formatCurrency(transaction.amount)}
            </Text>
          </View>
        </View>
      ));
    };
 
    return (
      <TransactionScrollView>
        {accountTransactions.map((datedTransactions, index) => {
          return (
            <View key={index} className="mr-3">
              <Text
                style={{ color: themedColors.colorPrimary }}
                className={`${paddingLeft} font-Bold py-3 !pl-6 text-lg`}
              >
                {datedTransactions.date}
              </Text>
 
              {/* Dated Transaction List */}
              <ListGroup
                items={RenderTransactions(datedTransactions.transactions)}
                variant="striped"
                stripedColors={{
                  even: `bg-odd-transaction-background`,
                  odd: `bg-alternate-background`,
                }}
              />
            </View>
          );
        })}
      </TransactionScrollView>
    );
  };
 
  return (
    <DialogBox
      id="account-summary-dialogbox"
      isOpen={isOpen}
      closable={false}
      onClose={onClose}
      paddingSize="none"
      addContentSpacing={true}
      animation={openAnimation}
      className={dialogBoxClasses}
      content={<WalletTransactions />}
      headerTemplate={<WalletSummary />}
      footerTemplate={<SummaryFooter />}
      backdropClickClose={onBackdropClickClose}
      // Accessibility
      accessibilityLabel={'Account Summary Dialog'}
    />
  );
};