// islands/TenantSettings.tsx import { useState } from "preact/hooks"; import { User } from "../db/interfaces.ts"; // Define the structure for the preference objects interface NotificationPreferences { email_messages: boolean; email_reminders: boolean; email_payments: boolean; } interface PaymentPreferences { payment_reminders: boolean; // Note: default_auto_pay is typically lease-specific, this is for general preference } interface TenantSettingsProps { user: User; } function parsePreferences>( raw: unknown, fallback: T, ): T { if (!raw) return fallback; if (typeof raw === "string") { try { const parsed = JSON.parse(raw) as Partial; return { ...fallback, ...parsed }; } catch { return fallback; } } if (typeof raw === "object") { return { ...fallback, ...(raw as Partial) }; } return fallback; } export default function TenantSettings({ user }: TenantSettingsProps) { const initialNotifPrefs: NotificationPreferences = parsePreferences( user.notification_preferences_json as unknown, { email_messages: true, email_reminders: true, email_payments: true, }, ); const initialPaymentPrefs: PaymentPreferences = parsePreferences( user.payment_preferences_json as unknown, { payment_reminders: true, }, ); const [notificationPrefs, setNotificationPrefs] = useState(initialNotifPrefs); const [paymentPrefs, setPaymentPrefs] = useState(initialPaymentPrefs); const [status, setStatus] = useState<"idle" | "saving" | "success" | "error">( "idle", ); const [errorMessage, setErrorMessage] = useState(null); const handleNotificationChange = (key: keyof NotificationPreferences) => { setNotificationPrefs((prev) => ({ ...prev, [key]: !prev[key] })); }; const handlePaymentChange = (key: keyof PaymentPreferences) => { setPaymentPrefs((prev) => ({ ...prev, [key]: !prev[key] })); }; const handleSubmit = async (e: Event) => { e.preventDefault(); setStatus("saving"); setErrorMessage(null); try { const response = await fetch("/api/user/preferences", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ notification_preferences: notificationPrefs, payment_preferences: paymentPrefs, }), }); if (response.ok) { setStatus("success"); setTimeout(() => setStatus("idle"), 3000); } else { const errorData = await response.json(); setErrorMessage(errorData.error || "Failed to save preferences."); setStatus("error"); } } catch (e) { console.error("Submission error:", e); setErrorMessage("An unexpected error occurred."); setStatus("error"); } }; const StatusMessage = () => { switch (status) { case "saving": return
Saving...
; case "success": return (
Preferences saved successfully!
); case "error": return
Error: {errorMessage}
; default: return null; } }; const PreferenceToggle = ({ label, checked, onChange }: { label: string; checked: boolean; onChange: () => void; }) => (
{label}
); return (
{/* Notification Preferences */}

Notification Preferences

Control how you receive alerts about your tenancy.

handleNotificationChange("email_messages")} /> handleNotificationChange("email_reminders")} /> handleNotificationChange("email_payments")} />
{/* Payment Preferences */}

Payment Preferences

Manage settings related to your rent payments.

handlePaymentChange("payment_reminders")} /> {/* Auto-pay setup is handled on the Payment Methods page, not here */}
{/* Save Button and Status */}
); }