freshyo/apps/user-ui/src/components/location/neo/NeoAddressForm.tsx
2026-11-08 13:49:17 +05:30

122 lines
4.6 KiB
TypeScript

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<NeoAddressFormProps> = ({ 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<ResolvedRegion | null>(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<boolean> => {
if (resolved == null || currentRegionId == null) return true
if (resolved.regionId === currentRegionId) return true
return new Promise<boolean>((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 (
<View style={tw`flex-1 bg-white`}>
<AddressForm
draftMode={!isAuthenticated}
onResolvedRegion={setResolved}
onBeforeSubmit={onBeforeSubmit}
onSuccess={(addressId) => {
void handleSuccess(addressId)
}}
/>
<BottomDialog open={warningOpen} onClose={() => finishWarning(false)}>
<View style={tw`p-6`}>
<MyText style={tw`text-xl font-bold text-gray-900 mb-2`}>Switch region?</MyText>
<MyText style={tw`text-gray-600 mb-5`}>
This address is in {targetRegionName}. Saving it will switch you from{' '}
{currentRegionName} and clear your cart for that region.
</MyText>
<MyTouchableOpacity
testID="neo-region-warning-cancel"
onPress={() => finishWarning(false)}
activeOpacity={0.85}
style={tw`border border-gray-200 py-3.5 rounded-xl items-center mb-3`}
>
<MyText style={tw`text-gray-700 font-bold`}>Cancel</MyText>
</MyTouchableOpacity>
<MyTouchableOpacity
testID="neo-region-warning-continue"
onPress={() => finishWarning(true)}
activeOpacity={0.85}
style={tw`bg-indigo-600 py-3.5 rounded-xl items-center`}
>
<MyText style={tw`text-white font-bold`}>Switch and save</MyText>
</MyTouchableOpacity>
</View>
</BottomDialog>
</View>
)
}
export default NeoAddressForm