- Created /list/[id] page showing all items in a list - Added ItemCard component with completion toggle, priority badge, and due date display - Implemented InlineItemCreate component for quick task creation - Enhanced ToggleItemButton with icon variant for compact display - Enhanced DeleteItemButton with icon variant and improved event handling - Added overdue indicators for past due dates - Organized items into 'To Do' and 'Completed' sections - Implemented empty state for lists with no tasks Requirements: 1.3, 1.5, 2.3, 5.5
92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { deleteItem } from '@/lib/actions'
|
|
import { Trash2 } from 'lucide-react'
|
|
|
|
interface DeleteItemButtonProps {
|
|
itemId: string
|
|
listId?: string
|
|
variant?: 'full' | 'icon'
|
|
}
|
|
|
|
export default function DeleteItemButton({ itemId, listId, variant = 'full' }: DeleteItemButtonProps) {
|
|
const router = useRouter()
|
|
const [isDeleting, setIsDeleting] = useState(false)
|
|
const [showConfirm, setShowConfirm] = useState(false)
|
|
|
|
const handleDelete = async (e: React.MouseEvent) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setIsDeleting(true)
|
|
try {
|
|
await deleteItem(itemId)
|
|
if (listId) {
|
|
router.push(`/list/${listId}`)
|
|
} else {
|
|
router.refresh()
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting item:', error)
|
|
setIsDeleting(false)
|
|
setShowConfirm(false)
|
|
}
|
|
}
|
|
|
|
const handleShowConfirm = (e: React.MouseEvent) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setShowConfirm(true)
|
|
}
|
|
|
|
const handleCancel = (e: React.MouseEvent) => {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
setShowConfirm(false)
|
|
}
|
|
|
|
if (showConfirm) {
|
|
return (
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
onClick={handleDelete}
|
|
disabled={isDeleting}
|
|
className="px-3 py-2 bg-red-500 hover:bg-red-600 disabled:bg-red-300 text-white rounded-lg text-sm transition-colors"
|
|
>
|
|
{isDeleting ? 'Deleting...' : 'Confirm'}
|
|
</button>
|
|
<button
|
|
onClick={handleCancel}
|
|
disabled={isDeleting}
|
|
className="px-3 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 rounded-lg text-sm transition-colors"
|
|
>
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (variant === 'icon') {
|
|
return (
|
|
<button
|
|
onClick={handleShowConfirm}
|
|
className="text-gray-400 hover:text-red-600 transition-colors p-2 hover:bg-red-50 rounded-lg"
|
|
aria-label="Delete item"
|
|
>
|
|
<Trash2 size={18} />
|
|
</button>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<button
|
|
onClick={handleShowConfirm}
|
|
className="flex items-center gap-2 px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded-lg transition-colors"
|
|
>
|
|
<Trash2 size={18} />
|
|
<span>Delete</span>
|
|
</button>
|
|
)
|
|
}
|