<commit_before>class HomeController < ApplicationController
  before_action :find_organization, only: [:search, :wishlists]
  before_action :find_wishlists, only: [:search, :wishlists]
  before_action :find_content, only: [:index, :search, :wishlists]

  def index
  end

  def login
    respond_to do |format|
      format.html
      format.json { render json: {:error => "No auth token sent"} }
    end
  end

  def logout
    reset_session
    redirect_to "https://#{Rails.application.config.auth0_endpoint}/v2/logout?returnTo=#{params['returnTo']}&client_id=#{params['client_id']}"
  end

  def library
  end

  def donate
    if params[:id_list].nil?
      @wishlists = []
      @donationLevel = 30.0
      @schoolname = "None"
      @Campaign = "General"
      @reader_name = ""
      @wishListID = ""
      @slug = ""
      if !params[:campaign_id].nil? && campaign = Campaign.find(params[:campaign_id])
        @Campaign = campaign.name
        @schoolname = campaign.organization.name
        @slug = campaign.organization.slug
      end
      if !params[:organization_id].nil? && org = Organization.find(params[:organization_id])
        @schoolname = org.name
        @slug = org.slug
      end
    else
      school = {}
      campaign = {}
      reader = {}
      slug = {}

      @wishlists = Wishlist.joins([{:campaign => :organization}]).where("wishlists.id in (?)", params[:id_list].split(",").map(&:to_i)).all
      @wishlists.each{|w|
        school[w.campaign.organization.name] = true
        campaign[w.campaign.name] = true
        reader[w.reader_name] = true
        slug[w.campaign.organization.slug] = true
      }
      @schoolname = school.keys.join(",")
      @donationLevel = 30.0*@wishlists.count #TODO: Make the amount configurable
      @Campaign = campaign.keys.join(",")
      @reader_name = reader.keys.join(",")
      @wishListID = params[:id_list]
      @slug = slug.keys.join("") if slug.count == 1
      session[:wishlist_cart] = params[:id_list]
    end
  end

  def search
  end

  def wishlists
    respond_to do |format|
      format.js {}
    end
  end

  def success
    @wishlists = Wishlist.joins([{:campaign => :organization}]).where("wishlists.id in (?)", params[:id_list].split(",").map(&:to_i)).all
    @wishlists.each do |w|
      @donation = w.donations.create!({:confirmation_code => params[:confirmation_code], :amount => params[:amount]})
    end

    session[:wishlist_cart] = nil

    respond_to do |format|
      format.html { render "index" }
      format.json { render json: @donation }
    end
  end

private

  def find_organization
    unless params[:slug].nil?
      @organization = Organization.find_by_slug(params[:slug])
    end

    #if !params[:term].nil? && @organization.nil?
    #  @organization = Organization.find_by_name(params[:term])
    #end
  end

  def find_wishlists
    term = params[:term]<commit_msg>Splits donations amongst all selected wish lists<commit_after>class HomeController < ApplicationController
  before_action :find_organization, only: [:search, :wishlists]
  before_action :find_wishlists, only: [:search, :wishlists]
  before_action :find_content, only: [:index, :search, :wishlists]

  def index
  end

  def login
    respond_to do |format|
      format.html
      format.json { render json: {:error => "No auth token sent"} }
    end
  end

  def logout
    reset_session
    redirect_to "https://#{Rails.application.config.auth0_endpoint}/v2/logout?returnTo=#{params['returnTo']}&client_id=#{params['client_id']}"
  end

  def library
  end

  def donate
    if params[:id_list].nil?
      @wishlists = []
      @donationLevel = 30.0
      @schoolname = "None"
      @Campaign = "General"
      @reader_name = ""
      @wishListID = ""
      @slug = ""
      if !params[:campaign_id].nil? && campaign = Campaign.find(params[:campaign_id])
        @Campaign = campaign.name
        @schoolname = campaign.organization.name
        @slug = campaign.organization.slug
      end
      if !params[:organization_id].nil? && org = Organization.find(params[:organization_id])
        @schoolname = org.name
        @slug = org.slug
      end
    else
      school = {}
      campaign = {}
      reader = {}
      slug = {}

      @wishlists = Wishlist.joins([{:campaign => :organization}]).where("wishlists.id in (?)", params[:id_list].split(",").map(&:to_i)).all
      @wishlists.each{|w|
        school[w.campaign.organization.name] = true
        campaign[w.campaign.name] = true
        reader[w.reader_name] = true
        slug[w.campaign.organization.slug] = true
      }
      @schoolname = school.keys.join(",")
      @donationLevel = 30.0*@wishlists.count #TODO: Make the amount configurable
      @Campaign = campaign.keys.join(",")
      @reader_name = reader.keys.join(",")
      @wishListID = params[:id_list]
      @slug = slug.keys.join("") if slug.count == 1
      session[:wishlist_cart] = params[:id_list]
    end
  end

  def search
  end

  def wishlists
    respond_to do |format|
      format.js {}
    end
  end

  def success
    @wishlists = Wishlist.joins([{:campaign => :organization}]).where("wishlists.id in (?)", params[:id_list].split(",").map(&:to_i)).all
    amt = params[:amount].to_d/@wishlists.count unless params[:amount].nil? || @wishlists.count == 0
    @wishlists.each do |w|
      @donation = w.donations.create!({:confirmation_code => params[:confirmation_code], :amount => amt})
    end

    session[:wishlist_cart] = nil

    respond_to do |format|
      format.html { render "index" }
      format.json { render json: @donation }
    end
  end

private

  def find_organization
    unless params[:slug].nil?
      @organization = Organization.find_by_slug(params[:slug])
    end

    #if !params[:term].nil? && @organization.nil?
    #  @organization = Organization.find_by_name(params[:term])
    #end
  end

  def find_wishlists
    term = params[:term]
{'fraction_non_alphanumeric': 0.14276025771447948, 'fraction_numerical': 0.003221430993557138, 'mean_word_length': 3.015656909462219, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '5544233', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1916, 'n_words': 379}