issues fixed on tenants

This commit is contained in:
azeeee05
2026-01-19 15:52:16 +05:30
parent 5a8973ffe8
commit 8013650c9a
@@ -240,6 +240,9 @@ const AllTenants = () => {
event.preventDefault(); event.preventDefault();
if (!selectedTenant) return; if (!selectedTenant) return;
// Capture the ID to use in the async context
const tenantId = selectedTenant.id;
setEditError(""); setEditError("");
setIsSaving(true); setIsSaving(true);
@@ -251,15 +254,22 @@ const AllTenants = () => {
is_active: editForm.is_active, is_active: editForm.is_active,
}; };
const updatedTenant = await tenantsApi.update(selectedTenant.id, payload); const updatedTenant = await tenantsApi.update(tenantId, payload);
// Safeguard: Ensure we actually got a tenant back
if (!updatedTenant || !updatedTenant.id) {
throw new Error("Invalid response from server");
}
setTenants((prev) => setTenants((prev) =>
prev.map((tenant) => prev.map((tenant) =>
tenant.id === updatedTenant.id ? updatedTenant : tenant tenant.id === tenantId ? updatedTenant : tenant
) )
); );
setIsEditOpen(false); setIsEditOpen(false);
setSelectedTenant(null); setSelectedTenant(null);
} catch (error) { } catch (error) {
console.error("Update failed:", error);
const message = const message =
error instanceof Error ? error.message : "Unable to update tenant."; error instanceof Error ? error.message : "Unable to update tenant.";
setEditError(message); setEditError(message);
@@ -271,17 +281,21 @@ const AllTenants = () => {
const handleDelete = async () => { const handleDelete = async () => {
if (!selectedTenant) return; if (!selectedTenant) return;
// Capture the ID securely
const tenantId = selectedTenant.id;
setDeleteError(""); setDeleteError("");
setIsDeleting(true); setIsDeleting(true);
try { try {
await tenantsApi.remove(selectedTenant.id); await tenantsApi.remove(tenantId);
setTenants((prev) => setTenants((prev) =>
prev.filter((tenant) => tenant.id !== selectedTenant.id) prev.filter((tenant) => tenant.id !== tenantId)
); );
setIsDeleteOpen(false); setIsDeleteOpen(false);
setSelectedTenant(null); setSelectedTenant(null);
} catch (error) { } catch (error) {
console.error("Delete failed:", error);
const message = const message =
error instanceof Error ? error.message : "Unable to delete tenant."; error instanceof Error ? error.message : "Unable to delete tenant.";
setDeleteError(message); setDeleteError(message);