import React, { useEffect, useMemo, useState } from 'react'; import { View, Text, TouchableOpacity, ScrollView, KeyboardAvoidingView, Platform, Alert, } from 'react-native'; import Icon from 'react-native-vector-icons/Ionicons'; import { getStyles } from './addLead.styles'; import { useTheme } from '@theme'; import { FormInput, FormPicker } from '@components'; import { RootState, useAppDispatch, useAppSelector } from '@store'; import { addLead, resetAddLeadState } from './thunk'; export const AddLeadScreen = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const userData = useAppSelector((state: RootState) => state.auth.user_data); const { loading, successMessage, error } = useAppSelector( (state: RootState) => state.addLead, ); const { items: sourceItems } = useAppSelector( (state: RootState) => state.sourceList, ); const { items: statusItems } = useAppSelector( (state: RootState) => state.statusList, ); const { items: countryItems } = useAppSelector( (state: RootState) => state.countryList, ); const [company, setCompany] = useState(''); const [fullName, setFullName] = useState(''); const [email, setEmail] = useState(''); const [phone, setPhone] = useState(''); const [website, setWebsite] = useState(''); const [source, setSource] = useState(''); const [status, setStatus] = useState(''); const [address, setAddress] = useState(''); const [city, setCity] = useState(''); const [state, setState] = useState(''); const [zip, setZip] = useState(''); const [country, setCountry] = useState(''); const sourceOptions = useMemo( () => sourceItems.map(s => ({ label: s.name, value: s.id })), [sourceItems], ); const statusOptions = useMemo( () => statusItems.map(s => ({ label: s.name, value: s.id })), [statusItems], ); const countryOptions = useMemo( () => countryItems.map(c => ({ label: c.short_name, value: c.country_id })), [countryItems], ); useEffect(() => { if (successMessage) { Alert.alert('Success', successMessage); setCompany(''); setFullName(''); setEmail(''); setPhone(''); setWebsite(''); setSource(''); setStatus(''); setAddress(''); setCity(''); setState(''); setZip(''); setCountry(''); dispatch(resetAddLeadState()); } else if (error) { Alert.alert('Error', error); dispatch(resetAddLeadState()); } }, [successMessage, error, dispatch]); const handleSubmit = () => { if (!fullName.trim() || !email.trim() || !source || !status) { Alert.alert('Error', 'Full Name, Email, Source and Status are required.'); return; } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email.trim())) { Alert.alert('Error', 'Please enter a valid email address.'); return; } const staffId = userData?.staffid || ''; if (!staffId) { Alert.alert('Error', 'User session invalid. Please log in again.'); return; } dispatch( addLead({ staff_id: staffId, name: fullName, company, email, phonenumber: phone, website, source, status, address, city, state, zip, country, }), ); }; return ( {/* Basic Info */} Basic Information } /> } /> } /> } /> } /> {/* Classification */} Classification {/* Address */} Address } /> } /> {/* Submit */} {loading ? 'Creating Lead...' : 'Create Lead'} ); };