import React, { useContext, useEffect, useState } from "react"; import { deleteHousehold, getHouseholdMembers, refreshInviteCode, removeMember, updateHousehold, updateMemberRole } from "../../api/households"; import { AuthContext } from "../../context/AuthContext"; import { HouseholdContext } from "../../context/HouseholdContext"; import "../../styles/components/manage/ManageHousehold.css"; export default function ManageHousehold() { const { userId } = useContext(AuthContext); const { activeHousehold, refreshHouseholds } = useContext(HouseholdContext); const [members, setMembers] = useState([]); const [loading, setLoading] = useState(true); const [editingName, setEditingName] = useState(false); const [newName, setNewName] = useState(""); const [showInviteCode, setShowInviteCode] = useState(false); const isAdmin = activeHousehold?.role === "admin"; useEffect(() => { loadMembers(); }, [activeHousehold?.id]); const loadMembers = async () => { if (!activeHousehold?.id) return; setLoading(true); try { const response = await getHouseholdMembers(activeHousehold.id); setMembers(response.data); } catch (error) { console.error("Failed to load members:", error); } finally { setLoading(false); } }; const handleUpdateName = async () => { if (!newName.trim() || newName === activeHousehold.name) { setEditingName(false); return; } try { console.log('Updating household:', activeHousehold.id, 'with name:', newName); const response = await updateHousehold(activeHousehold.id, newName); console.log('Update response:', response); await refreshHouseholds(); setEditingName(false); } catch (error) { console.error("Failed to update household name:", error); console.error("Error response:", error.response?.data); alert(`Failed to update household name: ${error.response?.data?.error || error.message}`); } }; const handleRefreshInvite = async () => { if (!confirm("Generate a new invite code? The old code will no longer work.")) return; try { const response = await refreshInviteCode(activeHousehold.id); await refreshHouseholds(); const refreshedInviteCode = response.data?.household?.invite_code; if (refreshedInviteCode) { alert(`New invite code: ${refreshedInviteCode}`); } else { alert("Invite code refreshed successfully"); } } catch (error) { console.error( "Failed to refresh invite code:", error?.response?.data?.error?.message || error?.response?.data?.message || error?.message ); alert("Failed to refresh invite code"); } }; const handleUpdateRole = async (userId, currentRole) => { const newRole = currentRole === "admin" ? "member" : "admin"; try { await updateMemberRole(activeHousehold.id, userId, newRole); await loadMembers(); } catch (error) { console.error("Failed to update role:", error); alert("Failed to update member role"); } }; const handleRemoveMember = async (userId, username) => { if (!confirm(`Remove ${username} from this household?`)) return; try { await removeMember(activeHousehold.id, userId); await loadMembers(); } catch (error) { console.error("Failed to remove member:", error); alert("Failed to remove member"); } }; const handleDeleteHousehold = async () => { if (!confirm(`Delete "${activeHousehold.name}"? This will delete all lists and data. This cannot be undone.`)) return; if (!confirm("Are you absolutely sure? Type DELETE to confirm.")) return; try { await deleteHousehold(activeHousehold.id); await refreshHouseholds(); } catch (error) { console.error("Failed to delete household:", error); alert("Failed to delete household"); } }; const copyInviteCode = () => { navigator.clipboard.writeText(activeHousehold.invite_code); alert("Invite code copied to clipboard!"); }; return (
{/* Household Name Section */}

Household Name

{editingName ? (
setNewName(e.target.value)} placeholder="Household name" autoFocus />
) : (

{activeHousehold.name}

{isAdmin && ( )}
)}
{/* Invite Code Section */} {isAdmin && (

Invite Code

Share this code with others to invite them to your household.

{showInviteCode && ( {activeHousehold.invite_code} )}
)} {/* Members Section */}

Members ({members.length})

{loading ? (

Loading members...

) : (
{members.map((member) => (
{member.role} {` ${member.username} [${member.id}] ${(member.id === parseInt(userId) ? " (You)" : "")} `}
{isAdmin && member.id !== parseInt(userId) && (
)}
))}
)}
{/* Danger Zone */} {isAdmin && (

Danger Zone

Deleting a household is permanent and will delete all lists, items, and history.

)}
); }