Skip to content

Commit 9fffb46

Browse files
author
Matt
committed
initial actions/reducers
1 parent 877c975 commit 9fffb46

File tree

6 files changed

+121
-47
lines changed

6 files changed

+121
-47
lines changed

client/src/App.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
3-
import LoginForm from 'components/LoginForm'
3+
import LoginContainer from 'components/Login/LoginContainer'
44
import ProtectedContainer from 'components/Protected/ProtectedContainer'
55
import HomeContainer from 'components/Home/HomeContainer'
66
import 'App.css'
@@ -23,7 +23,7 @@ export const App = () => (
2323
<hr />
2424

2525
<Route exact path="/" component={HomeContainer} />
26-
<Route path="/login" component={LoginForm} />
26+
<Route path="/login" component={LoginContainer} />
2727
<Route path="/protected" component={ProtectedContainer} />
2828
</div>
2929
</Router>

client/src/actions/appActions.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { SET_AUTH, CHANGE_FORM, SENDING_REQUEST, SET_ERROR_MESSAGE } from '../constants/AppConstants'
2+
3+
export function login(username, password) {
4+
return dispatch => {
5+
dispatch(sendingRequest(true))
6+
fetch('/api/login', {
7+
headers: {
8+
'Content-Type': 'application/json'
9+
},
10+
method: 'POST',
11+
body: JSON.stringify({ username, password })
12+
})
13+
.then(res => {
14+
if (res.ok) return res.json()
15+
else throw new Error(res.statusText)
16+
})
17+
.then(data => {
18+
dispatch(sendingRequest(false))
19+
dispatch(setAuthState(true))
20+
})
21+
.catch(error => {
22+
dispatch(sendingRequest(false))
23+
dispatch(setErrorMessage('Login failed'))
24+
})
25+
}
26+
}
27+
28+
export function setAuthState(newState) {
29+
return { type: SET_AUTH, newState }
30+
}
31+
32+
export function sendingRequest(sending) {
33+
return { type: SENDING_REQUEST, sending }
34+
}
35+
36+
function setErrorMessage(message) {
37+
return { type: SET_ERROR_MESSAGE, message }
38+
}
39+
40+
export function changeForm(newState) {
41+
return { type: CHANGE_FORM, newState }
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import { connect } from 'react-redux'
4+
import { withRouter, Redirect } from 'react-router-dom'
5+
6+
import LoginForm from 'components/Login/LoginForm'
7+
import { login, changeForm } from 'actions/appActions'
8+
9+
class LoginContainer extends React.Component {
10+
render() {
11+
const { loggedIn, handleSubmit, currentlySending, formState, handleChange } = this.props
12+
13+
return (
14+
<div>
15+
{loggedIn ? (
16+
<Redirect to="/home" />
17+
) : (
18+
<LoginForm
19+
handleSubmit={handleSubmit}
20+
currentlySending={currentlySending}
21+
formState={formState}
22+
handleChange={handleChange}
23+
/>
24+
)}
25+
</div>
26+
)
27+
}
28+
}
29+
30+
const mapStateToProps = state => ({
31+
loggedIn: state.loggedIn,
32+
currentlySending: state.currentlySending,
33+
formState: state.formState
34+
})
35+
36+
const mapDispatchToProps = dispatch => ({
37+
handleSubmit: (username, password) => dispatch(login(username, password)),
38+
handleChange: event => {
39+
const target = event.target
40+
const value = target.value
41+
const name = target.name
42+
43+
dispatch(changeForm({ [name]: value }))
44+
}
45+
})
46+
47+
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(LoginContainer))
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
const LoginForm = props => {
4+
const { formState, handleSubmit, handleChange, currentlySending } = props
5+
6+
const submitForm = event => {
7+
event.preventDefault()
8+
handleSubmit(formState.email, formState.password)
9+
}
10+
11+
return (
12+
<form onSubmit={submitForm}>
13+
<label>
14+
Email:
15+
<input type="text" name="email" value={formState.email} onChange={handleChange} />
16+
</label>
17+
<label>
18+
Name:
19+
<input type="password" name="password" value={formState.password} onChange={handleChange} />
20+
</label>
21+
<input type="submit" value="Submit" />
22+
</form>
23+
)
24+
}
25+
26+
export default LoginForm

client/src/components/LoginForm.js

-44
This file was deleted.

client/src/reducers/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export function homeReducer(state = initialState, action) {
1515
case CHANGE_FORM:
1616
return {
1717
...state,
18-
formState: action.newState
18+
formState: {
19+
...state.formState,
20+
...action.newState
21+
}
1922
}
2023
case SET_AUTH:
2124
return {

0 commit comments

Comments
 (0)