Skip to content

Commit

Permalink
add column user_added to groceries, user can edit all groceries
Browse files Browse the repository at this point in the history
  • Loading branch information
Susannah Sun authored and Susannah Sun committed Sep 11, 2019
1 parent fceccda commit 00f5b0f
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 60 deletions.
Binary file modified app/assets/images/reicpe_images/cucumber_and_tomato_salad.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 39 additions & 4 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,27 @@ input[type="checkbox"] {
cursor: grab;
}

.fa-pen {
cursor: grab;
padding: .5em;
}

.grocery-item-form {
display: flex; flex-direction: row;
align-items: center; justify-content: center;
}

.grocery-info {
width: 15vw;
display: flex; flex-direction: row;
align-items: center; justify-content: center;
}

.users-groceries {
display: flex; flex-direction: column;
align-items: center; justify-content: center;
}

.show {
display: block;
}
Expand Down Expand Up @@ -722,6 +743,19 @@ a:active {
}
}

.fa-concierge-bell {
color: black;
}

.fa-concierge-bell:hover {
color: red;
cursor: grab;
animation: shake 0.82s cubic-bezier(.36,.07,.19,.97) both;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}

.error {
text-align: center;
margin-bottom: 1em;
Expand Down Expand Up @@ -770,14 +804,15 @@ small {
font-size: .7em;
}

.drumstick {
padding: .5em;
}

select {
font-size: 1em;
}

input[type="submit"] {
font-family: 'Raleway', sans-serif;
border: none;
}

/* option {
background: transparent;
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def current_user
helper_method(:current_user)

def is_produce?(item)
produce = ['cucumber', 'strawberry', 'onion', 'garlic', 'green onions', 'onion', 'red onion', 'yellow onion', 'jalapeno', 'corn', 'green bell pepper', 'tomato', 'avocado', 'banana', 'red chili pepper', 'oregano']
produce = ['cucumber', 'strawberry', 'onion', 'garlic', 'green onions', 'onion', 'red onion', 'yellow onion', 'jalapeno', 'corn', 'green bell pepper', 'tomato', 'avocado', 'banana', 'red chili pepper', 'oregano', 'egg']

produce.include? item
end
Expand Down
30 changes: 27 additions & 3 deletions app/controllers/groceries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
class GroceriesController < ApplicationController

before_action :authenticate_user!
before_action :find_grocery, only: [:update, :destroy]

def index
@grocery = Grocery.new
@groceries = Grocery.where("is_completed = false AND user_id = #{current_user.id}")
@groceries = Grocery.where("is_completed = false AND user_id = #{current_user.id} AND user_added = false")
@groceries_by_user = Grocery.where("is_completed = false AND user_id = #{current_user.id} AND user_added = true")
@groceries_completed = Grocery.where("is_completed = true AND user_id = #{current_user.id}")
end

def create
@grocery = Grocery.new grocery_params
@grocery.user = current_user
@grocery.save
redirect_to groceries_path
@grocery.user_added = true
if @grocery.save
redirect_to groceries_path
else
redirect_to groceries_path, alert: 'sorry: did you enter an item that is already in the list?'
end
end

def complete_grocery
Expand All @@ -25,8 +31,26 @@ def complete_grocery
redirect_to groceries_path
end

def update
if @grocery
@grocery.update(quantity: params[:quantity], unit: params[:unit])
redirect_to groceries_path
end
end

def destroy
if @grocery
@grocery.destroy
redirect_to groceries_path
end
end

private
def grocery_params
params.require(:grocery).permit(:name, :quantity, :unit)
end

def find_grocery
@grocery = Grocery.find_by(id: params[:id])
end
end
11 changes: 6 additions & 5 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def use_up_leftovers(non_exp_leftovers, result_rstrn_tags_non_exp)
#compare quantities
quantity_leftover = leftover.quantity
quantity_recipe = proper_recipe_quantity(ingredient, r, leftover)
# byebug
if above_50_percent(quantity_recipe, quantity_leftover)
stats_50[r] += 1
break if stats_50[r] >= 3
Expand Down Expand Up @@ -219,7 +218,7 @@ def proper_recipe_quantity(ingredient, recipe, leftover)
end

def convert_quantity(ingredient, link, recipe, leftover)
output = ''
output = link.quantity
case ingredient.name
when 'cucumber'
if link.unit == 'cup'
Expand All @@ -232,7 +231,7 @@ def convert_quantity(ingredient, link, recipe, leftover)
output = floatify(link.quantity) * 8
end
else
output = ''
output = link.quantity
end
output
end
Expand Down Expand Up @@ -316,6 +315,8 @@ def add_groceries_from_mealplan
# end

# byebug
current_user.groceries.where(:user_added => false).destroy_all

added_up.each do |stats|
if stats[:quantity] != nil
if stats[:unit] == 'cup' && stats[:quantity] < 0.2
Expand All @@ -329,8 +330,8 @@ def add_groceries_from_mealplan
end

stats[:quantity] = stringify_quantity(stats[:quantity])
current_user.groceries.destroy_all

# current_user.groceries.destroy_all
# byebug
Grocery.create(name: stats[:name], quantity: stats[:quantity], unit: stats[:unit], user: current_user, is_completed: false)
end
Expand Down
118 changes: 105 additions & 13 deletions app/views/groceries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<div>
<h1 class='page-title'>Grocery List</h1>

<p style="padding: 1em">We can only save edits to imported groceries after meal plan has been finalized. :)</p>

<%=form_with model: @grocery, local: true do |f|%>
<div class='add-leftover-form'>
<div class='leftover-form-element'>
Expand All @@ -22,20 +24,74 @@
</div>
<%end%>
<% @groceries.each do |g| %>
<div>
<%=form_with url: complete_grocery_path(g), local: true do |f|%>
<!-- <%= f.hidden_field :grocery_id %> -->
<% g_info = "#{g.name}"%>
<%if g.quantity != ''%>
<% g_info += "#{[': ', g.quantity, g.unit].compact.join(' ')}"%>
<%if @groceries%>
<ul class='users-groceries'>
<%@groceries.each do |g|%>

<div class='button-wrapper'>
<li class='grocery-info'>
<%=form_with url: complete_grocery_path(g), class: 'grocery-item-form', local: true do |f|%>
<!-- <%= f.hidden_field :grocery_id %> -->
<% g_info = "#{g.name}"%>
<%if g.quantity != ''%>
<% g_info += "#{[': ', g.quantity, g.unit].compact.join(' ')}"%>
<%end%>
<%=f.submit g_info, class: 'grocery-item'%>
<%end%>
<%=f.submit g_info, class: 'grocery-item'%>
<%end%>
<!-- <input type="checkbox" name='item' class='checkbox-on-preferences' value="<%=g.name%>" <%= g.is_completed ? "checked" : "" %> -->
<!-- <label for="item"><%=g.name%> <%=g.quantity%> <%=g.unit%></label> -->
<i class="fas fa-pen"></i><%=link_to 'X', delete_grocery_path(g), style: 'font-weight: 900; padding: .5em', method: :delete%>
</li>
<li class='update-leftover'>
<p style='font-weight: 700; text-decoration: underline; margin-bottom: 1em'><%=g.name%></p>
<%=form_with url: update_grocery_path(g), method: :patch, local: true do |f|%>
<!-- <p><%=f.label :quantity%></p> -->
<p><%=f.text_field :quantity, value: g.quantity, placeholder: "quantity", class: 'leftover-input'%></p>
<!-- <p><%=f.label :unit%></p> -->
<p><%=f.text_field :unit, value: g.unit, placeholder: "unit (optional)", class: 'leftover-input'%></p>
<%=f.submit class: 'button small'%>
<%end%>
</li>
</div>
<% end %>
<%end%>
</ul>
<%end%>
<%if @groceries_by_user%>
<h3 style='text-align: center; font-weight: 700; margin-top: 2em'>Other Stuff I Need:</h3>
<ul class='users-groceries'>
<%@groceries_by_user.each do |gbu|%>

<div class='button-wrapper'>
<li class='grocery-info'>
<%=form_with url: complete_grocery_path(gbu), class: 'grocery-item-form', local: true do |f|%>
<!-- <%= f.hidden_field :grocery_id %> -->
<% gbu_info = "#{gbu.name}"%>
<%if gbu.quantity != ''%>
<% gbu_info += "#{[': ', gbu.quantity, gbu.unit].compact.join(' ')}"%>
<%end%>
<%=f.submit gbu_info, class: 'grocery-item'%>
<%end%>
<!-- <%=gbu.name%>
<%if gbu.quantity.to_s != ''%>
: <%=gbu.quantity%> <%=gbu.unit%>
<%end%> -->
<i class="fas fa-pen"></i><%=link_to 'X', delete_grocery_path(gbu), style: 'font-weight: 900; padding: .5em', method: :delete%>
</li>
<li class='update-leftover'>
<p style='font-weight: 700; text-decoration: underline; margin-bottom: 1em'><%=gbu.name%></p>
<%=form_with url: update_grocery_path(gbu), method: :patch, local: true do |f|%>
<!-- <p><%=f.label :quantity%></p> -->
<p><%=f.text_field :quantity, value: gbu.quantity, placeholder: "quantity", class: 'leftover-input'%></p>
<!-- <p><%=f.label :unit%></p> -->
<p><%=f.text_field :unit, value: gbu.unit, placeholder: "unit (optional)", class: 'leftover-input'%></p>
<%=f.submit class: 'button small'%>
<%end%>
</li>
</div>
<%end%>
</ul>
<%end%>

<button type="button" id='completed-items-header' class='button'>Completed Items</button>
<div id="completed-items" id="completed-items">
Expand Down Expand Up @@ -67,7 +123,7 @@
}
}

document.querySelector('nav').querySelectorAll('a').forEach(function(e) {
document.querySelectorAll('a').forEach(function(e) {
e.style.color = 'black';
})

Expand Down Expand Up @@ -96,7 +152,43 @@
$(".completed-grocery").mouseleave(function(event) {
$(event.target).css("text-decoration", "line-through");
})

$('.button-wrapper').css("margin", "0");
$('.button-wrapper').find('a').hover(function(event) {
$(event.target).css("text-decoration", "none");
$(event.target).css("color", "red");
})
$('.button-wrapper').find('a').mouseleave(function(event) {
$(event.target).css("color", "black");
})

$('.fa-pen').hover(function(event) {
$(event.target).css("color", "red");
})
$('.fa-pen').mouseleave(function(event) {
$(event.target).css("color", "black");
})
})

document.querySelectorAll(".update-leftover").forEach(function(e) {
e.style.display = 'none';
});
document.querySelectorAll('.grocery-info').forEach(function(e) {
e.style.display = 'flex';
});
document.querySelectorAll(".fa-pen").forEach(function(b) {
b.onclick = function (event) {
event.target.closest('div').querySelector('.grocery-info').style.display = 'none';
event.target.closest('div').querySelector('.update-leftover').style.display = 'block';
// if (x.style.display === "none") {
// x.style.display = "block";
// y.style.display = 'none';
// } else {
// x.style.display = "none";
// y.style.display = 'block';
// }
}
});
</script>


Loading

0 comments on commit 00f5b0f

Please sign in to comment.