diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index cbe6912..1cb00fd 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -34,7 +34,14 @@ def destroy private def recipe_params - params.require(:recipe).permit(:name, :description, ingredients_attributes: [:id, :food_id, :measurement, :_destroy]) + rp = params.require(:recipe).permit(:name, :description, ingredients_attributes: [:id, :food_id, :food_name, :measurement, :_destroy]) + rp[:ingredients_attributes].each do |ingredient| + food_name = ingredient.delete(:food_name) + if ingredient[:food_id].blank? || ingredient[:food_id] == 0 + ingredient[:food_id] = Food.find_or_create_by!(name: food_name).id + end + end + rp end end diff --git a/app/javascript/components/recipes/RecipeEdit.tsx b/app/javascript/components/recipes/RecipeEdit.tsx index 229ce04..2213e21 100644 --- a/app/javascript/components/recipes/RecipeEdit.tsx +++ b/app/javascript/components/recipes/RecipeEdit.tsx @@ -73,7 +73,7 @@ const EditRecipeForm: React.FC = () => { return (
-

Edit Recipe

+

Edit Recipe

( + + + +); + const RecipeForm: React.FC = ({ formData, setFormData, onSubmit, submitButtonText }) => { const { items: foods } = useAppSelector((state) => state.foods); @@ -47,11 +65,12 @@ const RecipeForm: React.FC = ({ formData, setFormData, onSubmit }); }; - const handleIngredientChange = (index: number, field: 'food_id' | 'measurement', value: string | number) => { + const handleIngredientChange = (index: number, field: IngredientField, value: string | number) => { const newIngredients = [...formData.ingredients_attributes]; newIngredients[index] = { id: newIngredients[index]?.id || undefined, food_id: newIngredients[index]?.food_id || 0, + food_name: newIngredients[index]?.food_name || '', measurement: newIngredients[index]?.measurement || '', [field]: value }; @@ -61,6 +80,18 @@ const RecipeForm: React.FC = ({ formData, setFormData, onSubmit }); }; + const getFoodSuggestions = (searchTerm: string) => { + const normalizedTerm = searchTerm?.toLowerCase().trim() || ''; + const hasExactMatch = foods.some(f => f.name.toLowerCase() === normalizedTerm); + if (hasExactMatch) return []; + + return foods + .filter(food => food.name.toLowerCase().includes(normalizedTerm)) + .map((food) => ( +