# All files / src/pages BetHistory.tsx

0% Statements 0/217
0% Branches 0/1
0% Functions 0/1
0% Lines 0/217

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

     import React, { useState, useEffect } from 'react';
    import BetCard from '../components/bets/BetCard';
    import { Bet } from '../types/game.types';
    import { useDarkMode } from '../contexts/DarkModeContext';
    import api from '../services/api';

    export default function BetHistory() {
      const { isDarkMode } = useDarkMode();
      const [bets, setBets] = useState<Bet[]>([]);
      const [loading, setLoading] = useState(true);
      const [error, setError] = useState<string | null>(null);

      // Filters
      const [statusFilter, setStatusFilter] = useState<string>('all');
      const [sportFilter, setSportFilter] = useState<string>('all');
      const [startDate, setStartDate] = useState<string>('');
      const [endDate, setEndDate] = useState<string>('');

      // Pagination
      const [page, setPage] = useState(1);
      const [hasMore, setHasMore] = useState(true);
      const [total, setTotal] = useState(0);

      const limit = 20;

      // Fetch bets
      const fetchBets = async (pageNum: number, append: boolean = false) => {
        setLoading(true);
        setError(null);

        try {
          const params: any = {
            page: pageNum,
            limit
          };

          if (statusFilter !== 'all') {
            params.status = statusFilter;
          }
          if (sportFilter !== 'all') {
            params.sport = sportFilter;
          }
          if (startDate) {
            params.startDate = new Date(startDate).toISOString();
          }
          if (endDate) {
            params.endDate = new Date(endDate).toISOString();
          }

          const response = await api.get('/bets', { params });

          const newBets = response.data.bets || [];
          const newTotal = response.data.total || 0;

          if (append) {
            setBets((prev) => [...prev, ...newBets]);
          } else {
            setBets(newBets);
          }

          setTotal(newTotal);
          setHasMore(newBets.length === limit);
          setError(null);
        } catch (err: any) {
          console.error('Error fetching bets:', err);
          setError(err.response?.data?.message || 'Failed to load bets');
        } finally {
          setLoading(false);
        }
      };

      // Fetch on mount and filter changes
      useEffect(() => {
        setPage(1);
        fetchBets(1, false);
      }, [statusFilter, sportFilter, startDate, endDate]);

      // Load more
      const handleLoadMore = () => {
        const nextPage = page + 1;
        setPage(nextPage);
        fetchBets(nextPage, true);
      };

      // Reset filters
      const handleResetFilters = () => {
        setStatusFilter('all');
        setSportFilter('all');
        setStartDate('');
        setEndDate('');
      };

      const hasActiveFilters = statusFilter !== 'all' || sportFilter !== 'all' || startDate || endDate;

      return (
        <div className="min-h-screen bg-gray-50 dark:bg-gray-900 py-8 px-4 sm:px-6 lg:px-8 transition-colors relative"
             style={{ imageRendering: 'pixelated' }}>
          {/* 8-bit Pixel Grid Background */}
          <div
            className="absolute inset-0 opacity-5 pointer-events-none"
            style={{
              backgroundImage: `repeating-linear-gradient(
                0deg,
                ${isDarkMode ? '#dc2626' : '#b91c1c'} 0px,
                transparent 2px,
                transparent 4px,
                ${isDarkMode ? '#dc2626' : '#b91c1c'} 4px
              ),
              repeating-linear-gradient(
                90deg,
                ${isDarkMode ? '#dc2626' : '#b91c1c'} 0px,
                transparent 2px,
                transparent 4px,
                ${isDarkMode ? '#dc2626' : '#b91c1c'} 4px
              )`,
              backgroundSize: '4px 4px',
              imageRendering: 'pixelated'
            }}
          />

          <div className="max-w-6xl mx-auto relative z-10">
            {/* Header */}
            <div className="mb-8">
              <h1 className="text-3xl font-display font-bold text-gray-900 dark:text-white text-shadow-pixel tracking-wide">BET HISTORY</h1>
              <p className="mt-2 text-gray-600 dark:text-gray-400">
                View and filter your betting history
              </p>
            </div>

            {/* Filters */}
            <div className="card p-6 mb-6">
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                {/* Status Filter */}
                <div>
                  <label htmlFor="status-filter" className="block text-[10px] font-bold tracking-wider uppercase opacity-60 mb-2">
                    Status
                  </label>
                  <select
                    id="status-filter"
                    value={statusFilter}
                    onChange={(e) => setStatusFilter(e.target.value)}
                    className="w-full px-3 py-2 bg-white dark:bg-gray-800 text-gray-900 dark:text-white rounded border-2 border-gray-300 dark:border-gray-700 focus:border-brand-600 focus:outline-none text-sm"
                  >
                    <option value="all">All</option>
                    <option value="pending">Pending</option>
                    <option value="won">Won</option>
                    <option value="lost">Lost</option>
                    <option value="push">Push</option>
                    <option value="cancelled">Cancelled</option>
                  </select>
                </div>

                {/* Sport Filter */}
                <div>
                  <label htmlFor="sport-filter" className="block text-[10px] font-bold tracking-wider uppercase opacity-60 mb-2">
                    Sport
                  </label>
                  <select
                    id="sport-filter"
                    value={sportFilter}
                    onChange={(e) => setSportFilter(e.target.value)}
                    className="w-full px-3 py-2 bg-white dark:bg-gray-800 text-gray-900 dark:text-white rounded border-2 border-gray-300 dark:border-gray-700 focus:border-brand-600 focus:outline-none text-sm"
                  >
                    <option value="all">All Sports</option>
                    <option value="basketball_nba">NBA</option>
                    <option value="americanfootball_nfl">NFL</option>
                    <option value="icehockey_nhl">NHL</option>
                    <option value="baseball_mlb">MLB</option>
                    <option value="americanfootball_ncaaf">NCAAF</option>
                    <option value="basketball_ncaab">NCAAB</option>
                  </select>
                </div>

                {/* Start Date */}
                <div>
                  <label htmlFor="start-date" className="block text-[10px] font-bold tracking-wider uppercase opacity-60 mb-2">
                    Start Date
                  </label>
                  <input
                    id="start-date"
                    type="date"
                    value={startDate}
                    onChange={(e) => setStartDate(e.target.value)}
                    className="w-full px-3 py-2 bg-white dark:bg-gray-800 text-gray-900 dark:text-white rounded border-2 border-gray-300 dark:border-gray-700 focus:border-brand-600 focus:outline-none text-sm"
                  />
                </div>

                {/* End Date */}
                <div>
                  <label htmlFor="end-date" className="block text-[10px] font-bold tracking-wider uppercase opacity-60 mb-2">
                    End Date
                  </label>
                  <input
                    id="end-date"
                    type="date"
                    value={endDate}
                    onChange={(e) => setEndDate(e.target.value)}
                    className="w-full px-3 py-2 bg-white dark:bg-gray-800 text-gray-900 dark:text-white rounded border-2 border-gray-300 dark:border-gray-700 focus:border-brand-600 focus:outline-none text-sm"
                  />
                </div>
              </div>

              {/* Filter Actions */}
              {hasActiveFilters && (
                <div className="mt-4 flex items-center justify-between">
                  <p className="text-sm text-gray-600">
                    Showing {bets.length} of {total} bets
                  </p>
                  <button
                    onClick={handleResetFilters}
                    className="text-sm text-brand-600 hover:text-brand-700 font-medium"
                  >
                    Reset Filters
                  </button>
                </div>
              )}
            </div>

            {/* Loading State */}
            {loading && bets.length === 0 && (
              <div className="grid gap-4">
                {[1, 2, 3].map((i) => (
                  <div key={i} className="bg-white rounded-lg shadow-md p-6 animate-pulse">
                    <div className="h-6 bg-gray-200 rounded w-3/4 mb-4"></div>
                    <div className="h-4 bg-gray-200 rounded w-1/2 mb-2"></div>
                    <div className="h-4 bg-gray-200 rounded w-2/3"></div>
                  </div>
                ))}
              </div>
            )}

            {/* Error State */}
            {error && (
              <div className="bg-red-50 border border-red-200 rounded-lg p-4 text-center">
                <p className="text-red-800">{error}</p>
                <button
                  onClick={() => fetchBets(1, false)}
                  className="mt-3 px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
                >
                  Retry
                </button>
              </div>
            )}

            {/* Bet List */}
            {!loading && !error && bets.length > 0 && (
              <>
                <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2 mb-6" style={{ gridAutoRows: '430px' }}>
                  {bets.map((bet) => (
                    <BetCard key={bet.id} bet={bet} />
                  ))}
                </div>

                {/* Load More */}
                {hasMore && (
                  <div className="text-center">
                    <button
                      onClick={handleLoadMore}
                      disabled={loading}
                      className="btn-primary disabled:opacity-50 disabled:cursor-not-allowed"
                    >
                      {loading ? 'Loading...' : 'Load More'}
                    </button>
                  </div>
                )}
              </>
            )}

            {/* Empty State */}
            {!loading && !error && bets.length === 0 && (
              <div className="card p-12 text-center">
                <svg className="w-12 h-12 mx-auto text-gray-400 dark:text-gray-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
                </svg>
                <h3 className="font-display font-bold text-xl text-gray-900 dark:text-white text-shadow-pixel mb-2">
                  No Bets Found
                </h3>
                <p className="text-gray-600 dark:text-gray-400 mb-4">
                  {hasActiveFilters
                    ? 'Try adjusting your filters to see more results'
                    : "The ledger's empty. Add your first bet to get rolling."}
                </p>
                {hasActiveFilters && (
                  <button onClick={handleResetFilters} className="btn-primary">
                    Clear Filters
                  </button>
                )}
              </div>
            )}
          </div>
        </div>
      );
    }