import React, { useEffect, useState } from 'react'; import { View, Text, TouchableOpacity, Switch, ScrollView, ActivityIndicator, Alert, } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { StackNavigationProp } from '@react-navigation/stack'; import { getStyles } from './preferencesScreen.styles'; import { PrimaryButton } from '@components'; import { useAppTheme } from '@theme'; import { AuthStackParamList } from '../../../navigation/authStack'; import { useAppDispatch, useAppSelector } from '@store'; import { fetchCategories, completeOnboard } from './thunk'; import { OnboardingStackParamList } from '@navigation/onboardingStack'; type NavProp = StackNavigationProp< OnboardingStackParamList, 'PreferencesScreen' >; export const PreferencesScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const navigation = useNavigation(); const dispatch = useAppDispatch(); // ─── Redux state ────────────────────────────────────────────────────────── const { categories, isLoading, error, isSubmitting, submitError } = useAppSelector(state => state.preferences); const locationData = useAppSelector(state => state.setLocation); const profileData = useAppSelector(state => state.completeProfile); // ─── Local state ────────────────────────────────────────────────────────── const [selectedCategories, setSelectedCategories] = useState([]); const [notificationsEnabled, setNotificationsEnabled] = useState(true); // ─── Fetch categories on mount ──────────────────────────────────────────── useEffect(() => { dispatch(fetchCategories()); }, [dispatch]); const toggleCategory = (id: string) => { setSelectedCategories(prev => prev.includes(id) ? prev.filter(c => c !== id) : [...prev, id], ); }; // ─── Continue → call onboard API ───────────────────────────────────────── const handleContinue = async () => { if (!locationData.latitude || !locationData.longitude) { Alert.alert('Location missing', 'Please set your location first.'); return; } if (!profileData.name || !profileData.email) { Alert.alert('Profile incomplete', 'Please complete your profile first.'); return; } const payload = { name: profileData.name, email: profileData.email, gender: profileData.gender, latitude: locationData.latitude, longitude: locationData.longitude, addressLabel: profileData.addressLabel, addressLine1: profileData.addressLine1, mapAddress: locationData.mapAddress, houseNumber: profileData.houseNumber, landmark: profileData.landmark, city: profileData.city, state: profileData.state, postalCode: profileData.postalCode, addressPhone: profileData.addressPhone, categoryPreferences: selectedCategories, }; const result = await dispatch(completeOnboard(payload)); if (completeOnboard.fulfilled.match(result)) { navigation.navigate('OnboardingCompleteScreen'); } else { Alert.alert( 'Onboarding failed', (result.payload as string) ?? 'Something went wrong. Please try again.', ); } }; return ( Preferences Select your interests {isLoading && ( )} {!!error && ( {error} )} {!!submitError && ( {submitError} )} {!isLoading && ( {(categories || []).map(cat => ( toggleCategory(cat.id)} activeOpacity={0.7} > {cat.name} ))} )} Enable Notifications Get order updates and offers ); };