source: components/poker/PokerHeader.jsx@ 433e0c5

main
Last change on this file since 433e0c5 was 433e0c5, checked in by anastasovv <simon@…>, 2 years ago

Added complaints, managing credits, and lost connection screens

  • Property mode set to 100644
File size: 4.3 KB
Line 
1import React from 'react'
2
3import Link from 'next/link'
4
5import { useRouter } from 'next/router'
6
7import { AiOutlineArrowLeft } from 'react-icons/ai'
8
9import { useEffect } from 'react'
10import { useDispatch, useSelector } from 'react-redux'
11
12import { setPlayer, setPokerGame } from '../../redux/reducers/playerSlice'
13import { setStyle } from '../../redux/reducers/styleSlice'
14
15import axios from 'axios';
16
17const PokerHeader = () => {
18 const dispatch = useDispatch();
19
20 const router = useRouter();
21
22 const playerState = useSelector(state => state.player);
23 const styleState = useSelector(state => state.style);
24
25 useEffect(() => {
26 // display loading screen
27 dispatch(setStyle({
28 ...styleState.style,
29 displayLoadingScreen: true,
30 }));
31
32 let interval = null;
33
34 axios.get(`/api/poker?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
35 if (res.data?.success) {
36 if (interval !== null) clearInterval(interval);
37
38 interval = setInterval(() => {
39 axios.get(`/api/poker?action=update_state&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(newRes => {
40 if (newRes.data?.success) {
41 dispatch(setPokerGame(newRes.data?.pokerGame))
42
43 if (newRes.data?.pokerGame?.player?.credits !== playerState.player.credits && newRes.data?.pokerGame?.player?.credits > 0) {
44 dispatch(setPlayer({
45 ...playerState.player,
46 displayName: res.data?.displayName,
47 session_id: res.data?.session_id,
48 credits: newRes.data?.pokerGame?.player?.credits,
49 }))
50 }
51 }
52
53 dispatch(setStyle({
54 ...styleState.style,
55 displayLoadingScreen: false,
56 notification: {
57 ...styleState.style.notification,
58 show: false,
59 },
60 lostConnectionInfo: {
61 show: false,
62 message: ''
63 }
64 }))
65 }).catch(error => {
66 dispatch(setStyle({
67 ...styleState.style,
68 displayLoadingScreen: false,
69 lostConnectionInfo: {
70 show: true,
71 message: 'Game will be played until the end upon server gets live. You cannot continue your game, but the money earned / lost will be updated.'
72 }
73 }))
74 });
75 }, 1000);
76 }
77 else {
78 dispatch(setStyle({
79 ...styleState.style,
80 notification: {
81 show: true,
82 text: 'Please login in order to play poker.',
83 status: 'error',
84 },
85 displayLoadingScreen: false,
86 }))
87
88 router.push('/');
89 }
90 });
91 }, [])
92
93 function leaveTable() {
94 axios.get(`/api/poker?action=leave_table&session_id=${localStorage.CAESSINO_SESSION_ID}`);
95 }
96
97 return (
98 <header className="header">
99 <div style={{display: 'flex', alignItems: 'center'}}>
100 <Link href="/" passHref>
101 <h2>
102 <AiOutlineArrowLeft />
103 </h2>
104 </Link>
105 { playerState.pokerGame?.player?.table?.length > 0 && <button style={{marginBottom: '4px', marginLeft: '32px', fontSize: '16px'}} className="tertiaryButton" onClick={() => leaveTable()}>Leave Table</button> }
106 </div>
107 <nav>
108 <ul>
109 <li>Hi, {playerState?.player?.displayName}</li>
110 <li>Balance: ${playerState?.player?.credits}</li>
111 </ul>
112 </nav>
113 </header>
114 )
115}
116
117export default PokerHeader
Note: See TracBrowser for help on using the repository browser.