112 lines
4.2 KiB
TypeScript
112 lines
4.2 KiB
TypeScript
import React, { 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'
|
||
|
||
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 coords = useLocationStore((state) => state.coords)
|
||
const [mode, setMode] = useState<'none' | 'current' | 'other'>('none')
|
||
|
||
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 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: 'Mahabubnagar',
|
||
state: 'Telangana',
|
||
isDefault: true,
|
||
latitude: coords.latitude,
|
||
longitude: coords.longitude,
|
||
}
|
||
: undefined
|
||
}
|
||
onSuccess={() => {
|
||
setMode('none')
|
||
onDone()
|
||
}}
|
||
/>
|
||
</View>
|
||
</Modal>
|
||
</View>
|
||
)
|
||
}
|
||
|
||
export default CartAddressGate
|