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 | 23x 23x 23x 23x 23x 23x 10x 10x 10x 10x 8x 2x 2x 10x 10x 23x 23x 1x 1x 1x 1x 1x 1x 23x 23x | import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { toast } from 'sonner'
import { api, type CollectionDetail, type Recipe } from '../api/client'
export function useCollectionData(collectionId: number) {
const navigate = useNavigate()
const [collection, setCollection] = useState<CollectionDetail | null>(null)
const [loading, setLoading] = useState(true)
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
const [deleting, setDeleting] = useState(false)
useEffect(() => {
Iif (!collectionId) return
const load = async () => {
try {
const data = await api.collections.get(collectionId)
setCollection(data)
} catch (error) {
console.error('Failed to load collection:', error)
toast.error('Failed to load collection')
} finally {
setLoading(false)
}
}
load()
}, [collectionId])
const handleRemoveRecipe = async (recipe: Recipe) => {
if (!collection) return
try {
await api.collections.removeRecipe(collectionId, recipe.id)
setCollection({
...collection,
recipes: collection.recipes.filter((item) => item.recipe.id !== recipe.id),
})
toast.success('Removed from collection')
} catch (error) {
console.error('Failed to remove recipe:', error)
toast.error('Failed to remove recipe')
}
}
const handleDeleteCollection = async () => {
setDeleting(true)
try {
await api.collections.delete(collectionId)
toast.success('Collection deleted')
navigate('/collections')
} catch (error) {
console.error('Failed to delete collection:', error)
toast.error('Failed to delete collection')
} finally {
setDeleting(false)
}
}
const handleRecipeClick = async (recipeId: number) => {
try {
await api.history.record(recipeId)
} catch (error) {
console.error('Failed to record history:', error)
}
navigate(`/recipe/${recipeId}`)
}
return {
collection,
loading,
showDeleteConfirm,
setShowDeleteConfirm,
deleting,
handleRemoveRecipe,
handleDeleteCollection,
handleRecipeClick,
}
}
|