forked from Remchi/bookworm-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import api from "../api"; | ||
import { userLoggedIn } from "./auth"; | ||
|
||
export const signup = data => dispatch => | ||
api.user.signup(data).then(user => dispatch(userLoggedIn(user))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { Form, Button } from "semantic-ui-react"; | ||
import isEmail from "validator/lib/isEmail"; | ||
import InlineError from "../messages/InlineError"; | ||
|
||
class SignupForm extends React.Component { | ||
state = { | ||
data: { | ||
email: "", | ||
password: "" | ||
}, | ||
loading: false, | ||
errors: {} | ||
}; | ||
|
||
onChange = e => | ||
this.setState({ | ||
...this.state, | ||
data: { ...this.state.data, [e.target.name]: e.target.value } | ||
}); | ||
|
||
onSubmit = e => { | ||
e.preventDefault(); | ||
const errors = this.validate(this.state.data); | ||
this.setState({ errors }); | ||
if (Object.keys(errors).length === 0) { | ||
this.setState({ loading: true }); | ||
this.props | ||
.submit(this.state.data) | ||
.catch(err => | ||
this.setState({ errors: err.response.data.errors, loading: false }) | ||
); | ||
} | ||
}; | ||
|
||
validate = data => { | ||
const errors = {}; | ||
|
||
if (!isEmail(data.email)) errors.email = "Invalid email"; | ||
if (!data.password) errors.password = "Can't be blank"; | ||
|
||
return errors; | ||
}; | ||
|
||
render() { | ||
const { data, errors, loading } = this.state; | ||
|
||
return ( | ||
<Form onSubmit={this.onSubmit} loading={loading}> | ||
<Form.Field error={!!errors.email}> | ||
<label htmlFor="email">Email</label> | ||
<input | ||
type="email" | ||
id="email" | ||
name="email" | ||
placeholder="[email protected]" | ||
value={data.email} | ||
onChange={this.onChange} | ||
/> | ||
{errors.email && <InlineError text={errors.email} />} | ||
</Form.Field> | ||
|
||
<Form.Field error={!!errors.password}> | ||
<label htmlFor="password">Password</label> | ||
<input | ||
type="password" | ||
id="password" | ||
name="password" | ||
value={data.password} | ||
onChange={this.onChange} | ||
/> | ||
{errors.password && <InlineError text={errors.password} />} | ||
</Form.Field> | ||
|
||
<Button primary>Sign Up</Button> | ||
</Form> | ||
); | ||
} | ||
} | ||
|
||
SignupForm.propTypes = { | ||
submit: PropTypes.func.isRequired | ||
}; | ||
|
||
export default SignupForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { connect } from "react-redux"; | ||
import SignupForm from "../forms/SignupForm"; | ||
import { signup } from "../../actions/users"; | ||
|
||
class SignupPage extends React.Component { | ||
submit = data => | ||
this.props.signup(data).then(() => this.props.history.push("/dashboard")); | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<SignupForm submit={this.submit} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
SignupPage.propTypes = { | ||
history: PropTypes.shape({ | ||
push: PropTypes.func.isRequired | ||
}).isRequired, | ||
signup: PropTypes.func.isRequired | ||
}; | ||
|
||
export default connect(null, { signup })(SignupPage); |