import { LoadingSpinner } from '@sourcegraph/react-loading-spinner';
import { upperFirst } from 'lodash';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Form } from '../components/Form';
import { eventLogger } from '../tracking/eventLogger';
import { getReturnTo, PasswordInput } from './SignInSignUpCommon';
/**
 * The form for signing in with a username and password.
 */
export class UsernamePasswordSignInForm extends React.Component {
    constructor(props) {
        super(props);
        this.onEmailFieldChange = (e) => {
            this.setState({ email: e.target.value });
        };
        this.onPasswordFieldChange = (e) => {
            this.setState({ password: e.target.value });
        };
        this.handleSubmit = (event) => {
            event.preventDefault();
            if (this.state.loading) {
                return;
            }
            this.setState({ loading: true });
            eventLogger.log('InitiateSignIn');
            fetch('/-/sign-in', {
                credentials: 'same-origin',
                method: 'POST',
                headers: Object.assign({}, window.context.xhrHeaders, { Accept: 'application/json', 'Content-Type': 'application/json' }),
                body: JSON.stringify({
                    email: this.state.email,
                    password: this.state.password,
                }),
            })
                .then(resp => {
                if (resp.status === 200) {
                    const returnTo = getReturnTo(this.props.location);
                    window.location.replace(returnTo);
                }
                else if (resp.status === 401) {
                    throw new Error('User or password was incorrect');
                }
                else {
                    throw new Error('Unknown Error');
                }
            })
                .catch(err => {
                console.error('auth error: ', err);
                this.setState({ loading: false, errorDescription: (err && err.message) || 'Unknown Error' });
            });
        };
        this.state = {
            email: '',
            password: '',
            errorDescription: '',
            loading: false,
        };
    }
    render() {
        return (React.createElement(Form, { className: "signin-signup-form signin-form", onSubmit: this.handleSubmit },
            window.context.allowSignup ? (React.createElement(Link, { className: "signin-signup-form__mode", to: `/sign-up${this.props.location.search}` }, "Don't have an account? Sign up.")) : (React.createElement("p", { className: "text-muted" }, "To create an account, contact the site admin.")),
            this.state.errorDescription !== '' && (React.createElement("div", { className: "alert alert-danger my-2" },
                "Error: ",
                upperFirst(this.state.errorDescription))),
            React.createElement("div", { className: "form-group" },
                React.createElement("input", { className: `form-control signin-signup-form__input`, type: "text", placeholder: "Username or email", onChange: this.onEmailFieldChange, required: true, value: this.state.email, disabled: this.state.loading, autoFocus: true, autoComplete: "username email" })),
            React.createElement("div", { className: "form-group" },
                React.createElement(PasswordInput, { className: "signin-signup-form__input", onChange: this.onPasswordFieldChange, value: this.state.password, required: true, disabled: this.state.loading, autoComplete: "current-password" })),
            React.createElement("div", { className: "form-group" },
                React.createElement("button", { className: "btn btn-primary btn-block", type: "submit", disabled: this.state.loading }, "Sign in"),
                window.context.resetPasswordEnabled && (React.createElement("small", { className: "form-text text-muted" },
                    React.createElement(Link, { to: "/password-reset" }, "Forgot password?")))),
            this.state.loading && (React.createElement("div", { className: "signin-signup-form__loader" },
                React.createElement(LoadingSpinner, { className: "icon-inline" })))));
    }
}
//# sourceMappingURL=UsernamePasswordSignInForm.js.map