159 lines
6.1 KiB
TypeScript
159 lines
6.1 KiB
TypeScript
import React, { useEffect, useState } from 'react'
|
||
import { View, Modal } from 'react-native'
|
||
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
|
||
import { tw, theme, MyText, MyTouchableOpacity } from 'common-ui'
|
||
import AddressForm from '@/src/components/AddressForm'
|
||
import { useLocationStore } from '@/src/store/locationStore'
|
||
import { useRegionNeo } from '@/src/hooks/useRegionNeo'
|
||
import { trpc } from '@/src/trpc-client'
|
||
import { toNeoAddress } from '@/src/lib/neo-address'
|
||
import { useNeoRegionStore } from '@/src/store/neoRegionStore'
|
||
|
||
// How often to re-check the device location while we still have no fix.
|
||
const LOCATION_POLL_MS = 3000
|
||
|
||
interface CartAddressGateProps {
|
||
/** Called once the first address has been saved. */
|
||
onDone: () => void
|
||
/** Back out of the cart. */
|
||
onBack: () => void
|
||
}
|
||
|
||
/**
|
||
* Shown on the cart page when the browsing region/bound is known (from the
|
||
* device location) but the signed-in user has no delivery address yet. The
|
||
* first address they save becomes their default, and we then show the cart.
|
||
*/
|
||
const CartAddressGate: React.FC<CartAddressGateProps> = ({ onDone, onBack }) => {
|
||
const neo = useRegionNeo()
|
||
const coords = neo.coords
|
||
const selectAddress = neo.selectAddress
|
||
const utils = trpc.useUtils()
|
||
const setAddresses = useNeoRegionStore((state) => state.setAddresses)
|
||
const probeLocation = useLocationStore((state) => state.probe)
|
||
const refreshPosition = useLocationStore((state) => state.refresh)
|
||
const [mode, setMode] = useState<'none' | 'current' | 'other'>('none')
|
||
|
||
// The store takes its fix once per app launch, so a user who turns the location
|
||
// on after landing here would never see the "current location" option enable.
|
||
// While we have no fix, keep checking and take one as soon as the location is
|
||
// usable. Polling stops as soon as coords arrive.
|
||
useEffect(() => {
|
||
if (coords) return
|
||
|
||
let cancelled = false
|
||
const check = async () => {
|
||
// A fix is already in flight; don't stack another one on top.
|
||
if (useLocationStore.getState().status === 'locating') return
|
||
const usable = await probeLocation()
|
||
if (cancelled || !usable) return
|
||
void refreshPosition()
|
||
}
|
||
|
||
const timer = setInterval(() => void check(), LOCATION_POLL_MS)
|
||
return () => {
|
||
cancelled = true
|
||
clearInterval(timer)
|
||
}
|
||
}, [coords, probeLocation, refreshPosition])
|
||
|
||
return (
|
||
<View style={tw`flex-1 bg-gray-50`}>
|
||
<View style={tw`bg-white px-4 py-3 border-b border-gray-100 mt-6`}>
|
||
<View style={tw`flex-row items-center`}>
|
||
<MyTouchableOpacity testID="cart-address-gate-back" onPress={onBack} style={tw`p-2 -ml-2 mr-1`} activeOpacity={0.7}>
|
||
<MaterialIcons name="chevron-left" size={28} color="#374151" />
|
||
</MyTouchableOpacity>
|
||
<MyText style={tw`text-lg font-bold text-gray-900`}>Add Delivery Address</MyText>
|
||
</View>
|
||
</View>
|
||
|
||
<View style={tw`px-5 pt-6`}>
|
||
<MyText style={tw`text-2xl font-extrabold text-gray-900`}>Where should we deliver?</MyText>
|
||
<MyText style={tw`text-gray-500 mt-2 mb-6`}>
|
||
You don’t have a delivery address yet. Add one to continue.
|
||
</MyText>
|
||
|
||
<MyTouchableOpacity
|
||
testID="cart-address-current"
|
||
disabled={!coords}
|
||
activeOpacity={0.85}
|
||
onPress={() => setMode('current')}
|
||
style={tw`bg-white border border-gray-200 rounded-2xl p-4 mb-3 shadow-sm ${!coords ? 'opacity-50' : ''}`}
|
||
>
|
||
<View style={tw`flex-row items-center mb-1`}>
|
||
<MaterialIcons name="my-location" size={20} color={theme.colors.brand500} />
|
||
<MyText style={tw`text-base font-bold text-gray-900 ml-2`}>
|
||
Address of current Location
|
||
</MyText>
|
||
</View>
|
||
<MyText style={tw`text-gray-500 text-sm`}>
|
||
{coords
|
||
? `Use your current location (${coords.latitude.toFixed(5)}, ${coords.longitude.toFixed(5)})`
|
||
: 'Turn on location to use this option'}
|
||
</MyText>
|
||
</MyTouchableOpacity>
|
||
|
||
<MyTouchableOpacity
|
||
testID="cart-address-other"
|
||
activeOpacity={0.85}
|
||
onPress={() => setMode('other')}
|
||
style={tw`bg-white border border-gray-200 rounded-2xl p-4 shadow-sm`}
|
||
>
|
||
<View style={tw`flex-row items-center mb-1`}>
|
||
<MaterialIcons name="map" size={20} color={theme.colors.brand500} />
|
||
<MyText style={tw`text-base font-bold text-gray-900 ml-2`}>
|
||
Choose another location
|
||
</MyText>
|
||
</View>
|
||
<MyText style={tw`text-gray-500 text-sm`}>Pick a point on the map</MyText>
|
||
</MyTouchableOpacity>
|
||
</View>
|
||
|
||
<Modal
|
||
visible={mode !== 'none'}
|
||
animationType="slide"
|
||
onRequestClose={() => setMode('none')}
|
||
>
|
||
<View style={tw`flex-1 bg-white`}>
|
||
<AddressForm
|
||
// "Current location" prefills the device coordinates; "another
|
||
// location" starts empty so the user picks on the map.
|
||
initialValues={
|
||
mode === 'current' && coords
|
||
? {
|
||
name: '',
|
||
phone: '',
|
||
addressLine1: '',
|
||
addressLine2: '',
|
||
city: neo.regionName,
|
||
state: 'Telangana',
|
||
isDefault: true,
|
||
latitude: coords.latitude,
|
||
longitude: coords.longitude,
|
||
}
|
||
: undefined
|
||
}
|
||
onSuccess={(addressId) => {
|
||
void (async () => {
|
||
if (addressId != null) {
|
||
try {
|
||
const response = await utils.user.address.getUserAddresses.fetch(undefined)
|
||
if (response.data) setAddresses(response.data.map(toNeoAddress))
|
||
} catch {
|
||
// The form's invalidation will refresh it.
|
||
}
|
||
await selectAddress(addressId)
|
||
}
|
||
setMode('none')
|
||
onDone()
|
||
})()
|
||
}}
|
||
/>
|
||
</View>
|
||
</Modal>
|
||
</View>
|
||
)
|
||
}
|
||
|
||
export default CartAddressGate
|