navigation after adding recipes

This commit is contained in:
fred 2025-07-08 11:13:01 -07:00
parent cd234531b9
commit e74f247415
3 changed files with 21 additions and 10 deletions

View file

@ -1,19 +1,33 @@
import React, { useState } from 'react';
import { addRecipe } from "../services/backend.js"; import { addRecipe } from "../services/backend.js";
import { useNavigate } from "react-router-dom";
function AddRecipe() { function AddRecipe() {
const [newRecipeId, setNewRecipeId] = useState<number | null>(null);
const navigate = useNavigate();
const addRecipeForm = async (event: React.FormEvent) => { const addRecipeForm = async (event: React.FormEvent) => {
event.preventDefault(); event.preventDefault();
const nameElement = document.getElementById('ar-name') as HTMLInputElement | null; const nameElement = document.getElementById('ar-name') as HTMLInputElement | null;
const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null; const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null;
if (nameElement && cuisineElement) { if (nameElement && cuisineElement) {
const recipeData = {
const name = nameElement.value; name: nameElement.value,
const cuisine = cuisineElement.value; cuisine: cuisineElement.value
await addRecipe(name, cuisine);
} }
const data = await addRecipe(recipeData);
setNewRecipeId(data.id);
} }
};
React.useEffect(() => {
if (newRecipeId !== null) {
navigate(`/recipe/${newRecipeId}`);
}
}, [newRecipeId, navigate]);
return ( return (
<div className="add-recipe-outer"> <div className="add-recipe-outer">
<form onSubmit={addRecipeForm} className="add-recipe-form"> <form onSubmit={addRecipeForm} className="add-recipe-form">

View file

@ -13,8 +13,6 @@ function Cookbook() {
const loadRecipes = async () => { const loadRecipes = async () => {
try { try {
const recipes = await getRecipes(); const recipes = await getRecipes();
console.log(recipes)
console.log("here")
setRecipes(recipes); setRecipes(recipes);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@ -28,10 +26,9 @@ function Cookbook() {
} }
}, [shouldFetchRecipes]); }, [shouldFetchRecipes]);
const handleDelete = async (id) => { const handleDelete = async (id: number | void) => {
try { try {
await deleteRecipe(id); await deleteRecipe(id);
// Trigger a re-fetch by setting the state variable to true
setShouldFetchRecipes(true); setShouldFetchRecipes(true);
} catch (error) { } catch (error) {
console.error("Error deleting recipe:", error); console.error("Error deleting recipe:", error);

View file

@ -10,11 +10,11 @@ export const getRecipeById = async (id) => {
return data; return data;
}; };
export const addRecipe = async (name, cuisine) => { export const addRecipe = async (recipeData) => {
const response = await fetch("http://localhost:6063/add-recipe", { const response = await fetch("http://localhost:6063/add-recipe", {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, cuisine }) body: JSON.stringify(recipeData)
}); });
const data = await response.json(); const data = await response.json();
console.log(data) console.log(data)