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 @@

Dashboard

+
+
<%= turbo_frame_tag "income_header_frame" do %> @@ -17,8 +19,8 @@ <%= turbo_frame_tag :incomes do %> <%= render "incomes/index" %> <% end %> -
+
@@ -32,7 +34,12 @@ <%= turbo_frame_tag :fixed_expenses do %> <%= render "fixed_expenses/index" %> <% end %> + + <%= turbo_frame_tag "total_costs" do %> + <%= render partial: "shared/total_costs", locals: { totals: @totals } %> + <% end %>
+
diff --git a/app/views/fixed_expenses/_form.html.erb b/app/views/fixed_expenses/_form.html.erb index 5ef407e..1bbf74f 100644 --- a/app/views/fixed_expenses/_form.html.erb +++ b/app/views/fixed_expenses/_form.html.erb @@ -38,8 +38,7 @@ <%= form.submit class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %>
- <%= link_to "Cancel", root_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %> -
+ <%= link_to "Cancel", root_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium", data:{ turbo_frame: :fixed_expenses } %> diff --git a/app/views/fixed_expenses/_index.html.erb b/app/views/fixed_expenses/_index.html.erb index b0b9d97..3ea8855 100644 --- a/app/views/fixed_expenses/_index.html.erb +++ b/app/views/fixed_expenses/_index.html.erb @@ -1,23 +1,8 @@ -<%= turbo_frame_tag :fixed_expenses do %> -
- <% fixed_headings = ["Expense Name", "Annual Cost", "Monthly Cost", "Bi-weekly Cost"] %> - <% fixed_headings.each do |fixed_heading| %> -
<%= fixed_heading %>
- <% end %> -
+
+ <% fixed_headings = ["Expense Name", "Annual Cost", "Monthly Cost", "Bi-weekly Cost"] %> + <% fixed_headings.each do |fixed_heading| %> +
<%= fixed_heading %>
+ <% end %> +
- <%= render @fixed_expenses %> - -
- <% total_costs = [ - "Total", - humanized_money_with_symbol(@totals.total_annual_cost), - humanized_money_with_symbol(@totals.total_monthly_cost), - humanized_money_with_symbol(@totals.total_bi_weekly_cost) - ] - %> - <% total_costs.each do |total| %> -
<%= total %>
- <% end %> -
-<% end %> \ No newline at end of file +<%= render @fixed_expenses %> diff --git a/app/views/fixed_expenses/create.turbo_stream.erb b/app/views/fixed_expenses/create.turbo_stream.erb new file mode 100644 index 0000000..be4f9b5 --- /dev/null +++ b/app/views/fixed_expenses/create.turbo_stream.erb @@ -0,0 +1,7 @@ +<%= turbo_stream.replace "fixed_expenses" do %> + <%= render @fixed_expenses %> +<% end %> + +<%= turbo_stream.replace "total_costs" do %> + <%= render partial: "shared/total_costs", locals: { totals: @totals } %> +<% end %> diff --git a/app/views/fixed_expenses/destroy.turbo_stream.erb b/app/views/fixed_expenses/destroy.turbo_stream.erb new file mode 100644 index 0000000..cb802b7 --- /dev/null +++ b/app/views/fixed_expenses/destroy.turbo_stream.erb @@ -0,0 +1,5 @@ +<%= turbo_stream.remove @fixed_expense %> + +<%= turbo_stream.replace "total_costs" do %> + <%= render partial: "shared/total_costs", locals: { totals: @totals } %> +<% end %> diff --git a/app/views/fixed_expenses/index.html.erb b/app/views/fixed_expenses/index.html.erb index cfa8ad0..2268232 100644 --- a/app/views/fixed_expenses/index.html.erb +++ b/app/views/fixed_expenses/index.html.erb @@ -4,11 +4,13 @@ <% end %>
-

Fixed expenses

- <%= link_to 'New fixed expense', new_fixed_expense_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> + <%= turbo_frame_tag :fixed_expenses do %> +

Fixed expenses

