import React, { useState } from 'react'; import { View, ScrollView, Dimensions } from 'react-native'; import { useMutation } from '@tanstack/react-query'; import { Formik } from 'formik'; import * as Yup from 'yup'; import { tw, MyText, MyTouchableOpacity , Checkbox , MyTextInput , LoadingDialog } from 'common-ui'; import { trpc } from '../trpc-client'; import LocationAttacher from './LocationAttacher'; import {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view'; import type { AddressFormProps } from '@packages/shared'; const AddressForm: React.FC = ({ onSuccess, initialValues, isEdit = false }) => { const [isSubmitting, setIsSubmitting] = useState(false); const [submitError, setSubmitError] = useState(null); const [showGoogleMapsField, setShowGoogleMapsField] = useState(!!initialValues?.googleMapsUrl); const [currentLocation, setCurrentLocation] = useState<{ latitude: number; longitude: number } | null>( initialValues?.latitude && initialValues?.longitude ? { latitude: initialValues.latitude, longitude: initialValues.longitude } : null ); const createAddressMutation = trpc.user.address.createAddress.useMutation({ onSuccess: (data) => { setIsSubmitting(false); const addressId = data?.data?.id; // Delay to allow modal to close animation setTimeout(() => onSuccess(addressId), 350); }, onError: (error: any) => { setIsSubmitting(false); setSubmitError(error.message || 'Failed to save address'); }, }); const updateAddressMutation = trpc.user.address.updateAddress.useMutation({ onSuccess: () => { setIsSubmitting(false); // Delay to allow modal to close animation setTimeout(() => onSuccess(), 350); }, onError: (error: any) => { setIsSubmitting(false); setSubmitError(error.message || 'Failed to update address'); }, }); const validationSchema = Yup.object({ name: Yup.string().required('Name is required'), phone: Yup.string().required('Phone is required').matches(/^\d{10}$/, 'Phone must be 10 digits'), addressLine1: Yup.string().required('Address Line 1 is required'), addressLine2: Yup.string(), city: Yup.string().required('City is required'), state: Yup.string().required('State is required'), pincode: Yup.string().required('Pincode is required').matches(/^\d{6}$/, 'Pincode must be 6 digits'), isDefault: Yup.boolean(), }); return ( {isEdit ? 'Edit Address' : 'Add Address'} {/* Service Area Notice */} ℹ️ We currently serve only in Mahabubnagar town {/* Submit Error Message */} {submitError && ( {submitError} )} { setIsSubmitting(true); setSubmitError(null); const payload = { ...values, latitude: currentLocation?.latitude, longitude: currentLocation?.longitude, googleMapsUrl: values.googleMapsUrl || undefined, }; if (isEdit && initialValues?.id) { updateAddressMutation.mutate({ id: initialValues.id, ...payload }); } else { createAddressMutation.mutate(payload); } }} > {({ handleChange, handleBlur, handleSubmit, values, errors, touched, setFieldValue }) => ( {touched.name && errors.name && {errors.name}} {touched.phone && errors.phone && {errors.phone}} {touched.addressLine1 && errors.addressLine1 && {errors.addressLine1}} {touched.city && errors.city && {errors.city}} {touched.state && errors.state && {errors.state}} {touched.pincode && errors.pincode && {errors.pincode}} setFieldValue('googleMapsUrl', url)} onLocationChange={(location) => setCurrentLocation(location)} disabled={false} /> {!showGoogleMapsField && ( setShowGoogleMapsField(true)} disabled={false} style={tw`mb-1`} > Attach with Google Maps )} {showGoogleMapsField && ( 1. Open Google Maps and Find location{'\n'} 2. Long press the desired location{'\n'} 3. Click on Share and Click on Copy{'\n'} 4. Paste the copied url here in the field. )} setFieldValue('isDefault', !values.isDefault)} /> Set as default address handleSubmit()} disabled={isSubmitting} > {isSubmitting ? (isEdit ? 'Updating...' : 'Adding...') : (isEdit ? 'Update Address' : 'Add Address')} )} ); }; export default AddressForm;