Skip to content

Commit

Permalink
Preserve values when going back (#116)
Browse files Browse the repository at this point in the history
When closing the window and pressing X the values are now preserved for
additional adjustments.
  • Loading branch information
hendricius authored Oct 10, 2024
1 parent 6e99caa commit a500f40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions calculator/app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class StaticController < ApplicationController
def index
@dough = Dough.from_params(allowed_parameters)
end

def dough
Expand Down
20 changes: 20 additions & 0 deletions calculator/app/services/dough.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def quantity_pluralised_singularised
end
end

def extract_dough_parameters
{
hydration: hydration,
pizzas: pizzas,
weight: weight,
dough_type: dough_type,
yeast: yeast,
sourdough: sourdough,
salt: salt
}
end

def selected_if_yeast
is_yeast? ? 'selected="selected"' : ""
end

def selected_if_sourdough
!is_yeast? ? 'selected="selected"' : ""
end

private

def ingredients_yeast
Expand Down
4 changes: 2 additions & 2 deletions calculator/app/views/static/dough.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<div class="results-inner">
<div class="title">
<h1 class="dough-title">Your pizza dough 🍕</h1>
<a class="close" href="/">
<%= link_to root_path(@dough.extract_dough_parameters), class: "close" do %>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Back">
<path d="M12 9.6L2.4 0L0 2.4L9.6 12L0 21.6L2.4 24L12 14.4L21.6 24L24 21.6L14.4 12L24 2.4L21.6 0L12 9.6Z" fill="white"></path>
</svg>
</a>
<% end %>
</div>
<div class="base-info first">
<div class="left">Total pizzas:</div>
Expand Down
10 changes: 5 additions & 5 deletions calculator/app/views/static/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
InputComponent.new(
name: "pizzas",
label: "Number of pizzas",
value: 2,
value: @dough.pizzas,
step: 1,
)
) %>
Expand All @@ -21,7 +21,7 @@
InputComponent.new(
name: "weight_per_pizza",
label: "Weight per pizza (grams)",
value: 250,
value: @dough.weight.to_i,
step: 1,
)
) %>
Expand All @@ -30,7 +30,7 @@
InputComponent.new(
name: "hydration",
label: "Hydration (water quantity in %, baker's math)",
value: "60",
value: (@dough.hydration * 100).to_i,
step: "1",
min: "1",
max: "99"
Expand All @@ -41,8 +41,8 @@
<div class="form-group">
<label class="form-label" for="dough_type">Dough type</label>
<select name="dough_type" id="dough_type" class="form-input">
<option value="yeast" selected="selected">Yeast</option>
<option value="sourdough">Sourdough</option>
<option value="yeast" <%= @dough.selected_if_yeast %>>Yeast</option>
<option value="sourdough" <%= @dough.selected_if_sourdough %>>Sourdough</option>
</select>
</div>

Expand Down

0 comments on commit a500f40

Please sign in to comment.