<commit_before>import React from 'react';
import LoginForm from '../components/LoginForm.jsx';
import Auth from '../modules/Auth';

const LOGIN_URL = '/api/login';

class LoginPage extends React.Component {
  /** @constructor */
  constructor(props) {
    super(props);

    const existingSuccessMessage = localStorage.getItem('successMessage');

    let successMessage = '';

    if (existingSuccessMessage) {
      successMessage = existingSuccessMessage;
      localStorage.removeItem('successMessage');
    }

    // set the initial component state
    this.state = {
      errors: {},
      successMessage,
      user: {
        email: '',
        password: '',
      },
    };

    this.processForm = this.processForm.bind(this);
    this.changeUser = this.changeUser.bind(this);
  }

  /**
  * @function processForm
  * @param {Object} event - JavaScript event object
  */
  processForm(event) {
    // prevent default action. In this case, action is the form submission
    event.preventDefault();

    const data = encodeURIComponent(JSON.stringify({
      email: this.state.email,
      password: <PASSWORD>,
    }));

    fetch(LOGIN_URL, {
      method: 'POST',
      body: data,
    })
      .then((response) => {
        switch (response.status) {
          case 200: {
            this.setState({
              errors: {},
            });
            if ('token' in response) {
              Auth.setToken(response.token);
            } else {
              throw new Error('No token in response', response);
            }
            break;
          }
          default: {
            const responseJson = response.json();
            const errors = responseJson.erros ? responseJson.erros : {};
            errors.summary = responseJson.message;

            this.setState({
              errors,
            });
          }
        }
      });
  }

  /**
  * @function changeUser
  * @param {Object} event - the JavaScript DOM event object
  */
  changeUser(event) {
    const field = event.target.name;
    const user = this.state.user;
    user[field] = event.target.value;

    this.setState({
      user,
    });
  }

  render() {
    return (
      <LoginForm
        onSubmit={this.processForm}
        onChange={this.changeUser}
        errors={this.state.errors}
        successMessage={this.state.successMessage}
        user={this.state.user}
      />
    );
  }
}

export default LoginPage;
<commit_msg>bug: (fix) 1. fix fetch issue of not sending req.body by adding headers; 2. fix redirect issue of not having this.context.replace by requiring react-router and use that<commit_after>import React from 'react';
import LoginForm from '../components/LoginForm.jsx';
import Auth from '../modules/Auth';

var Router = require('react-router');

const LOGIN_URL = '/api/login';

class LoginPage extends React.Component {
  /** @constructor */
  constructor(props) {
    super(props);

    const existingSuccessMessage = localStorage.getItem('successMessage');

    let successMessage = '';

    if (existingSuccessMessage) {
      successMessage = existingSuccessMessage;
      localStorage.removeItem('successMessage');
    }

    // set the initial component state
    this.state = {
      errors: {},
      successMessage,
      user: {
        email: '',
        password: '',
      },
    };

    this.processForm = this.processForm.bind(this);
    this.changeUser = this.changeUser.bind(this);
  }

  /**
  * @function processForm
  * @param {Object} event - JavaScript event object
  */
  processForm(event) {
    // prevent default action. In this case, action is the form submission
    event.preventDefault();

    const data = JSON.stringify({
      email: this.state.user.email,
      password: <PASSWORD>,
    });

    fetch(LOGIN_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: data,
    })
      .then((response) => {
        response.json()
          .then((responseJson) => {
            console.log('responseJson is', responseJson)
            switch (response.status) {
              case 200: {
                this.setState({
                  errors: {},
                });
                if ('token' in responseJson) {
                  Auth.setToken(responseJson.token);
                } else {
                  throw new Error('No token in response', responseJson);
                }
                console.log('trying to redirect')
                Router.browserHistory.push('/');
                // this.context.router.history.push('/');
                break;
              }
              default: {
                console.log('in error state')
                const errors = responseJson.errors ? responseJson.errors : {};
                errors.summary = responseJson.message;

                this.setState({
                  errors,
                });
              }
            }
          });
      });
  }

  /**
  * @function changeUser
  * @param {Object} event - the JavaScript DOM event object
  */
  changeUser(event) {
    const field = event.target.name;
    const user = this.state.user;
    user[field] = event.target.value;

    this.setState({
      user,
    });
  }

  render() {
    return (
      <LoginForm
        onSubmit={this.processForm}
        onChange={this.changeUser}
        errors={this.state.errors}
        successMessage={this.state.successMessage}
        user={this.state.user}
      />
    );
  }
}

export default LoginPage;

{'fraction_non_alphanumeric': 0.12316874660879001, 'fraction_numerical': 0.0014469162597214686, 'mean_word_length': 1.8998426848453067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2345923', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1476, 'n_words': 334}