From a1363ee4b50c6c52be5c6859eb830f1e13f3c923 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Fri, 7 Feb 2025 02:56:17 -0600 Subject: [PATCH] Fixed --- src/components/RegistryList.tsx | 144 ++++++++++++++++++++--------- src/components/RegistryManager.tsx | 55 ++++++++--- src/lib/types.ts | 1 + src/pages/api/registry/[id].ts | 41 +++++--- src/pages/api/registry/index.ts | 2 +- 5 files changed, 169 insertions(+), 74 deletions(-) diff --git a/src/components/RegistryList.tsx b/src/components/RegistryList.tsx index 05a12bb..4da6204 100644 --- a/src/components/RegistryList.tsx +++ b/src/components/RegistryList.tsx @@ -6,29 +6,31 @@ const RegistryList = () => { const [claimedItems, setClaimedItems] = useState>(new Set()); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const [claimantName, setClaimantName] = useState(""); + const [showClaimants, setShowClaimants] = useState>(new Set()); useEffect(() => { - const fetchRegistryItems = async () => { - setLoading(true); - setError(null); - try { - const response = await fetch("/api/registry"); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - const data = await response.json(); - setRegistryItems(data); - } catch (e: any) { - setError(e.message); - console.error("Failed to fetch registry items:", e); - } finally { - setLoading(false); - } - }; - fetchRegistryItems(); }, []); + const fetchRegistryItems = async () => { + setLoading(true); + setError(null); + try { + const response = await fetch("/api/registry"); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + setRegistryItems(data); + } catch (e: any) { + setError(e.message); + console.error("Failed to fetch registry items:", e); + } finally { + setLoading(false); + } + }; + const handleCheckboxChange = (itemId: string) => { const newClaimedItems = new Set(claimedItems); if (newClaimedItems.has(itemId)) { @@ -39,31 +41,42 @@ const RegistryList = () => { setClaimedItems(newClaimedItems); }; - const handleSubmit = async () => { - // Prepare updates for claimed items - const updates = registryItems.filter((item) => - claimedItems.has(item.id) - ); + const toggleClaimantVisibility = (itemId: string) => { + const newShowClaimants = new Set(showClaimants); + if (newShowClaimants.has(itemId)) { + newShowClaimants.delete(itemId); + } else { + newShowClaimants.add(itemId); + } + setShowClaimants(newShowClaimants); + }; + + const handleSubmit = async () => { + if (!claimantName.trim()) { + setError("Please enter your name before claiming items"); + return; + } + + const updates = registryItems.filter((item) => claimedItems.has(item.id)); - // Optimistically update the UI const updatedRegistryItems = registryItems.map((item) => { if (claimedItems.has(item.id)) { - return { ...item, taken: true }; + return { ...item, taken: true, claimedBy: claimantName }; } return item; }); setRegistryItems(updatedRegistryItems); - setClaimedItems(new Set()); // Clear claimed items after submission + setClaimedItems(new Set()); + setClaimantName(""); try { - // Send updates to the server for (const item of updates) { const response = await fetch(`/api/registry/${item.id}`, { method: "PUT", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ taken: true }), + body: JSON.stringify({ taken: true, claimedBy: claimantName }), }); if (!response.ok) { @@ -85,9 +98,7 @@ const RegistryList = () => { } if (error) { - return ( -
Error: {error}
- ); + return
Error: {error}
; } return ( @@ -99,17 +110,37 @@ const RegistryList = () => { Item Link + Status Claim {registryItems.map((item) => ( - + - {item.name} - {item.taken && ( - Taken - )} +
+
+ {item.name} + {item.taken && ( +
+ Taken + +
+ )} +
+ {item.taken && + item.claimedBy && + showClaimants.has(item.id) && ( +
+ Claimed by: {item.claimedBy} +
+ )} +
{item.link && ( @@ -123,28 +154,51 @@ const RegistryList = () => { )} + {item.taken ? "Claimed" : "Available"} - {!item.taken && ( + {!item.taken ? ( handleCheckboxChange(item.id)} /> - )} + ) : } ))} - + {claimedItems.size > 0 && ( +
+
+ + setClaimantName(e.target.value)} + placeholder="Enter your name" + required + /> +
+ +
+ )} ); }; diff --git a/src/components/RegistryManager.tsx b/src/components/RegistryManager.tsx index 7af6e8d..f7511e5 100644 --- a/src/components/RegistryManager.tsx +++ b/src/components/RegistryManager.tsx @@ -6,6 +6,7 @@ const RegistryManager = () => { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newItem, setNewItem] = useState({ name: "", link: "" }); + const [showClaimants, setShowClaimants] = useState>(new Set()); useEffect(() => { fetchRegistryItems(); @@ -46,7 +47,6 @@ const RegistryManager = () => { throw new Error(`Failed to add item: ${response.statusText}`); } - // Clear form and refresh list setNewItem({ name: "", link: "" }); await fetchRegistryItems(); } catch (error: any) { @@ -67,7 +67,6 @@ const RegistryManager = () => { throw new Error(`Failed to delete item: ${response.statusText}`); } - // Refresh the list after deletion await fetchRegistryItems(); } catch (error: any) { setError(error.message); @@ -75,6 +74,16 @@ const RegistryManager = () => { } }; + const toggleClaimantVisibility = (itemId: string) => { + const newShowClaimants = new Set(showClaimants); + if (newShowClaimants.has(itemId)) { + newShowClaimants.delete(itemId); + } else { + newShowClaimants.add(itemId); + } + setShowClaimants(newShowClaimants); + }; + if (loading) { return
Loading registry items...
; } @@ -85,9 +94,6 @@ const RegistryManager = () => { return (
-

Registry Manager

- - {/* Add New Item Form */}
@@ -130,7 +136,6 @@ const RegistryManager = () => {
- {/* Registry Items List */}
@@ -144,7 +149,35 @@ const RegistryManager = () => { {registryItems.map((item) => ( - + - +
{item.name} +
+ {item.name} + {item.taken && ( + <> + Taken + {item.claimedBy && ( + + )} + + )} +
+ {item.taken && + item.claimedBy && + showClaimants.has(item.id) && ( +
+ Claimed by: {item.claimedBy} +
+ )} +
{item.link && ( { )} - {item.taken ? ( - Taken - ) : ( - Available - )} - {item.taken ? "Claimed" : "Available"}