<commit_before>const React = require('react');
const $ = require('jquery');

import setting from '../utilities/Setting';

const API_HOST = window.location.hostname;
const API_PORT = window.location.port || undefined;
const API_VERSION = '/api/v1/';
const API_URL = `http://${API_HOST}${API_PORT ? ':' + API_PORT : ''}${API_VERSION}`;

function checkForSponsor(campaign) {
  const partners = campaign.affiliates.partners;
  if (partners.length < 1) {
    return false;
  }

  for (let partner of partners) {
    if (partner.sponsor) {
      return true;
    }
  }

  return false;
}

function rankCampaigns(a, b) {
  const aScore = a.time_commitment - (checkForSponsor(a) ? 1 : 0);
  const bScore = b.time_commitment - (checkForSponsor(b) ? 1 : 0);
  return aScore - bScore;
}

/**
 * Card Component
 * <Card />
 */
class FeatureCard extends React.Component {
  constructor(props) {
    super(props);

    // These variables shouldn't force a state change & subsequent render
    this.requests = [];
    this.campaigns = [];
    this.campaignsToExclude = setting('dsUser.activity.signups', []);
    this.campaignsApiPage = 1;

    this.state = {
      campaignIndex: -1,
      isSignedUp: ''
    };
  }

  componentDidMount() {
    this.getCampaigns(true, true, 1, () => {
      // Set the index so the first campaign renders
      this.setState({
        campaignIndex: 0
      });

      if (this.campaigns.length == 0) {
        // For our super users who already signed up for them all...
        this.viewAnother();
      }
      else {
        // These are all staff pick campaigns which we don't want to display twice.
        this.campaignsToExclude.concat(this.campaigns.map(campaign => parseInt(campaign.id)));
      }
    });
  }

  getCampaigns(staffPickOnly, paginate, page, cb) {
    this.requests.push(this.serverRequest = $.get(`${API_URL}campaigns?${staffPickOnly ? 'staff_pick=true&' : ''}${page != -1 ? 'page=' + page : ''}`, result => {

      // Filter out campaigns users have signed up for & then rank remaining campaigns
      const campaigns = result.data.filter(campaign => this.campaignsToExclude.indexOf(parseInt(campaign.id)) == -1).sort(rankCampaigns);

      // Add it to the master list
      this.campaigns = this.campaigns.concat(campaigns);

      if (paginate) {
        // If we have pages left, paginate!
        if (result.pagination && result.pagination.total_pages > page) {
          this.getCampaigns(staffPickOnly, true, page + 1, cb);
        }
        else {
          cb();
        }
      }
      else {
        cb();
      }
    }));
  }

  componentWillUnmount() {
    if (this.requests) {
      for (var request in this.requests) {
        request.abort();
      }
    }
  }

  signup() {
    const campaign = this.campaigns[this.state.campaignIndex];

    if (!campaign) {
      return;
    }

    this.campaignsToExclude.push(campaign.id);

    this.requests.push($.ajax({
      url: `${API_URL}campaigns/${campaign.id}/signup`,
      type: 'POST',
      headers: {
        "X-CSRF-Token": $('meta[name=csrf-token]').attr("content")
      },
      data: {
        source: 'onboarding'
      },
      success: (data) => {
        this.setState({ //TODO: Fancy animation
          isSignedUp: true
        });

        setTimeout(() => {<commit_msg>One more quick fix<commit_after>const React = require('react');
const $ = require('jquery');

import setting from '../utilities/Setting';

const API_HOST = window.location.hostname;
const API_PORT = window.location.port || undefined;
const API_VERSION = '/api/v1/';
const API_URL = `http://${API_HOST}${API_PORT ? ':' + API_PORT : ''}${API_VERSION}`;

function checkForSponsor(campaign) {
  const partners = campaign.affiliates.partners;
  if (partners.length < 1) {
    return false;
  }

  for (let partner of partners) {
    if (partner.sponsor) {
      return true;
    }
  }

  return false;
}

function rankCampaigns(a, b) {
  const aScore = a.time_commitment - (checkForSponsor(a) ? 1 : 0);
  const bScore = b.time_commitment - (checkForSponsor(b) ? 1 : 0);
  return aScore - bScore;
}

/**
 * Card Component
 * <Card />
 */
class FeatureCard extends React.Component {
  constructor(props) {
    super(props);

    // These variables shouldn't force a state change & subsequent render
    this.requests = [];
    this.campaigns = [];
    this.campaignsToExclude = setting('dsUser.activity.signups', []);
    this.campaignsApiPage = 1;

    this.state = {
      campaignIndex: -1,
      isSignedUp: ''
    };
  }

  componentDidMount() {
    this.getCampaigns(true, true, 1, () => {
      // Set the index so the first campaign renders
      this.setState({
        campaignIndex: 0
      });

      if (this.campaigns.length == 0) {
        // For our super users who already signed up for them all...
        this.viewAnother();
      }
      else {
        // These are all staff pick campaigns which we don't want to display twice.
        this.campaignsToExclude.concat(this.campaigns.map(campaign => parseInt(campaign.id)));
      }
    });
  }

  getCampaigns(staffPickOnly, paginate, page, cb) {
    this.requests.push(this.serverRequest = $.get(`${API_URL}campaigns?${staffPickOnly ? 'staff_pick=true&' : ''}${page != -1 ? 'page=' + page : ''}`, result => {

      // Filter out campaigns users have signed up for & then rank remaining campaigns
      const campaigns = result.data.filter(campaign => this.campaignsToExclude.indexOf(parseInt(campaign.id)) == -1).sort(rankCampaigns);

      // Add it to the master list
      this.campaigns = this.campaigns.concat(campaigns);

      if (paginate) {
        // If we have pages left, paginate!
        if (result.pagination && result.pagination.total_pages > page) {
          this.getCampaigns(staffPickOnly, true, page + 1, cb);
        }
        else {
          cb();
        }
      }
      else {
        cb();
      }
    }));
  }

  componentWillUnmount() {
    if (this.requests) {
      for (var request in this.requests) {
        request.abort();
      }
    }
  }

  signup() {
    const campaign = this.campaigns[this.state.campaignIndex];

    if (!campaign) {
      return;
    }

    this.campaignsToExclude.push(campaign.id);

    this.requests.push($.ajax({
      url: `${API_URL}campaigns/${campaign.id}/signup`,
      type: 'POST',
      headers: {
        "X-CSRF-Token": $('meta[name=csrf-token]').attr("content")
      },
      data: {
        source: 'onboarding'
      },
      success: (data) => {
        this.campaigns[this.state.campaignIndex].signedUp = true;
        this.setState({ //TODO: Fancy animation
          isSignedUp: true
        });

        setTimeout(() => {
{'fraction_non_alphanumeric': 0.14454940770730246, 'fraction_numerical': 0.004198530514319988, 'mean_word_length': 2.5235076597992605, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 19, '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': '6989138', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2090, 'n_words': 469}