1
1
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
2
- const userData = JSON . parse ( localStorage . getItem ( 'userData' ) ) ;
3
- if ( ! userData || ! userData . id || ! userData . token ) {
4
- console . error ( 'UserData is missing or incomplete' ) ;
5
- return ;
6
- }
2
+ const matchHistoryModal = document . getElementById ( 'HistoryModal' ) ;
3
+ matchHistoryModal . addEventListener ( 'show.bs.modal' , updateMatchHistory ) ;
4
+ } ) ;
7
5
8
- fetch ( `/user/${ userData . id } /game-history` , {
9
- method : 'GET' ,
10
- headers : {
11
- 'Authorization' : `Bearer ${ userData . token } `
12
- }
13
- } )
14
- . then ( response => {
15
- if ( ! response . ok ) {
16
- throw new Error ( 'Network response was not ok' ) ;
17
- }
18
- return response . json ( ) ;
19
- } )
20
- . then ( data => {
21
- const gameHistoryContainer = document . getElementById ( 'gameHistory' ) ;
22
- if ( ! gameHistoryContainer ) {
23
- console . error ( 'GameHistory container not found' ) ;
24
- return ;
25
- }
26
- let htmlContent = '<div class="container mt-4"><h2>Game History</h2>' ;
27
- data . forEach ( game => {
28
- htmlContent += `
29
- <div class="game-record mb-3">
30
- <h3>${ game . name } </h3>
31
- <p>Outcome: ${ game . outcome } </p>
32
- <p>Date Played: ${ new Date ( game . datePlayed ) . toLocaleDateString ( ) } </p>
33
- </div>
34
- ` ;
35
- } ) ;
36
- htmlContent += '</div>' ;
37
- gameHistoryContainer . innerHTML = htmlContent ;
38
- } )
39
- . catch ( error => {
40
- console . error ( 'Error fetching game history:' , error ) ;
41
- } ) ;
42
- } ) ;
6
+ export function updateMatchHistory ( ) {
7
+ const userData = JSON . parse ( localStorage . getItem ( 'userData' ) ) ;
8
+ if ( ! userData || ! userData . id || ! userData . token ) {
9
+ console . error ( 'UserData is missing or incomplete' ) ;
10
+ return ;
11
+ }
12
+ fetch ( `/game-history/` , {
13
+ method : 'GET' ,
14
+ headers : {
15
+ }
16
+ } )
17
+ . then ( response => {
18
+ if ( ! response . ok ) {
19
+ return response . json ( ) . then ( error => {
20
+ throw new Error ( error . detail || 'Network response was not ok' ) ;
21
+ } ) ;
22
+ }
23
+ return response . json ( ) ;
24
+ } )
25
+ . then ( data => {
26
+ const gameHistoryContainer = document . getElementById ( 'History' ) ;
27
+ if ( ! gameHistoryContainer ) {
28
+ console . error ( 'GameHistory container not found' ) ;
29
+ return ;
30
+ }
31
+ let htmlContent = '<div class="container mt-4"><h2>Game History</h2>' ;
32
+ data . forEach ( game => {
33
+ if ( game . player1_id === userData . id || game . player2_id === userData . id ) {
34
+ htmlContent += `
35
+ <div class="game-record mb-3">
36
+ <h3>Game: ${ game . game_id } </h3>
37
+ <p>${ new Date ( game . start_time ) . toLocaleString ( 'en-US' , { year : 'numeric' , month : 'long' , day : 'numeric' , hour : '2-digit' , minute : '2-digit' } ) } </p>
38
+ <p>${ game . player1_username } - ${ game . player2_username } </p>
39
+ <p>Winner: ${ game . winner_name } </p>
40
+ </div>
41
+ ` ;
42
+ }
43
+ } ) ;
44
+ htmlContent += '</div>' ;
45
+ gameHistoryContainer . innerHTML = htmlContent ;
46
+ } )
47
+ . catch ( error => {
48
+ console . error ( 'Error fetching game history:' , error . message ) ;
49
+ const gameHistoryContainer = document . getElementById ( 'History' ) ;
50
+ if ( gameHistoryContainer ) {
51
+ gameHistoryContainer . innerHTML = `<p>Error fetching game history: ${ error . message } </p>` ;
52
+ }
53
+ } ) ;
54
+ } ;
0 commit comments