import React, { useCallback, useRef, useState } from 'react' import { View } from 'react-native' import { BottomDialog, MyText, MyTouchableOpacity, tw } from 'common-ui' import AddressForm from '@/src/components/AddressForm' import { useAuthOptional } from '@/src/contexts/AuthContext' import { useNeoRegionStore } from '@/src/store/neoRegionStore' import { trpc } from '@/src/trpc-client' import { toNeoAddress } from '@/src/lib/neo-address' type ResolvedRegion = { regionId: number; boundId: number | null } interface NeoAddressFormProps { onDone?: () => void } /** * Adapter around the existing AddressForm. The form itself already implements * the add-address chart (device fix prefilled, map picker, serviceability gate, * "outside the area -> pick on the map"). This wrapper only: * - writes the result into the neo store, * - warns before an address in another region switches the browsing region. */ const NeoAddressForm: React.FC = ({ onDone }) => { const auth = useAuthOptional() const isAuthenticated = auth?.isAuthenticated ?? false const utils = trpc.useUtils() const currentRegionId = useNeoRegionStore((state) => state.regionId) const currentRegionName = useNeoRegionStore((state) => state.regionName) ?? 'your current region' const regions = useNeoRegionStore((state) => state.regions) const setAddresses = useNeoRegionStore((state) => state.setAddresses) const changeRegion = useNeoRegionStore((state) => state.changeRegion) const selectAddress = useNeoRegionStore((state) => state.selectAddress) const [resolved, setResolved] = useState(null) const [warningOpen, setWarningOpen] = useState(false) const warningResolver = useRef<((ok: boolean) => void) | null>(null) const targetRegionName = resolved != null ? regions.find((region) => region.id === resolved.regionId)?.name ?? 'another region' : 'another region' const onBeforeSubmit = useCallback((): boolean | Promise => { if (resolved == null || currentRegionId == null) return true if (resolved.regionId === currentRegionId) return true return new Promise((resolve) => { warningResolver.current = resolve setWarningOpen(true) }) }, [currentRegionId, resolved]) const finishWarning = (ok: boolean) => { setWarningOpen(false) warningResolver.current?.(ok) warningResolver.current = null } const handleSuccess = async (addressId?: number) => { // The server has the address now; refresh the store before selecting it. if (addressId != null) { try { const response = await utils.user.address.getUserAddresses.fetch(undefined) setAddresses(response.data.map(toNeoAddress)) } catch { // The subscription will refresh the list on the next fetch. } await selectAddress(addressId) onDone?.() return } // Signed-out draft: the address is stored locally and posted after login. // The browsing region still follows the chosen location now. if (resolved != null) { await changeRegion(resolved.regionId, resolved.boundId) } onDone?.() } return ( { void handleSuccess(addressId) }} /> finishWarning(false)}> Switch region? This address is in {targetRegionName}. Saving it will switch you from{' '} {currentRegionName} and clear your cart for that region. finishWarning(false)} activeOpacity={0.85} style={tw`border border-gray-200 py-3.5 rounded-xl items-center mb-3`} > Cancel finishWarning(true)} activeOpacity={0.85} style={tw`bg-indigo-600 py-3.5 rounded-xl items-center`} > Switch and save ) } export default NeoAddressForm