From eea04761099409ba398aa1e9430354ea41121f0d Mon Sep 17 00:00:00 2001 From: Elisa Date: Wed, 3 Jul 2024 15:53:56 -0600 Subject: [PATCH 1/3] Add tooltips and debug ca,paign#new -WIP --- .rubocop_todo.yml | 17 ++- app/controllers/campaigns_controller.rb | 108 ++++++++++++++++-- app/javascript/application.js | 11 ++ .../controllers/wallet_controller.js | 24 +++- app/views/home/index.html.erb | 33 ++++-- app/views/layouts/application.html.erb | 34 ++++++ 6 files changed, 199 insertions(+), 28 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c871b63..813aa2e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,15 +1,15 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-06-25 17:50:29 UTC using RuboCop version 1.64.1. +# on 2024-07-03 21:53:32 UTC using RuboCop version 1.64.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 3 +# Offense count: 4 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: - Max: 24 + Max: 45 # Offense count: 8 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. @@ -17,22 +17,27 @@ Metrics/AbcSize: Metrics/BlockLength: Max: 66 +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ClassLength: + Max: 134 + # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: Max: 9 -# Offense count: 6 +# Offense count: 8 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: - Max: 16 + Max: 30 # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: Max: 9 -# Offense count: 28 +# Offense count: 27 # Configuration parameters: AllowedConstants. Style/Documentation: Enabled: false diff --git a/app/controllers/campaigns_controller.rb b/app/controllers/campaigns_controller.rb index 29a1855..f61e7c3 100644 --- a/app/controllers/campaigns_controller.rb +++ b/app/controllers/campaigns_controller.rb @@ -6,7 +6,7 @@ class CampaignsController < ApplicationController before_action :set_user_for_modification, except: %i[index show] before_action :authorize_user!, only: %i[edit update destroy] before_action :check_campaign_existence, only: %i[new create] - before_action :set_repository, only: %i[new create] + # before_action :set_repository, only: %i[new create] def index @campaigns = Campaign.all @@ -22,10 +22,55 @@ def show end def new - @campaign = @user.campaigns.build - @repo_name = params[:repo_name] || @campaign.repo_identifier - @campaign.repo_identifier ||= @repo_name - @wallet = find_wallet_for_repo_owner(@repository) + unless current_user.wallet&.id + flash[:alert] = 'You need to connect a wallet to create a campaign.' + redirect_to request.referer || root_path + return + end + + pp 'new' + pp params[:receiving_wallet_id] + + @repo_name = params[:repo_name] + @receiving_wallet_id = current_user.wallet&.id + + @campaign = current_user.campaigns.build(repo_identifier: @repo_name, receiving_wallet_id: @receiving_wallet_id) + + github_data = fetch_github_data(@repo_name) + @repo = github_data[:repo] + + if @repo.nil? + logger.error "Repository data is nil for repo_name: #{@repo_name}" + render plain: 'Repository not found', status: :not_found + return + end + + @campaign.title = "New Campaign for #{@repo_name}" + @campaign.description = "This is a new campaign for the repository #{@repo_name}." + @campaign.accepted_currencies = %w[USD EUR] + @campaign.tier_name = 'Supporter' + @campaign.tier_amount = 10 + @campaign.contribution_cadence = 'monthly' + + if @campaign.save + flash[:success] = 'Campaign created successfully' + redirect_to user_campaign_path(current_user, @campaign) + else + flash[:error] = 'There was an error creating the campaign' + render :new + end + # @receiving_wallet_id = find_wallet_for_repo_owner(@repo_name) + # @campaign = @user.campaigns.build + # @repo_name = params[:repo_name] || @campaign.repo_identifier + # @campaign.repo_identifier ||= @repo_name + # @wallet = find_wallet_for_repo_owner(@repository) + # rescue StandardError => e + # logger.error "Error in new action: #{e.message}" + # pp "@campaign.repo_identifier ||= @repo_name: #{@campaign.repo_identifier ||= @repo_name}" + # pp @campaign + # pp @repo_name + # pp @wallet + # render plain: 'Internal Server Error', status: :internal_server_error end def create @@ -81,10 +126,29 @@ def set_campaign end end - def set_repository - github_service = GithubService.new(current_user) - @repositories = github_service.fetch_repositories - @repository = @repositories.find { |repo| repo.id == params[:repository_id].to_i } + def fetch_github_data(_repo_name) + { + repo: { + id: 203_172_436, + name: 'bookyourbeach', + full_name: 'codersquirrelbln/bookyourbeach', + owner: { + login: 'codersquirrelbln', + id: 50_495_826, + avatar_url: 'https://avatars.githubusercontent.com/u/50495826?v=4' + } + } + } + # github_data = GithubApiHelper.fetch_github_data(current_user) + # pp 'set repo' + # @repositories = github_data[:repos] + # first_three_repos = @repositories.first(3) + # pp first_three_repos + # @repository = @repositories.find { |repo| repo[:repo].id == params[:repository_id].to_i } + # pp "repo: #{@repository}" + # rescue StandardError => e + # logger.error "Error setting repository: #{e.message}" + # raise end def authorize_user! @@ -105,9 +169,31 @@ def log_errors(campaign) Rails.logger.debug campaign.errors.full_messages.to_sentence end - def find_wallet_for_repo_owner(repository) - owner = User.find_by(uid: repository.owner.id) + def find_wallet_for_repo_owner(repo_name) + github_data = fetch_github_data(repo_name) + repo = github_data[:repo] + owner_login = repo[:owner][:login] + + owner = User.find_by(uid: owner_login) owner&.wallets&.first + + # github_data = GithubApiHelper.fetch_github_data(current_user) + # @repositories = github_data[:repos] + # pp "first repo: #{@repositories.first[:full_name]}" + # pp 'repo included?' + # @repositories.each do |repo| + # pp 'same name?' + # pp(repo[:full_name] == repository) + # end + + # pp '======================' + # pp "repository passed to find wallet method: #{repository}" + # pp '======================' + + # pp 'find wallet' + # owner = User.find_by(uid: repository.owner.id) + # pp owner + # owner&.wallets&.first end def check_campaign_existence diff --git a/app/javascript/application.js b/app/javascript/application.js index 48f17d4..d55cd4f 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -2,3 +2,14 @@ import "@hotwired/turbo-rails" import "./controllers" import "../assets/stylesheets/application.tailwind.css"; + + +document.addEventListener('DOMContentLoaded', () => { + const alertElement = document.querySelector('.alert'); + if (alertElement) { + const alertMessage = alertElement.getAttribute('data-alert-message'); + if (alertMessage) { + alert(alertMessage); + } + } +}); \ No newline at end of file diff --git a/app/javascript/controllers/wallet_controller.js b/app/javascript/controllers/wallet_controller.js index aa08c7d..e4fc54a 100644 --- a/app/javascript/controllers/wallet_controller.js +++ b/app/javascript/controllers/wallet_controller.js @@ -4,7 +4,7 @@ import { mainnet, arbitrum } from "viem/chains"; import { getAccount, reconnect } from "@wagmi/core"; export default class extends Controller { - static targets = ["openModal"]; + static targets = ["openModal", 'createCampaignButton', 'tooltipText']; static values = { projectId: String, userId: Number, @@ -54,7 +54,9 @@ export default class extends Controller { } else { console.log('Modal closed without disconnection'); } + this.updateButtonState(); }); + this.updateButtonState(); } openModal() { @@ -121,4 +123,24 @@ export default class extends Controller { this.disconnectRequested = false; } } + + updateButtonState() { + if (this.walletConnectedValue) { + this.createCampaignButtonTarget.disabled = false; + this.createCampaignButtonTarget.classList.remove("bg-gray-500", "cursor-not-allowed"); + this.createCampaignButtonTarget.classList.add("bg-blue-500", "hover:bg-blue-700"); + + // Hide the tooltip + this.tooltipTextTarget.classList.remove("opacity-100"); + this.tooltipTextTarget.classList.add("opacity-0"); + } else { + this.createCampaignButtonTarget.disabled = true; + this.createCampaignButtonTarget.classList.remove("bg-blue-500", "hover:bg-blue-700"); + this.createCampaignButtonTarget.classList.add("bg-gray-500", "cursor-not-allowed"); + + // Show the tooltip + this.tooltipTextTarget.classList.remove("opacity-0"); + this.tooltipTextTarget.classList.add("opacity-100"); + } + } } diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index b304585..9f002b9 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,4 +1,9 @@ -
+
<%= image_tag 'logo.jpg', alt: "Logo", class: 'my-6' %> <%= image_tag @avatar, alt: "Avatar", class: 'w-24 h-24 rounded-full' %> @@ -8,20 +13,12 @@ <%= "Hello, #{current_user.nickname}, welcome to Line - The Developers Marketplace " %>

