# All files / src/hooks useBetSlip.ts

84.34% Statements 97/115
62.5% Branches 10/16
63.63% Functions 7/11
84.34% Lines 97/115

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
    1801x
    1x

    1x

    1x

    1x
    38x
    38x
    38x

    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x

    38x
    7x
    7x

    38x
    2x
    2x

    38x

    38x
    2x
    2x

    38x
    6x
    6x

    38x

    38x
    2x
    2x
    2x

    38x
    2x

    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    2x
    2x

    38x

    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x

    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
     import { useSelector, useDispatch } from 'react-redux';
    import { useState } from 'react';
    import { RootState, AppDispatch } from '../store';
    import {
      addLeg,
      removeLeg,
      updateLeg,
      setBetType,
      setStake,
      setTeaserPoints,
      clearSlip,
      selectLegs,
      selectFutureLegs,
      selectBetType,
      selectStake,
      selectTeaserPoints,
      selectCombinedOdds,
      selectPotentialPayout,
      selectPotentialProfit,
      selectIsValid,
      removeFutureLeg,
      updateFutureLeg
    } from '../store/betSlipSlice';
    import { BetLeg, FutureLeg } from '../types/game.types';
    import api from '../services/api';

    interface SubmitBetParams {
      name: string;
    }

    /**
     * Custom hook for bet slip operations
     */
    export function useBetSlip() {
      const dispatch = useDispatch<AppDispatch>();
      const [isSubmitting, setIsSubmitting] = useState(false);
      const [submitError, setSubmitError] = useState<string | null>(null);

      // Selectors
      const legs = useSelector(selectLegs);
      const futureLegs = useSelector(selectFutureLegs);
      const betType = useSelector(selectBetType);
      const stake = useSelector(selectStake);
      const teaserPoints = useSelector(selectTeaserPoints);
      const combinedOdds = useSelector(selectCombinedOdds);
      const potentialPayout = useSelector(selectPotentialPayout);
      const potentialProfit = useSelector(selectPotentialProfit);
      const isValid = useSelector(selectIsValid);

      // Actions
      const handleAddLeg = (leg: BetLeg) => {
        dispatch(addLeg(leg));
      };

      const handleRemoveLeg = (index: number) => {
        dispatch(removeLeg(index));
      };

      const handleUpdateLeg = (index: number, updates: Partial<BetLeg>) => {
        dispatch(updateLeg({ index, updates }));
      };

      const handleSetBetType = (type: 'single' | 'parlay' | 'teaser') => {
        dispatch(setBetType(type));
      };

      const handleSetStake = (amount: number) => {
        dispatch(setStake(amount));
      };

      const handleSetTeaserPoints = (points: number) => {
        dispatch(setTeaserPoints(points));
      };

      const handleClearSlip = () => {
        dispatch(clearSlip());
        setSubmitError(null);
      };

      /**
       * Submit bet to API
       * @param name - Bet name
       * @param boostedCombinedOdds - Optional boosted combined odds for parlay
       */
      const submitBet = async ({ name, boostedCombinedOdds }: SubmitBetParams & { boostedCombinedOdds?: number }): Promise<boolean> => {
        if (!isValid) {
          setSubmitError('Invalid bet configuration');
          return false;
        }

        setIsSubmitting(true);
        setSubmitError(null);

        try {
          console.log('Submitting bet:', {
            betType,
            stake,
            boostedCombinedOdds,
            legs: legs.map(l => ({ gameId: l.gameId, odds: l.odds }))
          });

          // Prepare bet data
          const betData: any = {
            name: name.trim(),
            betType,
            stake,
            legs: legs.map((leg) => ({
              gameId: leg.gameId,
              selectionType: leg.selectionType,
              selection: leg.selection,
              odds: Math.round(leg.odds), // Always use original odds
              line: leg.line
            }))
          };

          // Only add boostedCombinedOdds if it's defined
          if (boostedCombinedOdds !== undefined) {
            betData.boostedCombinedOdds = boostedCombinedOdds;
            console.log('Adding boostedCombinedOdds to request:', boostedCombinedOdds);
          }

          // Include futures legs if present
          if (futureLegs.length > 0) {
            betData.futureLegs = futureLegs.map((futureLeg) => ({
              futureId: futureLeg.futureId,
              outcome: futureLeg.outcome,
              odds: Math.round(futureLeg.odds) // Always use original odds
            }));
          }

          // If teaser, include teaser points
          if (betType === 'teaser') {
            betData.teaserPoints = teaserPoints;
          }

          // Submit to API
          const response = await api.post('/bets', betData);

          // Clear slip on success
          handleClearSlip();

          return true;
        } catch (error: any) {
          const errorMessage =
            error.response?.data?.message || 'Failed to place bet';
          setSubmitError(errorMessage);
          return false;
        } finally {
          setIsSubmitting(false);
        }
      };

      return {
        // State
        legs,
        futureLegs,
        betType,
        stake,
        teaserPoints,
        combinedOdds,
        potentialPayout,
        potentialProfit,
        isValid,
        isSubmitting,
        submitError,

        // Actions
        addLeg: handleAddLeg,
        removeLeg: handleRemoveLeg,
        updateLeg: handleUpdateLeg,
        removeFutureLeg: (futureId: string) => dispatch(removeFutureLeg(futureId)),
        updateFutureLeg: (futureId: string, updates: Partial<FutureLeg>) => dispatch(updateFutureLeg({ futureId, updates })),
        setBetType: handleSetBetType,
        setStake: handleSetStake,
        setTeaserPoints: handleSetTeaserPoints,
        clearSlip: handleClearSlip,
        submitBet
      };
    }