diff --git a/src/components/settings/PrivacyTab.tsx b/src/components/settings/PrivacyTab.tsx index 6378da44..164aa3cb 100644 --- a/src/components/settings/PrivacyTab.tsx +++ b/src/components/settings/PrivacyTab.tsx @@ -10,25 +10,27 @@ import { useToast } from '@/hooks/use-toast'; import { useAuth } from '@/hooks/useAuth'; import { supabase } from '@/integrations/supabase/client'; import { Eye, UserX, Shield, Search } from 'lucide-react'; - interface PrivacySettings { activity_visibility: 'public' | 'private'; search_visibility: boolean; show_location: boolean; show_age: boolean; } - interface ProfilePrivacy { privacy_level: 'public' | 'private'; show_pronouns: boolean; } - export function PrivacyTab() { - const { user, profile, refreshProfile } = useAuth(); - const { toast } = useToast(); + const { + user, + profile, + refreshProfile + } = useAuth(); + const { + toast + } = useToast(); const [loading, setLoading] = useState(false); const [preferences, setPreferences] = useState(null); - const form = useForm({ defaultValues: { privacy_level: (profile?.privacy_level === 'friends' ? 'public' : profile?.privacy_level) || 'public', @@ -39,31 +41,25 @@ export function PrivacyTab() { show_age: false } }); - useEffect(() => { fetchPreferences(); }, [user]); - const fetchPreferences = async () => { if (!user) return; - try { - const { data, error } = await supabase - .from('user_preferences') - .select('privacy_settings') - .eq('user_id', user.id) - .maybeSingle(); - + const { + data, + error + } = await supabase.from('user_preferences').select('privacy_settings').eq('user_id', user.id).maybeSingle(); if (error && error.code !== 'PGRST116') { console.error('Error fetching preferences:', error); return; } - if (data?.privacy_settings) { const privacySettings = data.privacy_settings as any; setPreferences(privacySettings); form.reset({ - privacy_level: profile?.privacy_level === 'friends' ? 'public' : (profile?.privacy_level || 'public'), + privacy_level: profile?.privacy_level === 'friends' ? 'public' : profile?.privacy_level || 'public', show_pronouns: profile?.show_pronouns || false, ...privacySettings }); @@ -75,27 +71,22 @@ export function PrivacyTab() { console.error('Error fetching preferences:', error); } }; - const initializePreferences = async () => { if (!user) return; - const defaultSettings: PrivacySettings = { activity_visibility: 'public', search_visibility: true, show_location: false, show_age: false }; - try { - const { error } = await supabase - .from('user_preferences') - .insert([{ - user_id: user.id, - privacy_settings: defaultSettings as any - }]); - + const { + error + } = await supabase.from('user_preferences').insert([{ + user_id: user.id, + privacy_settings: defaultSettings as any + }]); if (error) throw error; - setPreferences(defaultSettings); form.reset({ privacy_level: (profile?.privacy_level === 'friends' ? 'public' : profile?.privacy_level) || 'public', @@ -106,22 +97,18 @@ export function PrivacyTab() { console.error('Error initializing preferences:', error); } }; - const onSubmit = async (data: any) => { if (!user) return; - setLoading(true); try { // Update profile privacy settings - const { error: profileError } = await supabase - .from('profiles') - .update({ - privacy_level: data.privacy_level, - show_pronouns: data.show_pronouns, - updated_at: new Date().toISOString() - }) - .eq('user_id', user.id); - + const { + error: profileError + } = await supabase.from('profiles').update({ + privacy_level: data.privacy_level, + show_pronouns: data.show_pronouns, + updated_at: new Date().toISOString() + }).eq('user_id', user.id); if (profileError) throw profileError; // Update user preferences @@ -131,20 +118,16 @@ export function PrivacyTab() { show_location: data.show_location, show_age: data.show_age }; - - const { error: prefsError } = await supabase - .from('user_preferences') - .upsert([{ - user_id: user.id, - privacy_settings: privacySettings as any, - updated_at: new Date().toISOString() - }]); - + const { + error: prefsError + } = await supabase.from('user_preferences').upsert([{ + user_id: user.id, + privacy_settings: privacySettings as any, + updated_at: new Date().toISOString() + }]); if (prefsError) throw prefsError; - await refreshProfile(); setPreferences(privacySettings); - toast({ title: 'Privacy settings updated', description: 'Your privacy preferences have been successfully saved.' @@ -159,9 +142,7 @@ export function PrivacyTab() { setLoading(false); } }; - - return ( -
+ return
{/* Profile Visibility */}
@@ -179,12 +160,7 @@ export function PrivacyTab() {
- form.setValue('privacy_level', value)}> @@ -206,10 +182,7 @@ export function PrivacyTab() { Display your preferred pronouns on your profile

- form.setValue('show_pronouns', checked)} - /> + form.setValue('show_pronouns', checked)} />
@@ -219,24 +192,10 @@ export function PrivacyTab() { Display your location on your profile

- form.setValue('show_location', checked)} - /> + form.setValue('show_location', checked)} />
-
-
- -

- Display your age calculated from date of birth -

-
- form.setValue('show_age', checked)} - /> -
+
@@ -259,12 +218,7 @@ export function PrivacyTab() {
- form.setValue('activity_visibility', value)}> @@ -308,10 +262,7 @@ export function PrivacyTab() { Allow your profile to appear in search results

- form.setValue('search_visibility', checked)} - /> + form.setValue('search_visibility', checked)} />
@@ -351,6 +302,5 @@ export function PrivacyTab() { - - ); + ; } \ No newline at end of file