Skip to content

Commit

Permalink
Added feature to save previous calculations of the user in the sessio…
Browse files Browse the repository at this point in the history
…n storage
  • Loading branch information
SleekMutt committed Nov 29, 2024
1 parent 62610c8 commit 02d9977
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/controllers/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def calculate

@results = Calculators::CalculationService.new(@calculator, params[:inputs]).perform

session[:calculation_results] ||= {}
session[:calculation_results][@calculator.slug] = @results

respond_to :turbo_stream
end

Expand Down
10 changes: 7 additions & 3 deletions app/views/calculators/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
<% else %>
<%= form.select "inputs[#{field.var_name}]", options_from_collection_for_select(field.categories, :price, :name) %>
<% end %>
</div><br>
</div>
<br>
<% end %>
<div class="mt-6 form-actions">
<%= form.submit "Calculate", class: "bg-blue-500 text-white font-semibold px-4 py-2 rounded hover:bg-blue-700" %>
</div>
<% end %>

<%= turbo_frame_tag "calc-results" %>
<%= turbo_frame_tag "calc-results" do %>
<% if session.dig(:calculation_results, @calculator.slug)&.presence %>
<%= render "calculators/partials/calculation_results", results: session[:calculation_results][@calculator.slug].map(&:symbolize_keys) %>
<% end %>
<% end %>
</div>

0 comments on commit 02d9977

Please sign in to comment.