import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Recipe, recipesAPI } from '../services/api'; import { useAuth } from '../context/AuthContext'; import './RecipeModal.css'; interface RecipeModalProps { recipe: Recipe; onClose: () => void; onAddToSelection: (recipeId: string) => void; onRecipeDeleted?: (recipeId: string) => void; isSelected: boolean; selectedQuantity: number; } const RecipeModal: React.FC = ({ recipe, onClose, onAddToSelection, onRecipeDeleted, isSelected, selectedQuantity, }) => { const { user } = useAuth(); const navigate = useNavigate(); const [isDeleting, setIsDeleting] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }; const handleDeleteClick = () => { setShowDeleteConfirm(true); }; const handleDeleteConfirm = async () => { setIsDeleting(true); try { await recipesAPI.delete(recipe._id); onRecipeDeleted?.(recipe._id); onClose(); } catch (error) { console.error('Error deleting recipe:', error); alert('Failed to delete recipe. Please try again.'); } finally { setIsDeleting(false); setShowDeleteConfirm(false); } }; const handleDeleteCancel = () => { setShowDeleteConfirm(false); }; const handleEditClick = () => { // Navigate to edit page with recipe data navigate(`/edit-recipe/${recipe._id}`, { state: { recipe } }); onClose(); }; // Any logged-in user can edit or delete recipes const canEdit = !!user; const canDelete = !!user; const getDifficultyColor = (difficulty: string) => { switch (difficulty) { case 'easy': return '#4CAF50'; case 'medium': return '#FF9800'; case 'hard': return '#F44336'; default: return '#757575'; } }; const getCategoryColor = (category: string) => { switch (category) { case 'breakfast': return '#FFE082'; case 'lunch': return '#81C784'; case 'dinner': return '#64B5F6'; case 'dessert': return '#F48FB1'; case 'snack': return '#FFB74D'; case 'appetizer': return '#A1C181'; default: return '#E0E0E0'; } }; return (
{recipe.title} { (e.target as HTMLImageElement).src = '/placeholder-recipe.jpg'; }} />
{recipe.difficulty} {recipe.category}

{recipe.title}

{recipe.description}

Prep Time {recipe.prepTime} min
Cook Time {recipe.cookTime} min
Total Time {recipe.prepTime + recipe.cookTime} min
Servings {recipe.servings}

Ingredients

    {recipe.ingredients.map((ingredient, index) => (
  • {ingredient.amount} {ingredient.unit} {ingredient.name}
  • ))}

Instructions

    {recipe.instructions.map((instruction) => (
  1. {instruction.step}
    {instruction.description}
  2. ))}
{canEdit && ( )} {canDelete && ( )}
{/* Delete Confirmation Dialog */} {showDeleteConfirm && (

Delete Recipe

Are you sure you want to delete "{recipe.title}"?

This action cannot be undone. The recipe will be removed from all users' menus.

)}
); }; export default RecipeModal;