+ <%= link_to 'New fixed expense', new_fixed_expense_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> + <% end %>
- - <%#= render @fixed_expenses %> - +
+ <%= render @fixed_expenses %> +
diff --git a/app/views/fixed_expenses/update.turbo_stream.erb b/app/views/fixed_expenses/update.turbo_stream.erb new file mode 100644 index 0000000..be14367 --- /dev/null +++ b/app/views/fixed_expenses/update.turbo_stream.erb @@ -0,0 +1,5 @@ +<%= turbo_stream.update @fixed_expense %> + +<%= turbo_stream.replace "total_costs" do %> + <%= render partial: "shared/total_costs", locals: { totals: @totals } %> +<% end %> diff --git a/app/views/shared/_total.html.erb b/app/views/shared/_total.html.erb new file mode 100644 index 0000000..fa5ff61 --- /dev/null +++ b/app/views/shared/_total.html.erb @@ -0,0 +1,3 @@ +
+ <%= humanized_money_with_symbol(total) %> +
\ No newline at end of file diff --git a/app/views/shared/_total_costs.html.erb b/app/views/shared/_total_costs.html.erb new file mode 100644 index 0000000..fa125f7 --- /dev/null +++ b/app/views/shared/_total_costs.html.erb @@ -0,0 +1,17 @@ +
+
+ Total +
+ +
+ <%= render partial: "shared/total", locals: { total: @totals.total_annual_cost } %> +
+ +
+ <%= render partial: "shared/total", locals: { total: @totals.total_monthly_cost } %> +
+ +
+ <%= render partial: "shared/total", locals: { total: @totals.total_bi_weekly_cost } %> +
+
diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..0416adf --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "finance", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/spec/requests/fixed_expenses_spec.rb b/spec/requests/fixed_expenses_spec.rb index 60e1224..4bf6298 100644 --- a/spec/requests/fixed_expenses_spec.rb +++ b/spec/requests/fixed_expenses_spec.rb @@ -83,7 +83,7 @@ end end - describe "PATCH /update" do + describe "PATCH /index" do context "with valid parameters" do let(:new_attributes) { skip("Add a hash of attributes valid for your model") diff --git a/spec/requests/incomes_spec.rb b/spec/requests/incomes_spec.rb index f4c18e6..5159c01 100644 --- a/spec/requests/incomes_spec.rb +++ b/spec/requests/incomes_spec.rb @@ -83,7 +83,7 @@ end end - describe "PATCH /update" do + describe "PATCH /index" do context "with valid parameters" do let(:new_attributes) { skip("Add a hash of attributes valid for your model") diff --git a/spec/routing/fixed_expenses_routing_spec.rb b/spec/routing/fixed_expenses_routing_spec.rb index 83c5a26..991d33a 100644 --- a/spec/routing/fixed_expenses_routing_spec.rb +++ b/spec/routing/fixed_expenses_routing_spec.rb @@ -22,11 +22,11 @@ expect(post: "/fixed_expenses").to route_to("fixed_expenses#create") end - it "routes to #update via PUT" do + it "routes to #index via PUT" do expect(put: "/fixed_expenses/1").to route_to("fixed_expenses#update", id: "1") end - it "routes to #update via PATCH" do + it "routes to #index via PATCH" do expect(patch: "/fixed_expenses/1").to route_to("fixed_expenses#update", id: "1") end diff --git a/spec/routing/incomes_routing_spec.rb b/spec/routing/incomes_routing_spec.rb index d03193f..870f01b 100644 --- a/spec/routing/incomes_routing_spec.rb +++ b/spec/routing/incomes_routing_spec.rb @@ -22,11 +22,11 @@ expect(post: "/incomes").to route_to("incomes#create") end - it "routes to #update via PUT" do + it "routes to #index via PUT" do expect(put: "/incomes/1").to route_to("incomes#update", id: "1") end - it "routes to #update via PATCH" do + it "routes to #index via PATCH" do expect(patch: "/incomes/1").to route_to("incomes#update", id: "1") end