Skip to content

Commit

Permalink
Income switch (#14)
Browse files Browse the repository at this point in the history
* Add partials for income types

* Add route and method for switch to display income type
  • Loading branch information
neb417 authored Nov 14, 2023
1 parent 0d6c15d commit 7a22534
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
31 changes: 31 additions & 0 deletions app/controllers/incomes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@ def destroy
end
end

def income_switch
totals = FixedExpense.total_costs
if params[:enabled] == "0"
salary = Income.tax_on_income(income_type: "Salary")

respond_to do |format|
format.turbo_stream {
render turbo_stream: turbo_stream.replace("hourly_budget",
partial: "budget/salary_budget",
locals: {
totals: totals,
income: salary
})
}
end
else
hourly = Income.tax_on_income(income_type: "Hourly")

respond_to do |format|
format.turbo_stream {
render turbo_stream: turbo_stream.replace("salary_budget",
partial: "budget/hourly_budget",
locals: {
totals: totals,
income: hourly
})
}
end
end
end

private

# Use callbacks to share common setup or constraints between actions.
Expand Down
12 changes: 7 additions & 5 deletions app/views/budget/_hourly_budget.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<div id="hourly_budget" class="py-4">
<strong>Hourly</strong>
<%= render partial: "budget/budget_headings" %>
<%= render partial: "shared/budget", locals: {totals: totals, income: income} %>
</div>
<%= turbo_frame_tag "hourly_budget" do %>
<div id="hourly_budget" class="py-4">
<strong>Hourly</strong>
<%= render partial: "budget/budget_headings" %>
<%= render partial: "shared/budget", locals: {totals: totals, income: income} %>
</div>
<% end %>
12 changes: 7 additions & 5 deletions app/views/budget/_salary_budget.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<div id="salary_budget" class="py-4">
<strong>Salary</strong>
<%= render partial: "budget/budget_headings" %>
<%= render partial: "shared/budget", locals: {totals: totals, income: income} %>
</div>
<%= turbo_frame_tag "salary_budget" do %>
<div id="salary_budget" class="py-4">
<strong>Salary</strong>
<%= render partial: "budget/budget_headings" %>
<%= render partial: "shared/budget", locals: {totals: totals, income: income} %>
</div>
<% end %>
7 changes: 7 additions & 0 deletions app/views/components/_income_switch.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form_with url: income_switch_url, local: true do |form| %>
<label class="relative inline-flex items-center cursor-pointer">
<%= form.check_box :enabled, class: "sr-only peer", onchange: "this.form.requestSubmit()" %>
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>
<span class="ms-3 text-sm font-medium text-gray-900">Switch income type</span>
</label>
<% end %>
12 changes: 5 additions & 7 deletions app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
<div class="mt-5">
<h3 class="text-2xl font-bold tracking-tight text-gray-900">Final Budget</h3>
<div class="pt-4">
<%= render partial: "components/income_switch" %>
</div>
</div>

<%= turbo_frame_tag "salary_budget" do %>
<div id="final_income">
<%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %>
<% end %>

<%= turbo_frame_tag "hourly_budget" do %>
<%= render partial: "budget/hourly_budget", locals: {totals: @totals, income: @hourly_taxed} %>
<% end %>
</div>
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
resources :fixed_expenses
root "dashboard#index"
resources :incomes
post "/income_switch", to: "incomes#income_switch"
end

0 comments on commit 7a22534

Please sign in to comment.