<commit_before>const React = require('react');
const ReactDOM = require('react-dom');
import { Link, browserHistory } from 'react-router';

// TODO
// 1. Make labels invisible but don't use 'display: none'
// 2. Fill in route url
// 3. Session
// 4. Logic for first login

const Login = React.createClass({

  submitForm(e) {
    e.preventDefault();
    console.log('inside submit form onclick');
    const email = ReactDOM.findDOMNode(this.refs.emailAddress).value;
    const pw = ReactDOM.findDOMNode(this.refs.password).value;
    const pwAndEmailObj = {
      emailAddress: email,
      password: pw,
    };
    const url = 'http://localhost:3000/login'; // UPDATE WITH ROUTE
    const xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify(pwAndEmailObj));
    xhr.onreadystatechange = function () {
      this.processResponse(xhr);
    }.bind(this);
  },

  processResponse(xhr) {
    // If request is done
    if (xhr.readyState === 4) {
      console.log('readystate is 4');
      // And status is OK
      if (xhr.status === 200) {
        console.log('status is 200');
        this.parseDataAndSetState(xhr.responseText);
      } else {
        // If error, email or password was incorrect so display error
        console.log('Error: ' + xhr.status);
        this.displayError();
      }
    }
  },
  componentWillReceiveProps(newprops) {
    if (newprops.getState.isAdmin === false) {
      console.log('isAdmin is false');
      browserHistory.push('/resident/');
    }
    // Route to director pane
    if (newprops.getState.isAdmin) {
      console.log('isAdmin is true');
      browserHistory.push('/director/');
    }
  },

  parseDataAndSetState(responseData) {
    const response = JSON.parse(responseData);
    //localStorage.token = Math.random.toString(36).substring(7);
    console.log('response in parse ', response);
    const userEmail = response.email;
    const changedPW = response.changedPassword;
    const isAdmin = response.isAdmin;
    const dailyQuestions = response.dailyQuestions;
    const takenQuiz = response.takenQuiz;
    const quizAvailability = response.quizAvailability;
    const newStateObj = {
      userEmail,
      changedPW,
      isAdmin,
      dailyQuestions,
      takenQuiz,
      quizAvailability,
    };
    this.props.setAppState(newStateObj);
  },

  displayError() {
    document.getElementById('loginError').style.visibility = '';
  },

  render() {
    return (
      <div className="loginContainer">
        <div id="loginError" style={{ visibility: 'hidden' }}>
          Incorrect email address or password.<commit_msg>updated redirect logic in login file<commit_after>const React = require('react');
const ReactDOM = require('react-dom');
import { Link, browserHistory } from 'react-router';

// TODO
// 1. Make labels invisible but don't use 'display: none'
// 2. Fill in route url
// 3. Session
// 4. Logic for first login

const Login = React.createClass({

  submitForm(e) {
    e.preventDefault();
    console.log('inside submit form onclick');
    const email = ReactDOM.findDOMNode(this.refs.emailAddress).value;
    const pw = ReactDOM.findDOMNode(this.refs.password).value;
    const pwAndEmailObj = {
      emailAddress: email,
      password: pw,
    };
    const url = 'http://localhost:3000/login'; // UPDATE WITH ROUTE
    const xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify(pwAndEmailObj));
    xhr.onreadystatechange = function () {
      this.processResponse(xhr);
    }.bind(this);
  },

  processResponse(xhr) {
    // If request is done
    if (xhr.readyState === 4) {
      console.log('readystate is 4');
      // And status is OK
      if (xhr.status === 200) {
        console.log('status is 200');
        this.parseDataAndSetState(xhr.responseText);
      } else {
        // If error, email or password was incorrect so display error
        console.log('Error: ' + xhr.status);
        this.displayError();
      }
    }
  },

  redirectToUsersPane(isAdmin) {
    if (isAdmin) {
      browserHistory.push('/director/');
    } else {
      browserHistory.push('/resident');
    }
  },

  // Seems uncessesary, when you can redirect before setting state

  // componentWillReceiveProps(newprops) {
  //   if (newprops.getState.isAdmin === false) {
  //     console.log('isAdmin is false');
  //     browserHistory.push('/resident/');
  //   }
  //   // Route to director pane
  //   if (newprops.getState.isAdmin) {
  //     console.log('isAdmin is true');
  //     browserHistory.push('/director/');
  //   }
  // },

  parseDataAndSetState(responseData) {
    const response = JSON.parse(responseData);
    //localStorage.token = Math.random.toString(36).substring(7);
    console.log('response in parse ', response);
    const userEmail = response.email;
    const changedPW = response.changedPassword;
    const isAdmin = response.isAdmin;
    const dailyQuestions = response.dailyQuestions;
    const takenQuiz = response.takenQuiz;
    const quizAvailability = response.quizAvailability;
    const newStateObj = {
      userEmail,
      changedPW,
      isAdmin,
      dailyQuestions,
      takenQuiz,
      quizAvailability,
    };
    this.redirectToUsersPane(isAdmin);
    this.props.setAppState(newStateObj);
  },

  displayError() {
    document.getElementById('loginError').style.visibility = '';
  },

  render() {
    return (
      <div className="loginContainer">
        <div id="loginError" style={{ visibility: 'hidden' }}>
          Incorrect email address or password.
{'fraction_non_alphanumeric': 0.1328042328042328, 'fraction_numerical': 0.006701940035273368, 'mean_word_length': 2.8815879534565365, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6310862', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1665, 'n_words': 380}