import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '@/contexts/AuthContext'; import { supabase } from '@/lib/supabase'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Switch } from '@/components/ui/switch'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { useToast } from '@/hooks/use-toast'; import { Loader2, User, Mail, Shield, Bell, Palette, Globe, ArrowLeft } from 'lucide-react'; import { TwoFactorSetup } from '@/components/auth/TwoFactorSetup'; export default function ProfileSettings() { const navigate = useNavigate(); const { user } = useAuth(); const { toast } = useToast(); const [loading, setLoading] = useState(false); const [profile, setProfile] = useState({ username: '', full_name: '', bio: '', phone_number: '', location: '', website: '', language: 'en', theme: 'light', notifications_email: true, notifications_push: false, notifications_sms: false, two_factor_enabled: false }); useEffect(() => { if (user) { loadProfile(); } }, [user]); const loadProfile = async () => { try { const { data, error } = await supabase .from('profiles') .select('*') .eq('id', user?.id) .maybeSingle(); if (error) { console.error('Error loading profile:', error); return; } if (data) { setProfile({ username: data.username || '', full_name: data.full_name || '', bio: data.bio || '', phone_number: data.phone_number || '', location: data.location || '', website: data.website || '', language: data.language || 'en', theme: data.theme || 'light', notifications_email: data.notifications_email ?? true, notifications_push: data.notifications_push ?? false, notifications_sms: data.notifications_sms ?? false, two_factor_enabled: data.two_factor_enabled ?? false }); } } catch (error) { console.error('Error loading profile:', error); } }; const updateProfile = async () => { setLoading(true); try { // Call the secure upsert function const { data, error } = await supabase.rpc('upsert_user_profile', { p_username: profile.username || '', p_full_name: profile.full_name || null, p_bio: profile.bio || null, p_phone_number: profile.phone_number || null, p_location: profile.location || null, p_website: profile.website || null, p_language: profile.language || 'en', p_theme: profile.theme || 'dark', p_notifications_email: profile.notifications_email ?? true, p_notifications_push: profile.notifications_push ?? false, p_notifications_sms: profile.notifications_sms ?? false }); if (error) throw error; toast({ title: 'Success', description: 'Profile updated successfully', }); // Reload profile to ensure state is in sync await loadProfile(); } catch (error: any) { console.error('Profile update failed:', error); toast({ title: 'Error', description: error?.message || 'Failed to update profile', variant: 'destructive', }); } finally { setLoading(false); } }; const updateNotificationSettings = async () => { setLoading(true); try { // Call the upsert function with current profile data + notification settings const { data, error } = await supabase.rpc('upsert_user_profile', { p_username: profile.username || '', p_full_name: profile.full_name || null, p_bio: profile.bio || null, p_phone_number: profile.phone_number || null, p_location: profile.location || null, p_website: profile.website || null, p_language: profile.language || 'en', p_theme: profile.theme || 'dark', p_notifications_email: profile.notifications_email ?? true, p_notifications_push: profile.notifications_push ?? false, p_notifications_sms: profile.notifications_sms ?? false }); if (error) throw error; toast({ title: 'Success', description: 'Notification settings updated', }); } catch (error: any) { toast({ title: 'Error', description: error?.message || 'Failed to update notification settings', variant: 'destructive', }); } finally { setLoading(false); } }; const updateSecuritySettings = async () => { setLoading(true); try { const { error } = await supabase .from('profiles') .update({ two_factor_enabled: profile.two_factor_enabled, updated_at: new Date().toISOString() }) .eq('id', user?.id); if (error) throw error; toast({ title: 'Success', description: 'Security settings updated', }); } catch (error) { toast({ title: 'Error', description: 'Failed to update security settings', variant: 'destructive', }); } finally { setLoading(false); } }; const changePassword = async () => { const newPassword = prompt('Enter new password:'); if (!newPassword) return; setLoading(true); try { const { error } = await supabase.auth.updateUser({ password: newPassword }); if (error) throw error; toast({ title: 'Success', description: 'Password changed successfully', }); } catch (error) { toast({ title: 'Error', description: 'Failed to change password', variant: 'destructive', }); } finally { setLoading(false); } }; return (
Receive notifications via email
Receive push notifications in browser
Receive notifications via SMS