From 5845b80640712295790d519d96114f9065ee6ec7 Mon Sep 17 00:00:00 2001 From: Mykola Vasyl'yev Date: Wed, 22 Mar 2023 18:14:20 -0400 Subject: [PATCH] set up rating update function and update method on backend --- .byebug_history | 3 +++ app/controllers/recipes_controller.rb | 4 +++- app/serializers/recipe_serializer.rb | 2 +- client/src/components/App.js | 2 +- client/src/components/Recipe.js | 4 ++-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.byebug_history b/.byebug_history index b36912e..b72daa5 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,3 +1,6 @@ +exit +params[:rating] +params q recipe recipe.update({ingredients: params[:ingredients]}) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 41fc504..61e0036 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -8,7 +8,9 @@ def create end def update - recipe = Recipe.update!(update_recipe_params) + recipe_to_update = find_recipe + recipe_to_update.update!(update_recipe_params) + render json: recipe, status: :accepted end def destroy diff --git a/app/serializers/recipe_serializer.rb b/app/serializers/recipe_serializer.rb index e0e4a60..91e96bc 100644 --- a/app/serializers/recipe_serializer.rb +++ b/app/serializers/recipe_serializer.rb @@ -1,3 +1,3 @@ class RecipeSerializer < ActiveModel::Serializer - attributes :id, :title, :readyIn, :image, :summary, :instructions, :ingredients, :sourceURL, :user_id + attributes :id, :title, :readyIn, :image, :summary, :instructions, :ingredients, :sourceURL, :user_id, :rating end diff --git a/client/src/components/App.js b/client/src/components/App.js index 66127ad..3302996 100644 --- a/client/src/components/App.js +++ b/client/src/components/App.js @@ -128,7 +128,7 @@ function App() { fetch(`/recipes/${recipe.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, - body: JSON.stringify(recipe.rating), + body: JSON.stringify({ rating: recipe.rating }), }); } diff --git a/client/src/components/Recipe.js b/client/src/components/Recipe.js index 511de29..dbff408 100644 --- a/client/src/components/Recipe.js +++ b/client/src/components/Recipe.js @@ -22,7 +22,6 @@ function Recipe({ recipe }) { handleAddRecipe, handleUpdateRecipe, } = useContext(UserContext); - const [ratingValue, setRatingValue] = useState(0); const { title, readyIn, @@ -35,6 +34,7 @@ function Recipe({ recipe }) { user_id = null, id = null, } = recipe; + const [ratingValue, setRatingValue] = useState(rating); const mappedIngredients = ingredients.map((ingredient) => (
  • {ingredient}
  • @@ -84,7 +84,7 @@ function Recipe({ recipe }) { {user_id ? ( { setRatingValue(newRating);