All files / organisms/BetPackerTable/mobile BetPackerTable.native.tsx

93.02% Statements 40/43
82.05% Branches 32/39
92.3% Functions 12/13
92.85% Lines 39/42

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                                  1x         3x 3x   3x 3x   3x             6x               6x         6x   3x           3x 3x 3x                                                             6x             6x       6x   6x 15x 9x 3x     6x 3x       9x 6x 3x     3x 3x         6x     15x 15x                                               15x   15x       15x                                                                           15x   3x   3x   9x       3x       3x                            
import React from 'react';
import {
  BetPackerOddButtonProps,
  BetPackerTableData,
  BetPackerTableNativeProps,
} from './BetPackerTable.native.types';
import { Table } from '@sb/ui/components/organisms/Table/mobile/Table.native';
import { Lock } from '@sb/ui/components/atoms/Icons/mobile/Icons.native';
import { Dimensions, Pressable, Text, View } from 'react-native';
import { cn } from '@sb/libs';
import { colors } from '@sb/styles/colors';
import { ColumnProps } from '@sb/ui/components/organisms/Table/mobile/Table.native.types';
import { useTheme } from '@sb/ui/components/Themes/ThemeProvider';
import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons';
import { BetPackerSkeleton } from './BetPackerSkeleton.native';
import { useBetPacker } from '@sb/hooks';
 
export const BetPackerTable: React.FC<BetPackerTableNativeProps> = ({
  data,
  showHeader,
  isLoading,
}) => {
  const { theme } = useTheme();
  const { selectBet, selectedBetId } = useBetPacker();
 
  const SCREEN_WIDTH = Dimensions.get('screen').width;
  const ODDS_COL_WIDTH = SCREEN_WIDTH - SCREEN_WIDTH / 2.2;
 
  const columns: ColumnProps[] = [
    {
      key: 'name',
      title: 'Name',
      dataIndex: 'name',
      width: SCREEN_WIDTH / 2.2,
      render: (value) => (
        <View className={'flex flex-1 px-2'}>{nameTemplate(value as string)}</View>
      ),
    },
    {
      key: 'odds',
      title: 'Odds',
      dataIndex: 'odds',
      width: ODDS_COL_WIDTH,
      render: (value) => <Text className="text-text-main text-center">{value}</Text>,
    },
  ];
 
  function nameTemplate(value: string) {
    switch (value) {
      case 'Tipp':
        return (
          <Text testID="bet-packer-table-tip" className="text-text-main font-Bold text-left">
            {value}
          </Text>
        );
      default: {
        Eif (value.match(/(Unter\/Ober|Handicap)-\d+(\.\d+)?/)) {
          const [part1, part2] = value.split('-');
          return (
            <View
              style={{ width: SCREEN_WIDTH / 2.3 }}
              testID="bet-packer-table-split-name"
              className="flex flex-row justify-between"
            >
              <Text
                className={'text-text-main font-Bold text-left'}
                testID="bet-packer-table-split-name-part1"
              >
                {part1}
              </Text>
              <Text
                className={'text-text-main font-Bold text-left'}
                testID="bet-packer-table-split-name-part2"
              >
                {part2}
              </Text>
            </View>
          );
        }
        return (
          <Text testID="bet-packer-table-name" className="text-text-main font-Bold text-left">
            {value}
          </Text>
        );
      }
    }
  }
 
  function processData(data: BetPackerTableData[]) {
    return data.map((item, index) => ({
      name: item.name,
      odds: renderOddsTemplate(item.odds, index),
    }));
  }
 
  function renderOddsTemplate(odds: BetPackerTableData['odds'], index: number) {
    return <BetPackerOddsButtons odds={odds} idx={index} />;
  }
 
  function BetPackerOddsButtons({ odds, idx }: { odds: BetPackerTableData['odds']; idx: number }) {
    const oddsLength = odds.length;
 
    const getButtonClassName = (index: number) => {
      if (oddsLength === 3) {
        if (index === 0) {
          return 'rounded-l-[4px]';
        }
 
        if (index === 2) {
          return 'rounded-r-[4px]';
        }
      }
 
      if (oddsLength === 2) {
        if (index === 0) {
          return 'rounded-l-[4px]';
        }
 
        Eif (index === 1) {
          return 'rounded-r-[4px]';
        }
      }
    };
 
    return (
      <View className="flex w-full flex-row gap-[1px] px-2 py-0.5">
        {odds.map((odd, index) => {
          const isLocked = odd.isLocked || false;
          return (
            <BetPackerOddsButton
              key={odd.type}
              type={odd.type}
              value={odd.value}
              id={`bet-btn-${idx}-${index}`}
              isLocked={isLocked}
              className={getButtonClassName(index)}
              oddsLength={oddsLength}
            />
          );
        })}
      </View>
    );
  }
 
  function BetPackerOddsButton({
    type,
    value,
    isLocked,
    className,
    oddsLength,
    id,
  }: BetPackerOddButtonProps) {
    const isSelected = selectedBetId.includes(id);
 
    const iconColor = !isSelected
      ? colors[theme].colorIcon_Fill_Light
      : colors[theme].colorIcon_Fill_Dark;
 
    return (
      <Pressable
        key={id}
        onPress={() => {
          selectBet(isSelected, value, id);
        }}
        style={{
          flex: 1,
          width: oddsLength === 2 ? ODDS_COL_WIDTH / 2.2 : ODDS_COL_WIDTH / 3.3,
        }}
        className={cn(
          'bg-tertiary mt-2 flex h-[45px] flex-row items-center justify-between px-4 py-2',
          className,
          isLocked && 'pointer-events-none',
          isSelected && 'bg-accent text-primary'
        )}
      >
        <Text
          className={cn(
            'text-text-main font-normal',
            isSelected && 'text-primary',
            isLocked && 'text-text-main'
          )}
        >
          {renderOddsType(type, iconColor)}
        </Text>
        {!isLocked ? (
          <Text className={cn('text-text-main font-normal', isSelected && 'text-primary')}>
            {value}
          </Text>
        ) : (
          <Lock className="h-4 w-4" testID="bet-packer-table-lock-icon" color={iconColor} />
        )}
      </Pressable>
    );
  }
 
  function renderOddsType(type: string, iconColor: string) {
    switch (type) {
      case '+':
        return <MaterialCommunityIcons name={'plus'} size={16} color={iconColor} />;
      case '-':
        return <MaterialCommunityIcons name={'minus'} size={16} color={iconColor} />;
      default:
        return type;
    }
  }
 
  Iif (isLoading) {
    return <BetPackerSkeleton row={4} />;
  }
 
  return (
    <Table
      id={'bet-packer-table'}
      width={'100%'}
      variant={'striped'}
      emptyMessageClassName={'text-text-main p-4'}
      size={'xs'}
      columns={columns}
      data={processData(data)}
      expandableHeader={showHeader}
      rowClassName={'items-center'}
    />
  );
};