Skip to content

Commit

Permalink
Turbo stream income (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
neb417 authored Oct 9, 2023
1 parent e0bb004 commit ee18ddb
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 46 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
include SetDashboardInstanceVariables
end
12 changes: 12 additions & 0 deletions app/controllers/concerns/set_dashboard_instance_variables.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions app/controllers/fixed_expenses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand All @@ -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

Expand Down
30 changes: 30 additions & 0 deletions app/javascript/controllers/fixed_expense_controller.js
Original file line number Diff line number Diff line change
@@ -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"
// })
// }
// }
7 changes: 0 additions & 7 deletions app/javascript/controllers/hello_controller.js

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/fixed_expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<h1 class="text-3xl font-bold tracking-tight text-gray-900">Dashboard</h1>
</div>
</header>

<main>

<div class="bg-white shadow">
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
<%= turbo_frame_tag "income_header_frame" do %>
Expand All @@ -17,8 +19,8 @@
<%= turbo_frame_tag :incomes do %>
<%= render "incomes/index" %>
<% end %>
</div>
</div>
</div>

<div class="bg-white shadow">
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
Expand All @@ -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 %>
</div>
</div>

</main>
</div>
3 changes: 1 addition & 2 deletions app/views/fixed_expenses/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
<%= form.submit class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %>
</div>
<div>
<%= link_to "Cancel", root_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %>
</div>
<%= link_to "Cancel", root_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium", data:{ turbo_frame: :fixed_expenses } %>
</div>
</div>
</div>
Expand Down
29 changes: 7 additions & 22 deletions app/views/fixed_expenses/_index.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
<%= turbo_frame_tag :fixed_expenses do %>
<div class="grid grid-cols-6">
<% fixed_headings = ["Expense Name", "Annual Cost", "Monthly Cost", "Bi-weekly Cost"] %>
<% fixed_headings.each do |fixed_heading| %>
<div class="px-5"><strong><%= fixed_heading %></strong></div>
<% end %>
</div>
<div id="fixed_expense_labels" class="grid grid-cols-6">
<% fixed_headings = ["Expense Name", "Annual Cost", "Monthly Cost", "Bi-weekly Cost"] %>
<% fixed_headings.each do |fixed_heading| %>
<div class="px-5 py-4"><strong><%= fixed_heading %></strong></div>
<% end %>
</div>

<%= render @fixed_expenses %>

<div class="grid grid-cols-6">
<% 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| %>
<div class="px-5"><strong><%= total %></strong></div>
<% end %>
</div>
<% end %>
<%= render @fixed_expenses %>
7 changes: 7 additions & 0 deletions app/views/fixed_expenses/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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 %>
5 changes: 5 additions & 0 deletions app/views/fixed_expenses/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_stream.remove @fixed_expense %>

<%= turbo_stream.replace "total_costs" do %>
<%= render partial: "shared/total_costs", locals: { totals: @totals } %>
<% end %>
12 changes: 7 additions & 5 deletions app/views/fixed_expenses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<% end %>

<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Fixed expenses</h1>
<%= 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 %>
<h1 class="font-bold text-4xl">Fixed expenses</h1>
<%= 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 %>
</div>

<!-- <div id="fixed_expenses" class="min-w-full">-->
<%#= render @fixed_expenses %>
<!-- </div>-->
<div id="fixed_expenses" class="min-w-full">
<%= render @fixed_expenses %>
</div>
</div>
5 changes: 5 additions & 0 deletions app/views/fixed_expenses/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_stream.update @fixed_expense %>

<%= turbo_stream.replace "total_costs" do %>
<%= render partial: "shared/total_costs", locals: { totals: @totals } %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/shared/_total.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="px-5">
<span><strong><%= humanized_money_with_symbol(total) %></strong></span>
</div>
17 changes: 17 additions & 0 deletions app/views/shared/_total_costs.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="grid grid-cols-6">
<div class="px-5">
<strong>Total</strong>
</div>

<div>
<%= render partial: "shared/total", locals: { total: @totals.total_annual_cost } %>
</div>

<div>
<%= render partial: "shared/total", locals: { total: @totals.total_monthly_cost } %>
</div>

<div>
<%= render partial: "shared/total", locals: { total: @totals.total_bi_weekly_cost } %>
</div>
</div>
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/requests/fixed_expenses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/incomes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions spec/routing/fixed_expenses_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/routing/incomes_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ee18ddb

Please sign in to comment.