|
3 | 3 | import { Reading } from "@/lib/dummyData"; |
4 | 4 | import {} from "react"; |
5 | 5 | import ShareLinkButton from "@/components/ShareLinkButton"; |
| 6 | +import { DocumentService } from "@/services/documentService"; |
| 7 | +import { Trash } from "lucide-react"; |
6 | 8 | import { useRouter } from "next/navigation"; |
7 | 9 |
|
8 | 10 | interface ReadingItemProps { |
9 | 11 | reading: Reading; |
10 | 12 | viewMode: "gallery" | "list"; |
| 13 | + onDelete?: (id: string) => void; |
11 | 14 | } |
12 | 15 |
|
13 | | -export default function ReadingItem({ reading, viewMode }: ReadingItemProps) { |
| 16 | +export default function ReadingItem({ reading, viewMode, onDelete }: ReadingItemProps) { |
14 | 17 | // Minimal actions (share handled by icon button) |
15 | 18 | const router = useRouter(); |
16 | 19 |
|
| 20 | + const handleDelete = async (e: React.MouseEvent) => { |
| 21 | + e.stopPropagation(); |
| 22 | + |
| 23 | + console.log("Attempting to delete:", reading.id); |
| 24 | + |
| 25 | + const confirmed = window.confirm( |
| 26 | + `Delete "${reading.title}"? This cannot be undone.` |
| 27 | + ); |
| 28 | + if (!confirmed) return; |
| 29 | + |
| 30 | + try { |
| 31 | + await DocumentService.deleteDocument(reading.id); |
| 32 | + onDelete?.(reading.id); |
| 33 | + router.refresh(); |
| 34 | + } catch (err) { |
| 35 | + const error = err as Error; |
| 36 | + // console.error("Delete error →", error); |
| 37 | + alert("Failed to delete document: " + (error.message ?? "")); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
17 | 41 | const handleNavigate = () => { |
18 | 42 | if (reading.id) { |
19 | 43 | router.push(`/documents/${reading.id}`); |
@@ -80,7 +104,15 @@ export default function ReadingItem({ reading, viewMode }: ReadingItemProps) { |
80 | 104 | )} |
81 | 105 | </div> |
82 | 106 |
|
83 | | - <div className="ml-4" onClick={(e) => e.stopPropagation()}> |
| 107 | + <div className="ml-4 flex items-center gap-2" onClick={(e) => e.stopPropagation()}> |
| 108 | + <button |
| 109 | + onClick={handleDelete} |
| 110 | + className="p-1 text-red-500 hover:text-red-700" |
| 111 | + aria-label="Delete document" |
| 112 | + > |
| 113 | + <Trash className="w-5 h-5" /> |
| 114 | + </button> |
| 115 | + |
84 | 116 | <ShareLinkButton documentId={reading.id} variant="icon" /> |
85 | 117 | </div> |
86 | 118 | </div> |
@@ -118,7 +150,23 @@ export default function ReadingItem({ reading, viewMode }: ReadingItemProps) { |
118 | 150 | <h3 className="text-lg font-semibold text-gray-900 line-clamp-2"> |
119 | 151 | {reading.title} |
120 | 152 | </h3> |
121 | | - <div className="ml-2" onClick={(e) => e.stopPropagation()}> |
| 153 | + <div className="ml-2 flex items-center gap-2" onClick={(e) => e.stopPropagation()}> |
| 154 | + <button |
| 155 | + onClick={handleDelete} |
| 156 | + aria-label="Delete document" |
| 157 | + className=" |
| 158 | + rounded-md p-2 |
| 159 | + bg-white |
| 160 | + shadow-[2px_2px_6px_#d1d5db,-2px_-2px_6px_#ffffff] |
| 161 | + hover:shadow-[1px_1px_3px_#d1d5db,-1px_-1px_3px_#ffffff] |
| 162 | + transition-shadow |
| 163 | + text-red-500 hover:text-red-600 |
| 164 | + flex items-center justify-center |
| 165 | + " |
| 166 | + > |
| 167 | + <Trash className="w-4 h-4" strokeWidth={2} /> |
| 168 | + </button> |
| 169 | + |
122 | 170 | <ShareLinkButton documentId={reading.id} variant="icon" /> |
123 | 171 | </div> |
124 | 172 | </div> |
|
0 commit comments