Skip to content

Commit

Permalink
Merge pull request #4 from mykovasyl/adding-rating-to-recipes
Browse files Browse the repository at this point in the history
Adding rating to recipes
  • Loading branch information
mykovasyl committed Mar 22, 2023
2 parents d609f00 + 5845b80 commit 96f9699
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
exit
params[:rating]
params
q
recipe
recipe.update({ingredients: params[:ingredients]})
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def create
recipe.update({ingredients: params[:ingredients]})
render json: recipe, status: :accepted
end

def update
recipe_to_update = find_recipe
recipe_to_update.update!(update_recipe_params)
render json: recipe, status: :accepted
end

def destroy
recipe_to_delete = find_recipe
Expand All @@ -16,7 +22,11 @@ def destroy
private

def new_recipe_params
params.permit(:title, :readyIn, :image, :summary, :instructions, :ingredients, :sourceURL, :user_id, rating)
params.permit(:title, :readyIn, :image, :summary, :instructions, :ingredients, :sourceURL, :user_id, :rating)
end

def update_recipe_params
params.permit(:rating)
end

def find_recipe
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/recipe_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions client/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ function App() {
});
}

// update a recipe with rating
function handleUpdateRecipe(recipe) {
fetch(`/recipes/${recipe.id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ rating: recipe.rating }),
});
}

// delete a recipe from your recipe book
function handleDeleteRecipe(id) {
let newRecipes = currentUser.recipes.filter((recipe) => recipe.id !== id);
Expand All @@ -148,6 +157,7 @@ function App() {
avatar,
handleLogOut,
handleAddRecipe,
handleUpdateRecipe,
handleDeleteRecipe,
}}
>
Expand Down
30 changes: 27 additions & 3 deletions client/src/components/Recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { useContext, useState } from "react";
import { UserContext } from "./App";
import Button from "@mui/material/Button";
import FavoriteIcon from "@mui/icons-material/Favorite";
Expand All @@ -13,10 +13,15 @@ import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
// import { red } from "@mui/material/colors";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import { Rating } from "@mui/material";

function Recipe({ recipe }) {
const { currentUser, handleDeleteRecipe, handleAddRecipe } =
useContext(UserContext);
const {
currentUser,
handleDeleteRecipe,
handleAddRecipe,
handleUpdateRecipe,
} = useContext(UserContext);
const {
title,
readyIn,
Expand All @@ -25,23 +30,31 @@ function Recipe({ recipe }) {
ingredients,
instructions,
sourceURL,
rating = 0,
user_id = null,
id = null,
} = recipe;
const [ratingValue, setRatingValue] = useState(rating);

const mappedIngredients = ingredients.map((ingredient) => (
<li key={ingredient}>{ingredient}</li>
));

function addRecipe() {
recipe.user_id = currentUser.id;
recipe.rating = ratingValue;
handleAddRecipe(recipe);
}

function deleteRecipe() {
handleDeleteRecipe(id);
}

function updateRating(newRating) {
recipe.rating = newRating;
handleUpdateRecipe(recipe);
}

// start of card code -- DO NOT EDIT BELOW THIS LINE --

const ExpandMore = styled((props) => {
Expand All @@ -68,6 +81,17 @@ function Recipe({ recipe }) {
return (
<Card sx={{ maxWidth: 500 }}>
<CardHeader title={title} subheader={readyInText} />
{user_id ? (
<Rating
name='simple-controlled'
value={ratingValue}
precision={0.5}
onChange={(event, newRating) => {
setRatingValue(newRating);
updateRating(newRating);
}}
/>
) : null}
<CardMedia component='img' height='194' image={image} alt='food' />
<CardContent>
<Typography variant='body2' color='text.secondary'>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/RecipeBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function RecipeBook() {
color: theme.palette.text.secondary,
}));
const recipesDisplayed = currentUser.recipes.map((recipe) => (
<Grid item xs={6} md={4}>
<Grid item xs={6} md={4} key={recipe.id}>
<Item>
<Recipe key={recipe.summary} recipe={recipe} />
</Item>
Expand Down

0 comments on commit 96f9699

Please sign in to comment.