soapbox/app/gabsocial/features/auth_login/index.js

59 wiersze
1.5 KiB
JavaScript
Czysty Zwykły widok Historia

2020-04-04 01:52:13 +00:00
import React from 'react';
2020-04-04 20:28:57 +00:00
import { connect } from 'react-redux'
2020-04-04 01:52:13 +00:00
import ImmutablePureComponent from 'react-immutable-pure-component';
2020-04-05 21:54:51 +00:00
import { createAuthApp, logIn } from 'gabsocial/actions/auth';
import { Redirect } from 'react-router-dom';
2020-04-06 01:29:19 +00:00
import { fetchMe } from 'gabsocial/actions/me';
const mapStateToProps = (state, props) => ({
me: state.get('me'),
});
2020-04-04 01:52:13 +00:00
2020-04-04 20:28:57 +00:00
class LoginForm extends ImmutablePureComponent {
2020-04-06 21:20:03 +00:00
constructor(props) {
super(props);
this.state = {isLoading: false};
}
2020-04-04 20:28:57 +00:00
componentWillMount() {
2020-04-05 21:54:51 +00:00
this.props.dispatch(createAuthApp());
2020-04-04 01:52:13 +00:00
}
2020-04-04 20:28:57 +00:00
getFormData = (form) => {
2020-04-04 01:52:13 +00:00
return Object.fromEntries(
Array.from(form).map(i => [i.name, i.value])
);
}
2020-04-04 20:28:57 +00:00
handleSubmit = (event) => {
2020-04-06 01:29:19 +00:00
const { dispatch } = this.props;
const { username, password } = this.getFormData(event.target);
dispatch(logIn(username, password)).then(() => {
2020-04-06 21:20:03 +00:00
return dispatch(fetchMe());
}).catch(() => {
// TODO: Handle bad request
this.setState({isLoading: false});
2020-04-06 01:29:19 +00:00
});
2020-04-06 21:20:03 +00:00
this.setState({isLoading: true});
2020-04-04 01:52:13 +00:00
event.preventDefault();
}
render() {
const { me } = this.props;
if (me) return <Redirect to="/home" />;
2020-04-04 01:52:13 +00:00
return (
<form onSubmit={this.handleSubmit}>
2020-04-06 21:20:03 +00:00
<fieldset disabled={this.state.isLoading}>
<input name='username' placeholder='me@example.com' />
<input name='password' type='password' placeholder='Password' />
<input type='submit' value='Login' />
</fieldset>
2020-04-04 01:52:13 +00:00
</form>
)
}
}
2020-04-04 20:28:57 +00:00
export default connect(mapStateToProps)(LoginForm);