All files / src/screens Collections.tsx

0% Statements 0/54
0% Branches 0/26
0% Functions 0/12
0% Lines 0/53

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState, useEffect } from 'react'
import { ArrowLeft, FolderPlus, Plus, X } from 'lucide-react'
import { toast } from 'sonner'
import { api, type Collection } from '../api/client'
import { cn } from '../lib/utils'
import { CollectionGridSkeleton } from '../components/Skeletons'
 
interface CollectionsProps {
  onBack: () => void
  onCollectionClick: (collectionId: number) => void
  pendingRecipeId?: number | null
}
 
export default function Collections({
  onBack,
  onCollectionClick,
  pendingRecipeId,
}: CollectionsProps) {
  const [collections, setCollections] = useState<Collection[]>([])
  const [loading, setLoading] = useState(true)
  const [showCreateForm, setShowCreateForm] = useState(false)
  const [newCollectionName, setNewCollectionName] = useState('')
  const [creating, setCreating] = useState(false)
 
  useEffect(() => {
    loadCollections()
  }, [])
 
  const loadCollections = async () => {
    try {
      const data = await api.collections.list()
      setCollections(data)
    } catch (error) {
      console.error('Failed to load collections:', error)
      toast.error('Failed to load collections')
    } finally {
      setLoading(false)
    }
  }
 
  const handleCreateCollection = async (e: React.FormEvent) => {
    e.preventDefault()
    if (!newCollectionName.trim()) return
 
    setCreating(true)
    try {
      const collection = await api.collections.create({
        name: newCollectionName.trim(),
      })
      setCollections([collection, ...collections])
      setNewCollectionName('')
      setShowCreateForm(false)
      toast.success('Collection created')
 
      // If there's a pending recipe to add, add it and navigate to collection
      if (pendingRecipeId) {
        try {
          await api.collections.addRecipe(collection.id, pendingRecipeId)
          toast.success('Recipe added to collection')
        } catch (error) {
          console.error('Failed to add recipe to collection:', error)
        }
        onCollectionClick(collection.id)
      }
    } catch (error) {
      console.error('Failed to create collection:', error)
      toast.error('Failed to create collection')
    } finally {
      setCreating(false)
    }
  }
 
  const handleCollectionClick = async (collectionId: number) => {
    // If there's a pending recipe, add it first
    if (pendingRecipeId) {
      try {
        await api.collections.addRecipe(collectionId, pendingRecipeId)
        toast.success('Recipe added to collection')
      } catch (error: unknown) {
        // Ignore "already in collection" error
        if (error instanceof Error && !error.message.includes('already')) {
          console.error('Failed to add recipe to collection:', error)
          toast.error('Failed to add recipe to collection')
        } else {
          toast.info('Recipe is already in this collection')
        }
      }
    }
    onCollectionClick(collectionId)
  }
 
  return (
    <div className="min-h-screen bg-background">
      {/* Header */}
      <header className="flex items-center justify-between border-b border-border px-4 py-3">
        <div className="flex items-center gap-4">
          <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">Collections</h1>
        </div>
        <button
          onClick={() => setShowCreateForm(true)}
          className="flex items-center gap-2 rounded-lg bg-primary px-3 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
        >
          <Plus className="h-4 w-4" />
          New Collection
        </button>
      </header>
 
      {/* Content */}
      <main className="px-4 py-6">
        <div className="mx-auto max-w-4xl">
          {/* Create form */}
          {showCreateForm && (
            <form
              onSubmit={handleCreateCollection}
              className="mb-6 rounded-lg border border-border bg-card p-4"
            >
              <div className="mb-4 flex items-center justify-between">
                <h2 className="font-medium text-foreground">New Collection</h2>
                <button
                  type="button"
                  onClick={() => {
                    setShowCreateForm(false)
                    setNewCollectionName('')
                  }}
                  className="rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground"
                >
                  <X className="h-5 w-5" />
                </button>
              </div>
              <input
                type="text"
                value={newCollectionName}
                onChange={(e) => setNewCollectionName(e.target.value)}
                placeholder="Collection name"
                className="mb-3 w-full rounded-lg border border-border bg-input-background px-3 py-2 text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
                autoFocus
              />
              <div className="flex justify-end gap-2">
                <button
                  type="button"
                  onClick={() => {
                    setShowCreateForm(false)
                    setNewCollectionName('')
                  }}
                  className="rounded-lg px-4 py-2 text-sm text-muted-foreground hover:bg-muted hover:text-foreground"
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  disabled={!newCollectionName.trim() || creating}
                  className="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90 disabled:opacity-50"
                >
                  {creating ? 'Creating...' : 'Create'}
                </button>
              </div>
            </form>
          )}
 
          {/* Pending recipe notice */}
          {pendingRecipeId && (
            <div className="mb-4 rounded-lg border border-primary/30 bg-primary/10 p-3 text-sm text-foreground">
              Select a collection or create a new one to add the recipe
            </div>
          )}
 
          {loading ? (
            <CollectionGridSkeleton count={8} />
          ) : collections.length > 0 ? (
            <div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4">
              {collections.map((collection) => (
                <button
                  key={collection.id}
                  onClick={() => handleCollectionClick(collection.id)}
                  className={cn(
                    'group relative overflow-hidden rounded-lg bg-card p-4 text-left shadow-sm transition-all hover:shadow-md',
                    pendingRecipeId && 'ring-2 ring-primary/20 hover:ring-primary/40'
                  )}
                >
                  <div className="mb-8 rounded-lg bg-muted p-3">
                    <FolderPlus className="h-6 w-6 text-primary" />
                  </div>
                  <h3 className="mb-1 font-medium text-card-foreground line-clamp-1">
                    {collection.name}
                  </h3>
                  <p className="text-sm text-muted-foreground">
                    {collection.recipe_count} recipe
                    {collection.recipe_count !== 1 ? 's' : ''}
                  </p>
                </button>
              ))}
            </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">
                <FolderPlus className="h-8 w-8 text-muted-foreground" />
              </div>
              <h3 className="mb-2 text-lg font-medium text-foreground">
                No collections yet
              </h3>
              <p className="mb-4 text-center text-muted-foreground">
                Create one to organize your recipes!
              </p>
              <button
                onClick={() => setShowCreateForm(true)}
                className="rounded-lg bg-primary px-4 py-2 text-primary-foreground transition-colors hover:bg-primary/90"
              >
                Create Collection
              </button>
            </div>
          )}
        </div>
      </main>
    </div>
  )
}
 
← Back to Dashboard