Skip to content

Commit

Permalink
Add route and method for switch to display income type
Browse files Browse the repository at this point in the history
  • Loading branch information
neb417 committed Nov 14, 2023
1 parent 56013d9 commit f9889f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 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
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 %>
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 f9889f5

Please sign in to comment.