Skip to content

Commit

Permalink
create rating attribute for recipes and tie to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mykovasyl committed Mar 22, 2023
1 parent d609f00 commit d1f50ad
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def create
recipe.update({ingredients: params[:ingredients]})
render json: recipe, status: :accepted
end

def update
recipe = Recipe.update!(update_recipe_params)
end

def destroy
recipe_to_delete = find_recipe
Expand All @@ -16,7 +20,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
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(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,16 @@ 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 [ratingValue, setRatingValue] = useState(0);
const {
title,
readyIn,
Expand All @@ -25,6 +31,7 @@ function Recipe({ recipe }) {
ingredients,
instructions,
sourceURL,
rating = 0,
user_id = null,
id = null,
} = recipe;
Expand All @@ -35,13 +42,19 @@ function Recipe({ recipe }) {

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={rating}
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 d1f50ad

Please sign in to comment.