source: components/poker/PokerHeader.jsx@ faff334

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

Now you need to activate your account via email & also added mail sending after server crash

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