/
SignInFormContainer.js
68 lines (59 loc) · 1.5 KB
/
SignInFormContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import { withRouter } from 'react-router';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { connect } from 'react-redux';
import SignInForm from '../components/SignInForm';
import { signIn } from '../actions';
class SignInFormContainer extends React.Component {
constructor(props) {
super(props);
this.state = { errors: [] };
}
handleSubmit(values) {
this.props.mutate({ variables: values })
.then((response) => {
if (response.data.signIn.errors.length <= 0) {
this.props.signInDispatcher(response.data.signIn.token);
this.props.router.replace('/');
} else {
this.setState({
errors: response.data.signIn.errors
});
}
})
.catch((err) => {
console.error(err);
});
}
render() {
return (
<SignInForm
onSubmit={this.handleSubmit.bind(this)}
errors={this.state.errors}
/>
);
}
};
const signInMutation = gql`
mutation signIn($email: String!, $password: String!) {
signIn(email: $email, password: $password) {
token,
errors {
key
value
}
}
}
`;
const SignInWithData = graphql(signInMutation)(withRouter(SignInFormContainer));
const mapDispatchToProps = (dispatch) => ({
signInDispatcher(token) {
dispatch(signIn(token));
}
});
const SignInWithDataAndState = connect(
null,
mapDispatchToProps
)(SignInWithData);
export default SignInWithDataAndState;