<commit_before>var $ = require('jquery');
import React from 'react';
import { withRouter } from 'react-router';

import RobotsTableRow from './RobotsTableRow.jsx';

var RobotsTableBody = withRouter (
  React.createClass({
    render: function(){
      return (
        <tbody>
          {
            this.state.robots.map( function(robot){
              return (
                <RobotsTableRow key={robot._id} robot={robot}/>
              );
            })
          }
        </tbody>
      )
    },

    //
    // EVENT LIFECYCLE
    //

    getInitialState: function() {
      console.log("TABLE BODY GET INITIAL STATE");
      return {
        robots: []
      };
    },

    componentWillMount: function(){
      console.log("TABLE BODY WILL MOUNT");
    },

    componentDidMount: function(){
      console.log("TABLE BODY DID MOUNT");
      this.getRobots(this.props.params);
    },

    componentWillReceiveProps: function(nextProps) {
      console.log("TABLE BODY WILL RECEIVE PROPS");
      this.getRobots(nextProps.params)
    },

    componentWillUpdate: function(nextProps, nextState) {
      console.log("TABLE BODY WILL UPDATE");
    },

    //
    // MY FUNCTIONS
    //

    getRobots: function(paramz){
      if(paramz.id){
        console.log("GET ROBOT");
        $.ajax({
          url: '/api/robots/'+paramz.id,
          dataType: 'json',
          cache: false,
          success: function(data) {
            console.log("SET ROBOT");
            this.setState({robots: [data]});
          }.bind(this),
          error: function(xhr, status, err) {
            console.error(xhr, status, err);
            console.log("COULDN'T SET ROBOT");
            this.props.router.push({
              pathname: '/',
              state: {
                robots: [],
                flash: {danger: ["Couldn't find robot #"+paramz.id]}
              }
            });
          }.bind(this)
        });
      } else {
        console.log("GET ROBOTS");
        $.ajax({
          url: '/api/robots',
          dataType: 'json',
          cache: false,
          success: function(data) {
            console.log("GOT ROBOTS")
            this.setState({robots: data});
          }.bind(this),
          error: function(xhr, status, err) {
            console.error(xhr, status, err);
            console.log("GOT NO ROBOTS");
            this.props.router.push({
              pathname: '/',
              state: {
                robots: [],
                flash: {danger: ["Couldn't find any robots"]}
              }
            });
          }.bind(this)
        });
      };
    }
  })
);

module.exports = RobotsTableBody;<commit_msg>starting to decompose this complex method<commit_after>var $ = require('jquery');
import React from 'react';
import { withRouter } from 'react-router';

import RobotsTableRow from './RobotsTableRow.jsx';

var RobotsTableBody = withRouter (
  React.createClass({
    render: function(){
      return (
        <tbody>
          {
            this.state.robots.map( function(robot){
              return (
                <RobotsTableRow key={robot._id} robot={robot}/>
              );
            })
          }
        </tbody>
      )
    },

    //
    // EVENT LIFECYCLE
    //

    getInitialState: function() {
      console.log("TABLE BODY GET INITIAL STATE");
      return {
        robots: []
      };
    },

    componentWillMount: function(){
      console.log("TABLE BODY WILL MOUNT");
    },

    componentDidMount: function(){
      console.log("TABLE BODY DID MOUNT");
      this.determineRobots(this.props.params);
    },

    componentWillReceiveProps: function(nextProps) {
      console.log("TABLE BODY WILL RECEIVE PROPS");
      this.determineRobots(nextProps.params)
    },

    componentWillUpdate: function(nextProps, nextState) {
      console.log("TABLE BODY WILL UPDATE");
    },

    //
    // MY FUNCTIONS
    //

    determineRobots: function(paramz){
      console.log("DETERMINING ROBOTS BASED ON PARAMS", paramz);
      var robots;
      if(paramz.id){
        this.setRobot(paramz.id);
      } else {
        this.setRobots();
      };
    },

    setRobot: function(robotId){
      var requestUrl = '/api/robots/'+robotId;
      console.log("AJAX REQUEST", requestUrl)
      $.ajax({
        url: requestUrl,
        dataType: 'json',
        cache: false,
        success: function(data) {
          console.log("REQUEST SUCCESS", data);
          this.setState({robots: [data]});
        }.bind(this),
        error: function(xhr, status, err) {
          console.log("REQUEST ERROR", xhr, status, err);
          this.props.router.push({
            pathname: '/',
            state: {
              robots: [],
              flash: {danger: ["Couldn't find robot #"+robotId]}
            }
          });
        }.bind(this)
      });
    },

    setRobots: function(){
      var requestUrl = '/api/robots';
      console.log("AJAX REQUEST", requestUrl)
      $.ajax({
        url: requestUrl,
        dataType: 'json',
        cache: false,
        success: function(data) {
          console.log("REQUEST SUCCESS", data);
          this.setState({robots: data});
        }.bind(this),
        error: function(xhr, status, err) {
          console.log("REQUEST ERROR", xhr, status, err);
          this.props.router.push({
            pathname: '/',
            state: {
              robots: [],
              flash: {danger: ["Couldn't find any robots"]}
            }
          });
        }.bind(this)
      });
    }
  })
);

module.exports = RobotsTableBody;
{'fraction_non_alphanumeric': 0.14136878031255615, 'fraction_numerical': 0.0, 'mean_word_length': 1.6950629235237173, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5648828', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1625, 'n_words': 326}