source: components/LoginScreen.jsx@ 64dc53b

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

Code cleanings

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