Skip to content

Commit

Permalink
fix rating update issue, relocate recipe state to App and persist ran…
Browse files Browse the repository at this point in the history
…dom recipe after like
  • Loading branch information
mykovasyl committed Mar 29, 2023
1 parent 44b3b43 commit 0c67d3f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
def update
recipe_to_update = find_recipe
recipe_to_update.update!(update_recipe_params)
render json: recipe, status: :accepted
render json: recipe_to_update, status: :accepted
end

def destroy
Expand Down
12 changes: 12 additions & 0 deletions client/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export const UserContext = createContext();
function App() {
const [currentUser, setCurrentUser] = useState({});
const [errors, setErrors] = useState([]);
const [recipe, setRecipe] = useState({
title: "",
readyIn: "",
image: "",
summary: "",
instructions: "",
ingredients: [],
sourceURL: "",
user_id: null,
});

const navigate = useNavigate();

Expand Down Expand Up @@ -152,6 +162,8 @@ function App() {
<UserContext.Provider
value={{
currentUser,
recipe,
setRecipe,
setCurrentUser,
handleLogOut,
handleAddRecipe,
Expand Down
16 changes: 2 additions & 14 deletions client/src/components/RandomRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@ import { UserContext } from "./App";
import Recipe from "./Recipe";

function RandomRecipe() {
const [recipe, setRecipe] = useState({
title: "",
readyIn: "",
image: "",
summary: "",
instructions: "",
ingredients: [],
sourceURL: "",
user_id: null,
});
const { handleAddRecipe } = useContext(UserContext);
const [isLoaded, setIsLoaded] = useState(false);
const { handleAddRecipe, recipe, setRecipe } = useContext(UserContext);

function handleClick() {
fetch(
Expand All @@ -38,7 +27,6 @@ function RandomRecipe() {
sourceURL: data.recipes[0].spoonacularSourceUrl,
user_id: null,
});
setIsLoaded(true);
});
}

Expand All @@ -47,7 +35,7 @@ function RandomRecipe() {
<Button type='button' variant='contained' onClick={handleClick}>
Get Random Recipe
</Button>
{isLoaded ? (
{recipe.title ? (
<Recipe recipe={recipe} handleAddRecipe={handleAddRecipe} />
) : null}
</div>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Recipe({ recipe }) {
return (
<Card sx={{ maxWidth: 500 }}>
<CardHeader title={title} subheader={readyInText} />
{user_id ? (
{user_id && recipe.id ? (
<Rating
name='simple-controlled'
value={ratingValue}
Expand Down

0 comments on commit 0c67d3f

Please sign in to comment.