<commit_before>import React from 'react';
import { Redirect } from 'react-router';
import './Login.scss';
import AuthData from '../../data/Auth';

export default class Login extends React.Component {
  constructor() {
    super();
    this.state = {
      email: '',
      password: '',
      confirmPassword: '',
      wantsToSignUp: false,
      redirectToReferrer: false,
    };
  }

  handleEmailChange(e) { this.setState({ email: e.target.value }); }

  handlePasswordChange(e) { this.setState({ password: e.target.value }); }

  handlePassConfirm(e) { this.setState({ confirmPassword: e.target.value }); }

  toggleSignUp() {
    this.state.wantsToSignUp
      ? this.setState({ wantsToSignUp: false })
      : this.setState({ wantsToSignUp: true });
  }

  authenticate(res) {
    if (res) {
      AuthData.authenticate();
      this.setState({ redirectToReferrer: true });
    }
  }

  handlePasswordChange(e) {
    const password = e.target.value;
    this.setState({ password });
  }

  handlePasswordConfirm(e) {
    const confirmPassword = e.target.value;
    this.setState({ confirmPassword });
  }

  handleSignUp() {
    const { email, password, confirmPassword } = this.state;
    if (password === confirmPassword) {
      AuthData.handleSignUp({ email, password })
        // proceed to sign in
        .then(() => this.handleSignIn());
    }
  }

  wantsToSignUp() {
    this.setState({ wantsToSignUp: true });
  }

  wantsToLogIn() {
    this.setState({ wantsToSignUp: false });
  }

  handleSignIn() {
    // TODO: complete logic
    const { email, password } = this.state;
    AuthData.signInWithEmailAndPassword({ email, password })
      .then(() => {
        AuthData.authenticate();
        this.setState({ redirectToReferrer: true });
      });
      // .then(res => console.log(res, 'asdfasdfasdf'));
  }

  render() {
    // const { from } = this.props.location.state || '/'
    const { redirectToReferrer } = this.state;

    return (
      <div>
        {redirectToReferrer && <Redirect to={'/notes'} />}
        <form className="auth-form" onSubmit={e => this.handleSubmit(e)}>
          <h1>{this.state.wantsToSignUp ? 'Sign up' : 'Sign in'}</h1>
          <div>
            <label htmlFor="email">Email</label>
            <input
              type="email"
              name="email"
              id="email"
              placeholder="Email"
              required
              onChange={e => this.handleEmailChange(e)}
            />
          </div>
          <div>
            <label htmlFor="password">Password</label>
            <input
              type="password"
              name="password"<commit_msg>Add clarifying comments and rearrange method order<commit_after>import React from 'react';
import { Redirect } from 'react-router';
import './Login.scss';
import AuthData from '../../data/Auth';

export default class Login extends React.Component {
  constructor() {
    super();
    this.state = {
      email: '',
      password: '',
      confirmPassword: '',
      wantsToSignUp: false,
      redirectToReferrer: false,
    };
  }

  handleEmailChange(e) { this.setState({ email: e.target.value }); }

  handlePasswordChange(e) { this.setState({ password: e.target.value }); }

  handlePassConfirm(e) { this.setState({ confirmPassword: e.target.value }); }

  toggleSignUp() {
    this.state.wantsToSignUp
      ? this.setState({ wantsToSignUp: false })
      : this.setState({ wantsToSignUp: true });
  }

  authenticate(res) {
    if (res) {
      AuthData.authenticate();
      this.setState({ redirectToReferrer: true });
    }
  }

  /**
   * Sign in with 3rd party auth provider
   */
  signInWithProvider(e, provider) {
    e.preventDefault();
    AuthData.signInWithProvider(provider)
      .then(res => this.authenticate(res));
  }

  /**
   * Sign in with email and password
   */
  handleSignIn() {
    const { email, password } = this.state;
    AuthData.signInWithEmailAndPassword({ email, password }, alert => this.props.alertHandler(alert))
      .then(res => this.authenticate(res));
  }

  /**
   * Sign up with email and password
   */
  handleSignUp() {
    const { email, password, confirmPassword } = this.state;
    if (password === confirmPassword) {
      AuthData.handleSignUp({ email, password })
        // proceed to sign in
        .then(() => this.handleSignIn());
    }
  }

  handleSubmit(e) {
    e.preventDefault();
    this.state.wantsToSignUp
      ? this.handleSignUp()
      : this.handleSignIn();
  }

  render() {
    // const { from } = this.props.location.state || '/'
    const { redirectToReferrer } = this.state;

    return (
      <div>
        {redirectToReferrer && <Redirect to={'/notes'} />}
        <form className="auth-form" onSubmit={e => this.handleSubmit(e)}>
          <h1>{this.state.wantsToSignUp ? 'Sign up' : 'Sign in'}</h1>
          <div>
            <label htmlFor="email">Email</label>
            <input
              type="email"
              name="email"
              id="email"
              placeholder="Email"
              required
              onChange={e => this.handleEmailChange(e)}
            />
          </div>
          <div>
            <label htmlFor="password">Password</label>
            <input
              type="password"
              name="password"
{'fraction_non_alphanumeric': 0.1472809667673716, 'fraction_numerical': 0.0009441087613293052, 'mean_word_length': 2.3210031347962383, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015773', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1579, 'n_words': 314}