1 | import React from 'react'
|
---|
2 |
|
---|
3 | import { useSelector, useDispatch } from 'react-redux'
|
---|
4 |
|
---|
5 | import { setStyle } from '../redux/reducers/styleSlice';
|
---|
6 |
|
---|
7 | import axios from 'axios';
|
---|
8 |
|
---|
9 | const RegisterScreen = () => {
|
---|
10 | const dispatch = useDispatch();
|
---|
11 |
|
---|
12 | const styleState = useSelector(state => state.style);
|
---|
13 |
|
---|
14 | function onChangeUsername(e) {
|
---|
15 | dispatch(setStyle({
|
---|
16 | ...styleState.style,
|
---|
17 | registerScreenInfo: {
|
---|
18 | ...styleState.style.registerScreenInfo,
|
---|
19 | username: e.target.value,
|
---|
20 | }
|
---|
21 | }))
|
---|
22 | }
|
---|
23 |
|
---|
24 | function onChangeDisplayName(e) {
|
---|
25 | dispatch(setStyle({
|
---|
26 | ...styleState.style,
|
---|
27 | registerScreenInfo: {
|
---|
28 | ...styleState.style.registerScreenInfo,
|
---|
29 | displayName: e.target.value,
|
---|
30 | }
|
---|
31 | }))
|
---|
32 | }
|
---|
33 |
|
---|
34 | function onChangePassword(e) {
|
---|
35 | dispatch(setStyle({
|
---|
36 | ...styleState.style,
|
---|
37 | registerScreenInfo: {
|
---|
38 | ...styleState.style.registerScreenInfo,
|
---|
39 | password: e.target.value,
|
---|
40 | }
|
---|
41 | }))
|
---|
42 | }
|
---|
43 |
|
---|
44 | function closeForm() {
|
---|
45 | dispatch(setStyle({
|
---|
46 | ...styleState.style,
|
---|
47 | displayRegisterScreen: false,
|
---|
48 | registerScreenInfo: {
|
---|
49 | username: '',
|
---|
50 | displayName: '',
|
---|
51 | password: '',
|
---|
52 | },
|
---|
53 | inlineAlertText: '',
|
---|
54 | }));
|
---|
55 | }
|
---|
56 |
|
---|
57 | function register() {
|
---|
58 | dispatch(setStyle({
|
---|
59 | ...styleState.style,
|
---|
60 | displayLoadingScreen: true,
|
---|
61 | }))
|
---|
62 |
|
---|
63 | axios.post('/api/postgre', {
|
---|
64 | action: 'register',
|
---|
65 | username: styleState.style.registerScreenInfo.username,
|
---|
66 | displayName: styleState.style.registerScreenInfo.displayName,
|
---|
67 | password: styleState.style.registerScreenInfo.password,
|
---|
68 | })
|
---|
69 | .then(res => {
|
---|
70 | if (res.data?.success) {
|
---|
71 | dispatch(setStyle({
|
---|
72 | ...styleState.style,
|
---|
73 | displayRegisterScreen: false,
|
---|
74 | }));
|
---|
75 |
|
---|
76 | dispatch(setStyle({
|
---|
77 | ...styleState.style,
|
---|
78 | displayLoadingScreen: false,
|
---|
79 | displayRegisterScreen: false,
|
---|
80 | displayLoginScreen: true,
|
---|
81 | registerScreenInfo: {
|
---|
82 | username: '',
|
---|
83 | displayName: '',
|
---|
84 | password: '',
|
---|
85 | },
|
---|
86 | notification: {
|
---|
87 | show: true,
|
---|
88 | text: 'Successfully registered! Please Log In now.',
|
---|
89 | status: 'success',
|
---|
90 | },
|
---|
91 | }));
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | dispatch(setStyle({
|
---|
95 | ...styleState.style,
|
---|
96 | displayRegisterScreen: true,
|
---|
97 | inlineAlertText: res.data?.message,
|
---|
98 | }));
|
---|
99 | }
|
---|
100 | })
|
---|
101 | }
|
---|
102 |
|
---|
103 | return (
|
---|
104 | <div className="fullscreen fs-centered registerScreen" style={{display: styleState.style.displayRegisterScreen ? 'block' : 'none'}}>
|
---|
105 | <div className="fs-inputs-container">
|
---|
106 | {styleState.style.inlineAlertText.length > 0 && <span className="inlineAlert">{styleState.style.inlineAlertText}</span>}
|
---|
107 | <div>
|
---|
108 | <span>Username:</span>
|
---|
109 | <input type="text" onChange={(e) => {onChangeUsername(e)}} value={styleState.style.registerScreenInfo.username}/>
|
---|
110 | <span>Display Name:</span>
|
---|
111 | <input type="text" onChange={(e) => {onChangeDisplayName(e)}} value={styleState.style.registerScreenInfo.displayName}/>
|
---|
112 | <span>Password:</span>
|
---|
113 | <input type="password" onChange={(e) => {onChangePassword(e)}} value={styleState.style.registerScreenInfo.password}/>
|
---|
114 | <div style={{width: '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between'}}>
|
---|
115 | <button className="primaryButton" onClick={() => closeForm()}>Close Form</button>
|
---|
116 | <button className="secondaryButton" onClick={() => register()}>Register</button>
|
---|
117 | </div>
|
---|
118 | </div>
|
---|
119 | </div>
|
---|
120 | </div>
|
---|
121 | )
|
---|
122 | }
|
---|
123 |
|
---|
124 | export default RegisterScreen |
---|