source: components/LoginScreen.jsx@ ace7865

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

Finished Roulette

  • Property mode set to 100644
File size: 5.2 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
88 dispatch(setPlayer({
89 ...playerState.player,
90 username: res.data?.session?.username,
91 displayName: res.data?.session?.displayName,
92 credits: res.data?.session.credits,
93 session_id: res.data?.session?.id,
94 }));
95
96 dispatch(setStyle({
97 ...styleState.style,
98 displayLoadingScreen: false,
99 displayLoginScreen: false,
100 loginScreenInfo: {
101 username: '',
102 password: '',
103 },
104 notification: {
105 show: true,
106 text: 'Successfully logged in.',
107 status: 'success',
108 },
109 inlineAlertText: '',
110 }));
111 }
112 else {
113 dispatch(setStyle({
114 ...styleState.style,
115 displayLoginScreen: true,
116 inlineAlertText: res.data?.message,
117 }));
118 }
119 })
120 }
121
122 function checkForLink(e) {
123 if (e.target.innerHTML.includes('Register')) {
124 dispatch(setStyle({
125 ...styleState.style,
126 displayRegisterScreen: true,
127 displayLoginScreen: false,
128 loginScreenInfo: {
129 username: '',
130 password: '',
131 },
132 inlineAlertText: '',
133 }));
134 }
135 }
136
137 return (
138 <div className="fullscreen fs-centered loginScreen" style={{display: styleState.style.displayLoginScreen ? 'block' : 'none'}}>
139 <div className="fs-inputs-container">
140 {styleState.style.inlineAlertText.length > 0 && <span className="inlineAlert" onClick={(e) => checkForLink(e)}>{styleState.style.inlineAlertText}</span>}
141 <div>
142 <span>Username:</span>
143 <input ref={ref} type="text" onChange={(e) => {onChangeUsername(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.username} placeholder="your username..."/>
144 <span>Password:</span>
145 <input type="password" onChange={(e) => {onChangePassword(e)}} onKeyUp={(e) => keyUp(e)} value={styleState.style.loginScreenInfo.password} placeholder="your password..."/>
146 <div style={{width: '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between'}}>
147 <button className="primaryButton" onClick={() => closeForm()}>Close Form</button>
148 <button className="secondaryButton" onClick={() => login()}>Log In</button>
149 </div>
150 </div>
151 </div>
152 </div>
153 )
154}
155
156export default LoginScreen
Note: See TracBrowser for help on using the repository browser.