<% wallet_connected = current_user.wallet.present? %> -
-
-

<% if @organizations.present? %>

    @@ -60,7 +57,23 @@ <% if campaign %> <%= link_to 'Show Campaign', user_campaign_path(current_user, campaign, repo_identifier: repo[:repo].full_name), class: "btn btn-success" %> <% else %> - <%= link_to 'Create Campaign', new_user_campaign_path(current_user, repo_name: repo[:repo].full_name), class: "btn btn-primary" %> +
    + + + Please connect a wallet to create campaigns + +
    <% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 76f4490..c85e354 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,12 +5,46 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> + <%= stylesheet_link_tag "tailwind", "inter-font", media: 'all', "data-turbo-track": "reload" %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbo-track': 'reload' %> <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
    + <% if flash[:alert] %> +
    + <% end %> <%= yield %>
    + + \ No newline at end of file From 48ed2f4f81417d65021dedf2840dd8ae53e24a33 Mon Sep 17 00:00:00 2001 From: Elisa Date: Mon, 8 Jul 2024 13:38:09 -0600 Subject: [PATCH 2/3] disable and enable create campaign button depending on wallet (dis)connected --- .../controllers/wallet_controller.js | 50 ++++++++++++------- app/views/home/index.html.erb | 2 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/javascript/controllers/wallet_controller.js b/app/javascript/controllers/wallet_controller.js index e4fc54a..355d4c2 100644 --- a/app/javascript/controllers/wallet_controller.js +++ b/app/javascript/controllers/wallet_controller.js @@ -10,11 +10,15 @@ export default class extends Controller { userId: Number, walletIdValue: Number, csrfToken: String, + walletConnected: Boolean, }; connect() { this.isDeleting = false; this.disconnectRequested = false; + this.walletConnectedValue = this.walletConnectedValue || false; + + this.updateButtonState(); const projectId = this.projectIdValue; const chains = [mainnet, arbitrum]; @@ -36,6 +40,14 @@ export default class extends Controller { }); reconnect(this.config); + this.account = getAccount(this.config); + if (this.account && this.account.isConnected) { + this.walletConnectedValue = true; + } else { + this.walletConnectedValue = false; + } + this.updateButtonState(); + this.modal = createWeb3Modal({ wagmiConfig: this.config, projectId: this.projectIdValue, @@ -48,15 +60,17 @@ export default class extends Controller { if (event.data.event === 'CONNECT_SUCCESS') { this.addWalletToDatabase(this.account); this.openModalTarget.textContent = 'Disconnect Wallet'; + this.walletConnectedValue = true; + this.updateButtonState(); } else if (event.data.event === 'MODAL_CLOSE' && this.disconnectRequested && !event.data.properties.connected) { this.removeWalletFromDatabase(this.account); this.openModalTarget.textContent = 'Connect Wallet'; + this.walletConnectedValue = false; + this.updateButtonState(); } else { console.log('Modal closed without disconnection'); } - this.updateButtonState(); }); - this.updateButtonState(); } openModal() { @@ -125,22 +139,24 @@ export default class extends Controller { } updateButtonState() { - if (this.walletConnectedValue) { - this.createCampaignButtonTarget.disabled = false; - this.createCampaignButtonTarget.classList.remove("bg-gray-500", "cursor-not-allowed"); - this.createCampaignButtonTarget.classList.add("bg-blue-500", "hover:bg-blue-700"); + this.createCampaignButtonTargets.forEach((button, index) => { + const tooltip = this.tooltipTextTargets[index]; + if (this.walletConnectedValue) { + button.disabled = false; + button.classList.remove("bg-gray-500", "cursor-not-allowed"); + button.classList.add("bg-blue-500", "hover:bg-blue-700"); - // Hide the tooltip - this.tooltipTextTarget.classList.remove("opacity-100"); - this.tooltipTextTarget.classList.add("opacity-0"); - } else { - this.createCampaignButtonTarget.disabled = true; - this.createCampaignButtonTarget.classList.remove("bg-blue-500", "hover:bg-blue-700"); - this.createCampaignButtonTarget.classList.add("bg-gray-500", "cursor-not-allowed"); + tooltip.classList.add("hidden"); + tooltip.classList.remove("block"); + } else { + button.disabled = true; + button.classList.remove("bg-blue-500", "hover:bg-blue-700"); + button.classList.add("bg-gray-500", "cursor-not-allowed"); - // Show the tooltip - this.tooltipTextTarget.classList.remove("opacity-0"); - this.tooltipTextTarget.classList.add("opacity-100"); - } + tooltip.classList.remove("hidden"); + tooltip.classList.add("block"); + } + }); } + } diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 9f002b9..334d269 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -57,7 +57,7 @@ <% if campaign %> <%= link_to 'Show Campaign', user_campaign_path(current_user, campaign, repo_identifier: repo[:repo].full_name), class: "btn btn-success" %> <% else %> -
    +
    + + <% else %> +

    No repos found.

    + <% end %> +
    +