source: components/LoginScreen.jsx@ 9bd09b0

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

Roulette place a bet functionality

  • Property mode set to 100644
File size: 5.4 KB
Line 
1import React from 'react'
2
3import { useSelector, useDispatch } from 'react-redux'
4
5import { useRef } from 'react'
6
7import { setPlayer } from '../redux/reducers/playerSlice';
8import { setStyle } from '../redux/reducers/styleSlice';
9
10import axios from 'axios';
11
12const LoginScreen = () => {
13 const ref = useRef(null);
14
15 const dispatch = useDispatch();
16
17 const playerState = useSelector(state => state.player);
18 const styleState = useSelector(state => state.style);
19
20 setTimeout(() => {
21 if (styleState.style.displayLoginScreen && styleState.style.loginScreenInfo.setFocus) {
22 ref.current.focus();
23 dispatch(setStyle({
24 ...styleState.style,
25 loginScreenInfo: {
26 ...styleState.style.loginScreenInfo,
27 setFocus: false
28 }
29 }))
30 }
31 }, 10);
32
33 function onChangeUsername(e) {
34 dispatch(setStyle({
35 ...styleState.style,
36 loginScreenInfo: {
37 ...styleState.style.loginScreenInfo,
38 username: e.target.value,
39 }
40 }))
41 }
42
43 function onChangePassword(e) {
44 dispatch(setStyle({
45 ...styleState.style,
46 loginScreenInfo: {
47 ...styleState.style.loginScreenInfo,
48 password: e.target.value,
49 }
50 }))
51 }
52
53 function keyUp(e) {
54 if (e.key === 'Enter') {
55 login();
56 }
57 }
58
59 function closeForm() {
60 dispatch(setStyle({
61 ...styleState.style,
62 displayLoginScreen: false,
63 loginScreenInfo: {
64 username: '',
65 password: '',
66 },
67 inlineAlertText: '',
68 }));
69
70 }
71 function login() {
72 dispatch(setStyle({
73 ...styleState.style,
74 displayLoadingScreen: true,
75 }))
76
77 axios.post(`/api/postgre`, {
78 action: 'login',
79 username: 'drama',
80 password: 'drama'
81 // username: styleState.style.loginScreenInfo.username,
82 // password: styleState.style.loginScreenInfo.password,
83 })
84 .then(res => {
85 if (res.data?.success) {
86 localStorage.CAESSINO_SESSION_ID = res.data?.session?.id;
87 dispatch(setStyle({
88 ...styleState.style,
89 displayLoginScreen: false,
90 }));
91
92 dispatch(setPlayer({
93 ...playerState.player,
94 username: res.data?.session?.username,
95 displayName: res.data?.session?.displayName,
96 credits: res.data?.session.credits,
97 session_id: res.data?.session?.id,
98 }));
99
100 dispatch(setStyle({
101 ...styleState.style,
102 displayLoadingScreen: false,
103 displayLoginScreen: false,
104 loginScreenInfo: {
105 username: '',
106 password: '',
107 },
108 notification: {
109 show: true,
110 text: 'Successfully logged in.',
111 status: 'success',
112 },
113 }));
114 }
115 else {
116 dispatch(setStyle({
117 ...styleState.style,
118 displayLoginScreen: true,
119 inlineAlertText: res.data?.message,
120 }));
121 }
122 })
123 }
124
125 function checkForLink(e) {
126 if (e.target.innerHTML.includes('Register')) {
127 dispatch(setStyle({
128 ...styleState.style,
129 displayRegisterScreen: true,
130 displayLoginScreen: false,
131 loginScreenInfo: {
132 username: '',
133 password: '',
134 },
135 inlineAlertText: '',
136 }));
137 }
138 }
139
140 return (
141 <div className="fullscreen fs-centered loginScreen" style={{display: styleState.style.displayLoginScreen ? 'block' : 'none'}}>
142 <div className="fs-inputs-container">
143 {styleState.style.inlineAlertText.length > 0 && <span className="inlineAlert" onClick={(e) => checkForLink(e)}>{styleState.style.inlineAlertText}</span>}
144 <div>
145 <span>Username:</span>
146 <input ref={ref} type="text" onChange={(e) => {onChangeUsername(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.username} placeholder="your username..."/>
147 <span>Password:</span>
148 <input type="password" onChange={(e) => {onChangePassword(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.password} placeholder="your password..."/>
149 <div style={{width: '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between'}}>
150 <button className="primaryButton" onClick={() => closeForm()}>Close Form</button>
151 <button className="secondaryButton" onClick={() => login()}>Log In</button>
152 </div>
153 </div>
154 </div>
155 </div>
156 )
157}
158
159export default LoginScreen
Note: See TracBrowser for help on using the repository browser.