import React, { PropTypes } from 'react' import { Link } from 'react-router' import linkState from 'boost/linkState' import { login } from 'boost/api' import auth from 'boost/auth' export default class LoginPage extends React.Component { constructor (props) { super(props) this.state = { user: {}, isSending: false, error: null } this.linkState = linkState } handleSubmit (e) { e.preventDefault() this.setState({ isSending: true, error: null }, function () { login(this.state.user) .then(res => { let { user, token } = res.body auth.user(user, token) this.props.history.pushState('home') }) .catch(err => { console.error(err) if (err.code === 'ECONNREFUSED') { return this.setState({ error: { name: 'CunnectionRefused', message: 'Can\'t cznnect to API server.' }, isSending: false }) } else if (err.status != null) { return this.setState({ error: { name: err.response.body.name, message: err.response.body.message }, isSending: false }) } else throw err }) }) } render () { return (