source: components/Header.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.5 KB
RevLine 
[87614a5]1import React from 'react'
2
3import Link from 'next/link'
4
[64dc53b]5import { useEffect } from 'react'
[87614a5]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,
[64dc53b]23 registerScreenInfo: {
24 ...styleState.style.registerScreenInfo,
25 setFocus: true
26 }
[87614a5]27 }))
28 }
29
30 function login() {
31 dispatch(setStyle({
32 ...styleState.style,
33 displayLoginScreen: true,
[64dc53b]34 loginScreenInfo: {
35 ...styleState.style.loginScreenInfo,
36 setFocus: true
37 }
[87614a5]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
[433e0c5]56 function complain() {
57 dispatch(setStyle({
58 ...styleState.style,
59 displayComplainScreen: true,
60 }))
61 }
62
[87614a5]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,
[433e0c5]96 displayDepositModal: false,
97 displayWithdrawModal: false,
[87614a5]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,
[e007fcd]118 username: res.data?.username,
119 session_id: res.data?.session_id,
[87614a5]120 credits: res.data?.credits,
121 }));
122 }
123 });
124 }
125
126 if (!success) {
127 dispatch(setPlayer({
128 ...playerState.player,
129 displayName: 'Guest',
130 }))
131 dispatch(setStyle({
132 ...styleState.style,
133 displayLoadingScreen: false,
134 }))
135 }
136 }
137 }, [dispatch, playerState.player, styleState.style]);
138
139 return (
140 <header className="header">
141 <Link href="/" passHref>
142 <div className="logo">
143
144 </div>
145 </Link>
[64dc53b]146 <nav className='mainHeaderNavigation'>
[87614a5]147 <ul>
148 {playerState.player.displayName === '' || playerState.player.displayName === 'Guest' ? (
149 <>
150 <li onClick={() => {register()}}>Register</li>
151 <li onClick={() => {login()}}>Login</li>
152 </>
153 ) : (
154 <>
155 <li onClick={() => {manageCredits()}}>Manage Credits</li>
[433e0c5]156 <li onClick={() => {showStats()}}>Statistics</li>
157 <li onClick={() => {complain()}}>Complain</li>
[87614a5]158 <li onClick={() => {logout()}}>Logout</li>
159 </>
160 )}
161 </ul>
162 </nav>
163 </header>
164 )
165}
166
167export default Header
Note: See TracBrowser for help on using the repository browser.