1 | import React from 'react'
|
---|
2 |
|
---|
3 | import Link from 'next/link'
|
---|
4 |
|
---|
5 | import { useEffect } from 'react'
|
---|
6 | import { useDispatch, useSelector } from 'react-redux'
|
---|
7 |
|
---|
8 | import { setPlayer } from '../redux/reducers/playerSlice'
|
---|
9 | import { setStyle } from '../redux/reducers/styleSlice'
|
---|
10 |
|
---|
11 | import axios from 'axios';
|
---|
12 |
|
---|
13 | const Header = () => {
|
---|
14 | const dispatch = useDispatch();
|
---|
15 |
|
---|
16 | const playerState = useSelector(state => state.player);
|
---|
17 | const styleState = useSelector(state => state.style);
|
---|
18 |
|
---|
19 | function register() {
|
---|
20 | dispatch(setStyle({
|
---|
21 | ...styleState.style,
|
---|
22 | displayRegisterScreen: true,
|
---|
23 | registerScreenInfo: {
|
---|
24 | ...styleState.style.registerScreenInfo,
|
---|
25 | setFocus: true
|
---|
26 | }
|
---|
27 | }))
|
---|
28 | }
|
---|
29 |
|
---|
30 | function login() {
|
---|
31 | dispatch(setStyle({
|
---|
32 | ...styleState.style,
|
---|
33 | displayLoginScreen: true,
|
---|
34 | loginScreenInfo: {
|
---|
35 | ...styleState.style.loginScreenInfo,
|
---|
36 | setFocus: true
|
---|
37 | }
|
---|
38 | }))
|
---|
39 | }
|
---|
40 |
|
---|
41 | function logout() {
|
---|
42 | axios.get(`/api/postgre?action=logout&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
43 | if (res.data?.success) {
|
---|
44 | localStorage.removeItem('CAESSINO_SESSION_ID');
|
---|
45 | dispatch(setPlayer({
|
---|
46 | displayName: '',
|
---|
47 | username: '',
|
---|
48 | session_id: '',
|
---|
49 | room_id: '',
|
---|
50 | credits: 0,
|
---|
51 | }))
|
---|
52 | }
|
---|
53 | })
|
---|
54 | }
|
---|
55 |
|
---|
56 | function complain() {
|
---|
57 | dispatch(setStyle({
|
---|
58 | ...styleState.style,
|
---|
59 | displayComplainScreen: true,
|
---|
60 | }))
|
---|
61 | }
|
---|
62 |
|
---|
63 | function showStats() {
|
---|
64 | axios.get(`/api/postgre?action=get_stats&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
65 | if (res.data?.success) {
|
---|
66 | dispatch(setStyle({
|
---|
67 | ...styleState.style,
|
---|
68 | displayStatsScreen: true,
|
---|
69 | statsScreenInfo: {
|
---|
70 | money: {
|
---|
71 | ...styleState.style.statsScreenInfo.money,
|
---|
72 | earned: res.data.stats.money_earned,
|
---|
73 | },
|
---|
74 | blackjack: {
|
---|
75 | ...styleState.style.statsScreenInfo.blackjack,
|
---|
76 | wins: res.data.stats.blackjack_won_games,
|
---|
77 | },
|
---|
78 | roulette: {
|
---|
79 | ...styleState.style.statsScreenInfo.roulette,
|
---|
80 | wins: res.data.stats.roulette_won_games,
|
---|
81 | },
|
---|
82 | poker: {
|
---|
83 | ...styleState.style.statsScreenInfo.poker,
|
---|
84 | wins: res.data.stats.poker_won_games,
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }))
|
---|
88 | }
|
---|
89 | })
|
---|
90 | }
|
---|
91 |
|
---|
92 | function manageCredits() {
|
---|
93 | dispatch(setStyle({
|
---|
94 | ...styleState.style,
|
---|
95 | displayManageCreditsScreen: true,
|
---|
96 | displayDepositModal: false,
|
---|
97 | displayWithdrawModal: false,
|
---|
98 | }))
|
---|
99 | }
|
---|
100 |
|
---|
101 | useEffect(() => {
|
---|
102 | if (playerState.player.displayName === '') {
|
---|
103 | dispatch(setStyle({
|
---|
104 | ...styleState.style,
|
---|
105 | displayLoadingScreen: true
|
---|
106 | }));
|
---|
107 |
|
---|
108 | let success = false;
|
---|
109 |
|
---|
110 | if (localStorage?.CAESSINO_SESSION_ID && localStorage.CAESSINO_SESSION_ID.length > 0) {
|
---|
111 | axios.get(`/api/postgre?action=check_if_logged_in&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
112 | if (res.data?.success) {
|
---|
113 | success = true;
|
---|
114 |
|
---|
115 | dispatch(setPlayer({
|
---|
116 | ...playerState.player,
|
---|
117 | displayName: res.data?.displayName,
|
---|
118 | sesssion_id: res.data?.session_id,
|
---|
119 | credits: res.data?.credits,
|
---|
120 | }));
|
---|
121 | }
|
---|
122 | });
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (!success) {
|
---|
126 | dispatch(setPlayer({
|
---|
127 | ...playerState.player,
|
---|
128 | displayName: 'Guest',
|
---|
129 | }))
|
---|
130 | dispatch(setStyle({
|
---|
131 | ...styleState.style,
|
---|
132 | displayLoadingScreen: false,
|
---|
133 | }))
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }, [dispatch, playerState.player, styleState.style]);
|
---|
137 |
|
---|
138 | return (
|
---|
139 | <header className="header">
|
---|
140 | <Link href="/" passHref>
|
---|
141 | <div className="logo">
|
---|
142 |
|
---|
143 | </div>
|
---|
144 | </Link>
|
---|
145 | <nav className='mainHeaderNavigation'>
|
---|
146 | <ul>
|
---|
147 | {playerState.player.displayName === '' || playerState.player.displayName === 'Guest' ? (
|
---|
148 | <>
|
---|
149 | <li onClick={() => {register()}}>Register</li>
|
---|
150 | <li onClick={() => {login()}}>Login</li>
|
---|
151 | </>
|
---|
152 | ) : (
|
---|
153 | <>
|
---|
154 | <li onClick={() => {manageCredits()}}>Manage Credits</li>
|
---|
155 | <li onClick={() => {showStats()}}>Statistics</li>
|
---|
156 | <li onClick={() => {complain()}}>Complain</li>
|
---|
157 | <li onClick={() => {logout()}}>Logout</li>
|
---|
158 | </>
|
---|
159 | )}
|
---|
160 | </ul>
|
---|
161 | </nav>
|
---|
162 | </header>
|
---|
163 | )
|
---|
164 | }
|
---|
165 |
|
---|
166 | export default Header |
---|