From ee18ddb53dde070994b3f6b8304a56dfe030e8a1 Mon Sep 17 00:00:00 2001 From: Benjamin Randolph <104036158+neb417@users.noreply.github.com> Date: Mon, 9 Oct 2023 09:10:40 -0600 Subject: [PATCH] Turbo stream income (#7) * add some stuff * Render updated total costs as stream * Render updated total costs as stream when new expense created * Render updated total costs as stream when expense destroyed * Lint --- app/controllers/application_controller.rb | 1 + .../set_dashboard_instance_variables.rb | 12 ++++++++ app/controllers/dashboard_controller.rb | 3 +- app/controllers/fixed_expenses_controller.rb | 11 +++++-- .../controllers/fixed_expense_controller.js | 30 +++++++++++++++++++ .../controllers/hello_controller.js | 7 ----- app/models/fixed_expense.rb | 4 +++ app/views/dashboard/index.html.erb | 9 +++++- app/views/fixed_expenses/_form.html.erb | 3 +- app/views/fixed_expenses/_index.html.erb | 29 +++++------------- .../fixed_expenses/create.turbo_stream.erb | 7 +++++ .../fixed_expenses/destroy.turbo_stream.erb | 5 ++++ app/views/fixed_expenses/index.html.erb | 12 ++++---- .../fixed_expenses/update.turbo_stream.erb | 5 ++++ app/views/shared/_total.html.erb | 3 ++ app/views/shared/_total_costs.html.erb | 17 +++++++++++ package-lock.json | 6 ++++ spec/requests/fixed_expenses_spec.rb | 2 +- spec/requests/incomes_spec.rb | 2 +- spec/routing/fixed_expenses_routing_spec.rb | 4 +-- spec/routing/incomes_routing_spec.rb | 4 +-- 21 files changed, 130 insertions(+), 46 deletions(-) create mode 100644 app/controllers/concerns/set_dashboard_instance_variables.rb create mode 100644 app/javascript/controllers/fixed_expense_controller.js delete mode 100644 app/javascript/controllers/hello_controller.js create mode 100644 app/views/fixed_expenses/create.turbo_stream.erb create mode 100644 app/views/fixed_expenses/destroy.turbo_stream.erb create mode 100644 app/views/fixed_expenses/update.turbo_stream.erb create mode 100644 app/views/shared/_total.html.erb create mode 100644 app/views/shared/_total_costs.html.erb create mode 100644 package-lock.json diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..102a448 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + include SetDashboardInstanceVariables end diff --git a/app/controllers/concerns/set_dashboard_instance_variables.rb b/app/controllers/concerns/set_dashboard_instance_variables.rb new file mode 100644 index 0000000..855860d --- /dev/null +++ b/app/controllers/concerns/set_dashboard_instance_variables.rb @@ -0,0 +1,12 @@ +module SetDashboardInstanceVariables + extend ActiveSupport::Concern + + included do + # before_action :set_variables + end + + def set_variables + Rails.logger.debug "**** Dashboard Vars Being Set ****" + @totals = FixedExpense.total_costs + end +end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 09bfa86..dff97de 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,7 +1,8 @@ class DashboardController < ApplicationController + include SetDashboardInstanceVariables def index @incomes = Income.all - @fixed_expenses = FixedExpense.all.order(annual_cost_cents: :desc) + @fixed_expenses = FixedExpense.get_ordered @totals = FixedExpense.total_costs end end diff --git a/app/controllers/fixed_expenses_controller.rb b/app/controllers/fixed_expenses_controller.rb index c9a281b..36d22df 100644 --- a/app/controllers/fixed_expenses_controller.rb +++ b/app/controllers/fixed_expenses_controller.rb @@ -25,7 +25,10 @@ def create respond_to do |format| if @fixed_expense.save + @totals = FixedExpense.total_costs + @fixed_expenses = FixedExpense.get_ordered format.html { redirect_to root_path, notice: "Fixed expense was successfully created." } + format.turbo_stream else format.html { render :new, status: :unprocessable_entity } format.json { render json: @fixed_expense.errors, status: :unprocessable_entity } @@ -37,8 +40,9 @@ def create def update respond_to do |format| if @fixed_expense.update_from_dashboard(params: params[:fixed_expense]) + @totals = FixedExpense.total_costs format.html { redirect_to root_path, notice: "Fixed expense was successfully updated." } - format.turbo_stream { render turbo_stream: turbo_stream.update(@fixed_expense) } + format.turbo_stream else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @fixed_expense.errors, status: :unprocessable_entity } @@ -49,9 +53,12 @@ def update # DELETE /fixed_expenses/1 or /fixed_expenses/1.json def destroy @fixed_expense.destroy + @totals = FixedExpense.total_costs + @fixed_expenses = FixedExpense.get_ordered respond_to do |format| format.html { redirect_to fixed_expenses_path, notice: "Fixed expense was successfully destroyed." } - format.turbo_stream { render turbo_stream: turbo_stream.remove(@fixed_expense) } + # format.turbo_stream { render turbo_stream: turbo_stream.remove(@fixed_expense) } + format.turbo_stream end end diff --git a/app/javascript/controllers/fixed_expense_controller.js b/app/javascript/controllers/fixed_expense_controller.js new file mode 100644 index 0000000..e35e972 --- /dev/null +++ b/app/javascript/controllers/fixed_expense_controller.js @@ -0,0 +1,30 @@ +import { Controller } from "@hotwired/stimulus" +// import { get } from "@rails/request.js" + +export default class extends Controller { + static targets = [ "fixedTotals" ] + + updateTotals(event) { + const data = Object.fromEntries(new FormData(event.target).entries()) + console.log(data) + console.log(data["fixed_expense[amount]"]) + console.log(data["fixed_expense[frequency]"]) + console.log(this.fixedTotalsTarget) + let amount = data["fixed_expense[amount]"] + let frequency = data["fixed_expense[frequency]"] + // get(`/fixed_expenses/total_expenses?`) + } +} + + +// export default class extends Controller { +// static targets = ["monthlyPaymentDisplay"] +// +// homeDataUpdate(event){ +// let homeId = event.target.selectedOptions[0].value +// let target = this.monthlyPaymentDisplayTarget.id +// get(`/recurring_payments/monthly_payment?home=${homeId}&target=${target}`,{ +// responseKind: "turbo-stream" +// }) +// } +// } \ No newline at end of file diff --git a/app/javascript/controllers/hello_controller.js b/app/javascript/controllers/hello_controller.js deleted file mode 100644 index 5975c07..0000000 --- a/app/javascript/controllers/hello_controller.js +++ /dev/null @@ -1,7 +0,0 @@ -import { Controller } from "@hotwired/stimulus" - -export default class extends Controller { - connect() { - this.element.textContent = "Hello World!" - } -} diff --git a/app/models/fixed_expense.rb b/app/models/fixed_expense.rb index 9a82aa3..cf1cbfc 100644 --- a/app/models/fixed_expense.rb +++ b/app/models/fixed_expense.rb @@ -71,4 +71,8 @@ def self.total_costs total_bi_weekly_cost: sum(&:bi_weekly_cost_cents).to_f / 100 ) end + + def self.get_ordered + all.order(annual_cost_cents: :desc) + end end diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index a08745d..2e2e4b4 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -4,7 +4,9 @@