This commit is contained in:
shafi54 2026-10-15 22:07:50 +05:30
parent 38401bc2ec
commit 2986f18c59
2 changed files with 87 additions and 19 deletions

View file

@ -28,7 +28,6 @@ const AddressForm: React.FC<AddressFormProps> = ({
const [isSubmitting, setIsSubmitting] = useState(false);
const [submitError, setSubmitError] = useState<string | null>(null);
const [mapOpen, setMapOpen] = useState(false);
const [mapConfirmOpen, setMapConfirmOpen] = useState(false);
const [currentLocation, setCurrentLocation] = useState<Coords | null>(
initialValues?.latitude != null && initialValues?.longitude != null
? { latitude: initialValues.latitude, longitude: initialValues.longitude }
@ -53,14 +52,48 @@ const AddressForm: React.FC<AddressFormProps> = ({
const cityName = regions.find((region) => region.id === resolvedRegion?.regionId)?.name ?? '';
// The map focuses on the region the address will belong to: the region a picked
// location resolved to, else the browsing region. Confirmed before opening, and
// its stored centre (first-priority bound) becomes the map's centre.
// location resolved to, else the browsing region. Its stored centre
// (first-priority bound) becomes the map's centre.
const mapRegion =
regions.find((region) => region.id === (resolvedRegion?.regionId ?? browsingRegionId)) ?? null;
const openMap = useCallback(() => {
setMapConfirmOpen(false);
/** Centre the map WebView opens on (null = the page's own default). */
const [mapCenter, setMapCenter] = useState<{ lat: number; lng: number } | null>(null);
/**
* The pending "which region is this map for?" confirmation. Only the two
* location triggers ask — the City selector's "Select in Map" opens straight
* away. Null means no dialog.
*/
const [mapDialog, setMapDialog] = useState<{ regionName: string | null } | null>(null);
const requestMapForAddressRegion = useCallback(() => {
Keyboard.dismiss();
setMapCenter(mapRegion?.center ?? null);
setMapDialog({ regionName: mapRegion?.name ?? null });
}, [mapRegion]);
// The City selector's fallback: no town picked, so open the map straight away,
// focused on the first region's bound (the first row of that selector).
const openMapForFirstRegion = useCallback(() => {
const firstRegion = regions[0] ?? null;
setCityOpen(false);
setMapCenter(firstRegion?.center ?? null);
setMapOpen(true);
}, [regions]);
const confirmMapDialog = useCallback(() => {
setMapDialog(null);
setMapOpen(true);
}, []);
const dismissMapDialog = useCallback(() => {
setMapDialog(null);
setMapCenter(null);
}, []);
const closeMap = useCallback(() => {
setMapOpen(false);
setMapCenter(null);
}, []);
// Resolve coordinates -> region + bound. Used for whichever source the user
@ -344,6 +377,16 @@ const AddressForm: React.FC<AddressFormProps> = ({
{regions.length === 0 && (
<MyText style={tw`p-3 text-gray-400 text-sm`}>No towns available</MyText>
)}
{/* Last option: skip the town list and pick the point on the map. */}
<MyTouchableOpacity
testID="address-city-option-map"
onPress={openMapForFirstRegion}
style={tw`p-3 flex-row items-center`}
>
<MaterialIcons name="map" size={18} color="#3B82F6" />
<MyText style={tw`text-blue-500 font-medium ml-2`}>Select in Map</MyText>
</MyTouchableOpacity>
</View>
)}
</View>
@ -376,7 +419,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
</MyText>
<MyTouchableOpacity
testID="address-choose-location"
onPress={() => setMapConfirmOpen(true)}
onPress={requestMapForAddressRegion}
style={tw`flex-row items-center mt-2`}
>
<MaterialIcons name="map" size={18} color="#3B82F6" />
@ -388,7 +431,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
) : (
<MyTouchableOpacity
testID="address-attach-location"
onPress={() => setMapConfirmOpen(true)}
onPress={requestMapForAddressRegion}
style={tw`flex-row items-center`}
>
<MaterialIcons name="place" size={18} color="#3B82F6" />
@ -438,35 +481,37 @@ const AddressForm: React.FC<AddressFormProps> = ({
/>
{/* Confirm the region before handing over to the map WebView, which opens
focused on that region's centre. */}
<BottomDialog open={mapConfirmOpen} onClose={() => setMapConfirmOpen(false)}>
focused on that region's centre. ("Select in Map" skips this.) */}
<BottomDialog open={mapDialog !== null} onClose={dismissMapDialog}>
<View style={tw`p-6`}>
<View style={tw`flex-row items-center mb-3`}>
<View style={tw`w-10 h-10 bg-blue-50 rounded-full items-center justify-center mr-3`}>
<MaterialIcons name="map" size={20} color="#2563EB" />
</View>
<MyText style={tw`text-lg font-bold text-gray-900 flex-1`}>
{mapRegion ? `Open the map for ${mapRegion.name}?` : 'Open the map?'}
{mapDialog?.regionName
? `Open the map for ${mapDialog.regionName}?`
: 'Open the map?'}
</MyText>
</View>
<MyText style={tw`text-gray-600 text-sm`}>
{mapRegion
? `The map will open on ${mapRegion.name}. Pick a point inside this region.`
{mapDialog?.regionName
? `The map will open on ${mapDialog.regionName}. Pick a point inside this region.`
: 'Pick a point on the map.'}
</MyText>
<View style={tw`flex-row gap-3 mt-6`}>
<MyTouchableOpacity
testID="address-map-confirm-cancel"
onPress={() => setMapConfirmOpen(false)}
onPress={dismissMapDialog}
style={tw`flex-1 border border-gray-200 py-3.5 rounded-xl items-center`}
>
<MyText style={tw`text-gray-700 font-bold`}>Cancel</MyText>
</MyTouchableOpacity>
<MyTouchableOpacity
testID="address-map-confirm-open"
onPress={openMap}
onPress={confirmMapDialog}
style={tw`flex-1 bg-brand500 py-3.5 rounded-xl items-center`}
>
<MyText style={tw`text-white font-bold`}>Open map</MyText>
@ -476,15 +521,15 @@ const AddressForm: React.FC<AddressFormProps> = ({
</BottomDialog>
{/* Full-screen map picker. */}
<Modal visible={mapOpen} animationType="slide" onRequestClose={() => setMapOpen(false)}>
<Modal visible={mapOpen} animationType="slide" onRequestClose={closeMap}>
<LocationMapStep
lat={mapRegion?.center?.lat}
lng={mapRegion?.center?.lng}
lat={mapCenter?.lat}
lng={mapCenter?.lng}
onSelect={(coords) => {
setMapOpen(false);
closeMap();
void resolveLocation(coords, 'map');
}}
onCancel={() => setMapOpen(false)}
onCancel={closeMap}
/>
</Modal>
</ScrollView>

View file

@ -4768,3 +4768,26 @@ user-ui
selected region; on confirm the map opens focused on that region's stored centre.
apps/web-ui: unchanged (option A - the page already centres from ?lat=&lng).
No tests run.
2026-10-15 22:03:55 AddressForm City selector: + "Select in Map" as the last option.
apps/user-ui/src/components/AddressForm.tsx
- the City picker (the selector above the Delivery Location / map block) now ends with a
"Select in Map" row. Picking it opens the map focused on the FIRST region's bound (the
first region in that same name-ordered list; its stored `center` is already its
first-priority bound's centre).
- the pending-map state is now a single `mapRequest { regionName, center }` instead of a
boolean, so the confirm dialog's wording and the WebView's lat/lng both come from the
request: the address's region for the two existing triggers, the first region for
"Select in Map". It is cleared when the map closes.
- the confirmation BottomDialog is kept for this path too.
No other file changed. No tests run.
2026-10-15 22:09:40 "Select in Map" now opens the map directly (no confirmation).
apps/user-ui/src/components/AddressForm.tsx
- the single `mapRequest` state is split: `mapCenter` (what the WebView opens on) and
`mapDialog` (the pending confirmation, null = hidden). "Select in Map" sets the centre to
the first region's bound and opens the map in the same step, skipping the dialog; the two
location triggers still ask first.
No other file changed. No tests run.