source: components/Header.jsx@ aac3b2b

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

Code cleanings

  • Property mode set to 100644
File size: 4.2 KB
Line 
1import React from 'react'
2
3import Link from 'next/link'
4
5import { useEffect } from 'react'
6import { useDispatch, useSelector } from 'react-redux'
7
8import { setPlayer } from '../redux/reducers/playerSlice'
9import { setStyle } from '../redux/reducers/styleSlice'
10
11import axios from 'axios';
12
13const 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 showStats() {
57 axios.get(`/api/postgre?action=get_stats&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
58 if (res.data?.success) {
59 dispatch(setStyle({
60 ...styleState.style,
61 displayStatsScreen: true,
62 statsScreenInfo: {
63 money: {
64 ...styleState.style.statsScreenInfo.money,
65 earned: res.data.stats.money_earned,
66 },
67 blackjack: {
68 ...styleState.style.statsScreenInfo.blackjack,
69 wins: res.data.stats.blackjack_won_games,
70 },
71 roulette: {
72 ...styleState.style.statsScreenInfo.roulette,
73 wins: res.data.stats.roulette_won_games,
74 },
75 poker: {
76 ...styleState.style.statsScreenInfo.poker,
77 wins: res.data.stats.poker_won_games,
78 }
79 }
80 }))
81 }
82 })
83 }
84
85 function manageCredits() {
86 dispatch(setStyle({
87 ...styleState.style,
88 displayManageCreditsScreen: true,
89 }))
90 }
91
92 useEffect(() => {
93 if (playerState.player.displayName === '') {
94 dispatch(setStyle({
95 ...styleState.style,
96 displayLoadingScreen: true
97 }));
98
99 let success = false;
100
101 if (localStorage?.CAESSINO_SESSION_ID && localStorage.CAESSINO_SESSION_ID.length > 0) {
102 axios.get(`/api/postgre?action=check_if_logged_in&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
103 if (res.data?.success) {
104 success = true;
105
106 dispatch(setPlayer({
107 ...playerState.player,
108 displayName: res.data?.displayName,
109 sesssion_id: res.data?.session_id,
110 credits: res.data?.credits,
111 }));
112 }
113 });
114 }
115
116 if (!success) {
117 dispatch(setPlayer({
118 ...playerState.player,
119 displayName: 'Guest',
120 }))
121 dispatch(setStyle({
122 ...styleState.style,
123 displayLoadingScreen: false,
124 }))
125 }
126 }
127 }, [dispatch, playerState.player, styleState.style]);
128
129 return (
130 <header className="header">
131 <Link href="/" passHref>
132 <div className="logo">
133
134 </div>
135 </Link>
136 <nav className='mainHeaderNavigation'>
137 <ul>
138 {playerState.player.displayName === '' || playerState.player.displayName === 'Guest' ? (
139 <>
140 <li onClick={() => {register()}}>Register</li>
141 <li onClick={() => {login()}}>Login</li>
142 </>
143 ) : (
144 <>
145 <li onClick={() => {manageCredits()}}>Manage Credits</li>
146 <li onClick={() => {showStats()}}>Stats</li>
147 <li onClick={() => {logout()}}>Logout</li>
148 </>
149 )}
150 </ul>
151 </nav>
152 </header>
153 )
154}
155
156export default Header
Note: See TracBrowser for help on using the repository browser.