All files / src/screens Favorites.tsx

0% Statements 0/22
0% Branches 0/4
0% Functions 0/7
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                                                                                                                                                                     
import { useState, useEffect } from 'react'
import { ArrowLeft, Heart } from 'lucide-react'
import { toast } from 'sonner'
import { api, type Recipe, type Favorite } from '../api/client'
import RecipeCard from '../components/RecipeCard'
import { RecipeGridSkeleton } from '../components/Skeletons'
 
interface FavoritesProps {
  onBack: () => void
  onRecipeClick: (recipeId: number) => void
}
 
export default function Favorites({ onBack, onRecipeClick }: FavoritesProps) {
  const [favorites, setFavorites] = useState<Favorite[]>([])
  const [loading, setLoading] = useState(true)
 
  useEffect(() => {
    loadFavorites()
  }, [])
 
  const loadFavorites = async () => {
    try {
      const data = await api.favorites.list()
      setFavorites(data)
    } catch (error) {
      console.error('Failed to load favorites:', error)
      toast.error('Failed to load favorites')
    } finally {
      setLoading(false)
    }
  }
 
  const handleRemoveFavorite = async (recipe: Recipe) => {
    try {
      await api.favorites.remove(recipe.id)
      setFavorites(favorites.filter((f) => f.recipe.id !== recipe.id))
      toast.success('Removed from favorites')
    } catch (error) {
      console.error('Failed to remove favorite:', error)
      toast.error('Failed to remove from favorites')
    }
  }
 
  return (
    <div className="min-h-screen bg-background">
      {/* Header */}
      <header className="flex items-center gap-4 border-b border-border px-4 py-3">
        <button
          onClick={onBack}
          className="rounded-lg p-2 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
        >
          <ArrowLeft className="h-5 w-5" />
        </button>
        <h1 className="text-xl font-medium text-foreground">Favorites</h1>
      </header>
 
      {/* Content */}
      <main className="px-4 py-6">
        <div className="mx-auto max-w-4xl">
          {loading ? (
            <RecipeGridSkeleton count={8} />
          ) : favorites.length > 0 ? (
            <div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4">
              {favorites.map((favorite) => (
                <RecipeCard
                  key={favorite.recipe.id}
                  recipe={favorite.recipe}
                  isFavorite
                  onFavoriteToggle={handleRemoveFavorite}
                  onClick={() => onRecipeClick(favorite.recipe.id)}
                />
              ))}
            </div>
          ) : (
            /* Empty state */
            <div className="flex flex-col items-center justify-center rounded-xl border-2 border-dashed border-border py-12">
              <div className="mb-4 rounded-full bg-muted p-4">
                <Heart className="h-8 w-8 text-muted-foreground" />
              </div>
              <h3 className="mb-2 text-lg font-medium text-foreground">
                No favorites yet
              </h3>
              <p className="mb-4 text-center text-muted-foreground">
                Browse recipes to add some!
              </p>
              <button
                onClick={onBack}
                className="rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
              >
                Discover Recipes
              </button>
            </div>
          )}
        </div>
      </main>
    </div>
  )
}
 
← Back to Dashboard