<commit_before>'use strict';

var Solitaire = React.createClass({
  componentWillMount: function() {
    var onSuccess = function(response) {
      this.setState({
        cards: response.cards,
      });
    }.bind(this);
    var onError = function(response) {
      this.setState({
        error: response
      });
    }.bind(this);
    $.get(this.props.url).then(onSuccess, onError);
  },

  getInitialState() {
    return {
      cards: [],
      selected: new Set(),
      found: new Set()
    };
  },

  onClickSetCard: function(card, cardState) {
    if (cardState.selected) {
      this.state.selected.add(card);
    }
    else {
      this.state.selected.delete(card);
    }

    if (this.state.selected.size == 3) {
      $.ajax(this.props.url, {
        data: {
          cards: JSON.stringify([...this.state.selected].map(function(component) {
            return component.props.card;
          }))
        },
        method: 'PUT',
        contextType: 'application/json'
      }).then((response) => {
        switch(response['result']) {
          case 'OK':
            console.log('OK');
            this.setState({
              found: this.state.found.add(new Set(this.state.selected)),
              solved: response['solved']
            });
            break;
          case 'NOT_A_SET':
            console.log('NOT_A_SET'); break;
          case 'ALREADY_FOUND':
            console.log('ALREADY_FOUND'); break;
          default:
            throw("This shouldn't happen.");
        }
      }, (response) => {
        console.error(response);
      }).then(() => {
        console.log(this.state.selected);
        for (card of this.state.selected) {
          card.setState({
            selected: false
          });
        }
        this.state.selected.clear();
        console.log(this.state.selected);
      });
    }
  },

  renderSet: function(the_set) {
    return [...the_set].map((card_component) => {
      return `<${card_component.content()}>`;
    }).join(' ');
  },

  renderSetsFound: function() {
    return this.state.found.size ? (
      <div id="status">
        <ul>
          {[...this.state.found].map((the_set) => {
            return (
              <li>{this.renderSet(the_set)}</li>
            );
          })}
        </ul>
      </div>
    ) : (
      <div id="status">No sets found so far.</div>
    );
  },

  render: function() {
    return (
      <div id="cards">
        {this.state.solved ? <h3>Solved!</h3> : void 0}
        <ul>
          {this.state.cards.map((card) => {
            return (
              <li>
                <SetCard card={card}
                         parentHandleClick={this.onClickSetCard} />
              </li>
            );
          })}
        </ul>
        {this.renderSetsFound()}
      </div>
    );
  }
});

var SetCard = React.createClass({
  getInitialState() {
    return {
      selected: false
    };
  },

  handleClick: function(e) {
    var newState = {
      selected: !this.state.selected
    };
    this.setState(newState);
    this.props.parentHandleClick(this, newState);
  },

  content: function() {
    var card = this.props.card;
    return [card.number, card.color, card.shading, card.shape + (card.number == 'one' ? '' : 's')].join(' ');
  },

  render: function() {
    let classes = ["card"];
    if (this.state.selected) {
      classes.push("selected");
    }
    return (
      <div className={classes.join(" ")} onClick={this.handleClick}>{this.content()}</div>
    );
  }
});

ReactDOM.render(
  <Solitaire url="/game" />, document.getElementById('solitaire')
);<commit_msg>use es6 shorthand function syntax, replace var with let<commit_after>'use strict';

var Solitaire = React.createClass({
  componentWillMount() {
    let onSuccess = function(response) {
      this.setState({
        cards: response.cards,
      });
    }.bind(this);
    let onError = function(response) {
      this.setState({
        error: response
      });
    }.bind(this);
    $.get(this.props.url).then(onSuccess, onError);
  },

  getInitialState() {
    return {
      cards: [],
      selected: new Set(),
      found: new Set()
    };
  },

  onClickSetCard(card, cardState) {
    if (cardState.selected) {
      this.state.selected.add(card);
    }
    else {
      this.state.selected.delete(card);
    }

    if (this.state.selected.size == 3) {
      $.ajax(this.props.url, {
        data: {
          cards: JSON.stringify([...this.state.selected].map(function(component) {
            return component.props.card;
          }))
        },
        method: 'PUT',
        contextType: 'application/json'
      }).then((response) => {
        switch(response['result']) {
          case 'OK':
            console.log('OK');
            this.setState({
              found: this.state.found.add(new Set(this.state.selected)),
              solved: response['solved']
            });
            break;
          case 'NOT_A_SET':
            console.log('NOT_A_SET'); break;
          case 'ALREADY_FOUND':
            console.log('ALREADY_FOUND'); break;
          default:
            throw("This shouldn't happen.");
        }
      }, (response) => {
        console.error(response);
      }).then(() => {
        console.log(this.state.selected);
        for (card of this.state.selected) {
          card.setState({
            selected: false
          });
        }
        this.state.selected.clear();
        console.log(this.state.selected);
      });
    }
  },

  renderSet(the_set) {
    return [...the_set].map((card_component) => {
      return `<${card_component.content()}>`;
    }).join(' ');
  },

  renderSetsFound() {
    return this.state.found.size ? (
      <div id="status">
        <ul>
          {[...this.state.found].map((the_set) => {
            return (
              <li>{this.renderSet(the_set)}</li>
            );
          })}
        </ul>
      </div>
    ) : (
      <div id="status">No sets found so far.</div>
    );
  },

  render() {
    return (
      <div id="cards">
        {this.state.solved ? <h3>Solved!</h3> : void 0}
        <ul>
          {this.state.cards.map((card) => {
            return (
              <li>
                <SetCard card={card}
                         parentHandleClick={this.onClickSetCard} />
              </li>
            );
          })}
        </ul>
        {this.renderSetsFound()}
      </div>
    );
  }
});

var SetCard = React.createClass({
  getInitialState() {
    return {
      selected: false
    };
  },

  handleClick(e) {
    let newState = {
      selected: !this.state.selected
    };
    this.setState(newState);
    this.props.parentHandleClick(this, newState);
  },

  content() {
    let card = this.props.card;
    return [card.number, card.color, card.shading, card.shape + (card.number == 'one' ? '' : 's')].join(' ');
  },

  render() {
    let classes = ["card"];
    if (this.state.selected) {
      classes.push("selected");
    }
    return (
      <div className={classes.join(" ")} onClick={this.handleClick}>{this.content()}</div>
    );
  }
});

ReactDOM.render(
  <Solitaire url="/game" />, document.getElementById('solitaire')
);
{'fraction_non_alphanumeric': 0.16880759564367495, 'fraction_numerical': 0.0012566322256352975, 'mean_word_length': 1.8079184633477068, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 57, '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': '1176613', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2209, 'n_words': 360}