Skip to content

Commit e577819

Browse files
committed
merged server logic to login
1 parent dbd32bd commit e577819

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/actionTypes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ export const UPDATE_HTML_ATTR: string = 'UPDATE_HTML_ATTR';
4343

4444
export const SET_USERNAME: string = 'SET_USERNAME';
4545
export const SET_PASSWORD: string = 'SET_PASSWORD';
46+
export const LOGIN: string = 'LOGIN';
47+
export const SIGNUP: string = 'SIGNUP';

src/actions/actionCreators.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ import {
4040
UPDATE_HTML_ATTR,
4141
UPDATE_CODE,
4242
SET_USERNAME,
43-
SET_PASSWORD
43+
SET_PASSWORD,
44+
LOGIN,
45+
SIGNUP
4446
} from '../actionTypes/index';
4547
import { loadState } from '../localStorage';
4648
import createFiles from '../utils/createFiles.util';
@@ -339,4 +341,20 @@ export const setUsername = (username: string): Action => ({
339341
export const setPassword = (password: string): Action => ({
340342
type: SET_PASSWORD,
341343
payload: password
342-
})
344+
})
345+
346+
// export const login = (username: string, password: string) => ({
347+
// return (dispatch: any) => {
348+
// fetch('/login', {
349+
// method: 'POST',
350+
// body: JSON.stringify({ username, password })
351+
// })
352+
// .then(response => response.json())
353+
// .then(data => console.log(data))
354+
// }
355+
// })
356+
357+
// export const signup = (username: string, password: string): Action => ({
358+
// type: SIGNUP,
359+
// payload: { username, password }
360+
// })

src/components/login/SignIn.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { Component, useState } from 'react';
22
import { connect } from 'react';
33
import { LoginInt } from '../../interfaces/Interfaces';
44
import { setUsername, setPassword } from '../../actions/actionCreators';
@@ -26,7 +26,9 @@ const mapStateToProps = (store: any) => ({
2626

2727
const mapDispatchToProps = (dispatch: any) => ({
2828
setUsername: (username: string) => dispatch(setUsername(username)),
29-
setPassword: (password: string) => dispatch(setPassword(password))
29+
setPassword: (password: string) => dispatch(setPassword(password)),
30+
// login: (username: string, password: string) => dispatch(login(username, password)),
31+
// signup: (username: string, password: string) => dispatch(signup(username, password)),
3032
})
3133

3234
interface LoginProps extends LoginInt {
@@ -69,7 +71,34 @@ const useStyles = makeStyles((theme) => ({
6971

7072
const SignIn: React.FC<LoginProps> = (props) => {
7173
const classes = useStyles();
72-
const count = useSelector(state => state);
74+
//const count = useSelector(state => state);
75+
76+
const [username, setUsername] = useState('');
77+
const [password, setPassword] = useState('');
78+
79+
const handleChange = (e) => {
80+
let inputVal = e.target.value;
81+
switch (e.target.name) {
82+
case 'username':
83+
setUsername(inputVal);
84+
break;
85+
case 'password':
86+
setPassword(inputVal);
87+
break;
88+
}
89+
};
90+
91+
const handleLogin = (e) => {
92+
console.log('click fired on handleLogin')
93+
e.preventDefault();
94+
fetch('/login', {
95+
method: 'POST',
96+
body: JSON.stringify({ username, password })
97+
})
98+
.then(res => res.json())
99+
.then(data => console.log(data))
100+
.catch(err => console.log(err))
101+
}
73102

74103
return (
75104
<Container component="main" maxWidth="xs">
@@ -92,6 +121,8 @@ const SignIn: React.FC<LoginProps> = (props) => {
92121
name="username"
93122
autoComplete="username"
94123
autoFocus
124+
value={password}
125+
onChange={handleChange}
95126
/>
96127
<TextField
97128
variant="outlined"
@@ -103,6 +134,8 @@ const SignIn: React.FC<LoginProps> = (props) => {
103134
type="password"
104135
id="password"
105136
autoComplete="current-password"
137+
value={password}
138+
onChange={handleChange}
106139
/>
107140
<FormControlLabel
108141
control={<Checkbox value="remember" color="primary" />}
@@ -114,6 +147,7 @@ const SignIn: React.FC<LoginProps> = (props) => {
114147
variant="contained"
115148
color="primary"
116149
className={classes.submit}
150+
onClick={handleLogin}
117151
>
118152
Sign In
119153
</Button>

0 commit comments

Comments
 (0)