diff --git a/apps/user-ui/JEST-TEST-CASES.md b/apps/user-ui/JEST-TEST-CASES.md index 8c6a5e3..e907193 100644 --- a/apps/user-ui/JEST-TEST-CASES.md +++ b/apps/user-ui/JEST-TEST-CASES.md @@ -1011,6 +1011,8 @@ real. | ADDRF-37 | edge | map URL without lat/lng | base URL only | | ADDRF-38 | edge | edit + device fix | submits saved coords; device never auto-attached | | ADDRF-39 | edge | edit region differs from browsing | saved address town kept | +| ADDRF-40 | edge | attached location outside the service area | submit button disabled | +| ADDRF-41 | edge | picked town does not contain the location | submit button disabled | --- diff --git a/apps/user-ui/src/components/AddressForm.tsx b/apps/user-ui/src/components/AddressForm.tsx index 62cf4ae..95ed547 100644 --- a/apps/user-ui/src/components/AddressForm.tsx +++ b/apps/user-ui/src/components/AddressForm.tsx @@ -46,6 +46,8 @@ const AddressForm: React.FC(null); + // True while a location/bound resolution request is in flight. + const [isCheckingLocation, setIsCheckingLocation] = useState(false); const [locationSource, setLocationSource] = useState( initialValues?.latitude != null && initialValues?.longitude != null ? 'device' : null ); @@ -113,6 +115,7 @@ const AddressForm: React.FC handleSubmit()} - disabled={isSubmitting || !currentLocation} + disabled={isSubmitting || !isLocationServiceable} > {isSubmitting ? (isEdit ? 'Updating...' : 'Adding...') : (isEdit ? 'Update Address' : 'Add Address')} diff --git a/apps/user-ui/tests/components/address-form.test.tsx b/apps/user-ui/tests/components/address-form.test.tsx index aafc0bf..d87e663 100644 --- a/apps/user-ui/tests/components/address-form.test.tsx +++ b/apps/user-ui/tests/components/address-form.test.tsx @@ -201,6 +201,45 @@ describe('AddressForm', () => { expect(getByText('Attach a location to add address')).toBeTruthy() }) + it('ADDRF-40: an attached location outside the service area disables submit', async () => { + denyLocation() + spyResolveRegion(null) + const { getByTestId, getByText } = renderForm({ initialValues: withCoords() }) + + await waitFor(() => + expect( + getByText('This location is outside our delivery area. Try selecting on the map.') + ).toBeTruthy() + ) + + expect(getByTestId('address-submit').props.accessibilityState.disabled).toBe(true) + }) + + it('ADDRF-41: a town that does not contain the location keeps submit disabled', async () => { + denyLocation() + spyResolveRegion() + setProcedure('common.resolveBound', { + impl: jest.fn(() => { + throw new Error('nope') + }), + }) + const screen = renderForm({ initialValues: withCoords() }) + + await waitFor(() => expect(screen.onResolvedRegion).toHaveBeenCalled()) + expect(screen.getByTestId('address-submit').props.accessibilityState.disabled).toBe(false) + + fireEvent.press(screen.getByTestId('address-city')) + fireEvent.press(screen.getByTestId('address-city-option-2')) + + await waitFor(() => + expect( + screen.getByText('Your location is outside this town. Pick a point on the map inside it.') + ).toBeTruthy() + ) + + expect(screen.getByTestId('address-submit').props.accessibilityState.disabled).toBe(true) + }) + it('ADDRF-10: opening the city picker lists only the regions', async () => { denyLocation() const { getByTestId, queryByTestId, queryByText } = renderForm() diff --git a/apps/web-ui/src/components/AddressForm.tsx b/apps/web-ui/src/components/AddressForm.tsx index 30bd4ae..80e8eb8 100644 --- a/apps/web-ui/src/components/AddressForm.tsx +++ b/apps/web-ui/src/components/AddressForm.tsx @@ -27,6 +27,8 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv const [mapCenter, setMapCenter] = useState<{ lat: number; lng: number } | null>(null) const [locating, setLocating] = useState(false) const [locationError, setLocationError] = useState(null) + // True while a location/bound resolution request is in flight. + const [isCheckingLocation, setIsCheckingLocation] = useState(false) const [locationSource, setLocationSource] = useState( initialValues?.latitude != null && initialValues?.longitude != null ? 'device' : null ) @@ -61,6 +63,7 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv } setResolvedRegion({ regionId, boundId: null }) setLocationError(null) + setIsCheckingLocation(true) try { const resolved = await utils.common.resolveBound.fetch({ regionId, @@ -72,6 +75,8 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv onResolvedRegion?.(info) } catch { setLocationError('Your location is outside this town. Pick a point on the map inside it.') + } finally { + setIsCheckingLocation(false) } }, [utils, currentLocation, onResolvedRegion] @@ -95,6 +100,7 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv setCurrentLocation(coords) setLocationSource(source) setLocationError(null) + setIsCheckingLocation(true) try { const resolved = await utils.common.resolveRegionByLocation.fetch({ lat: coords.latitude, @@ -114,6 +120,8 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv } catch { setResolvedRegion(null) setLocationError('Could not verify this location. Please try again.') + } finally { + setIsCheckingLocation(false) } }, [utils, onResolvedRegion] @@ -208,6 +216,12 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv ) } + // Submit is gated on a location that actually resolved inside a serviceable + // town: a point outside the delivery area (or one still being checked) must + // not be saveable. + const isLocationServiceable = + currentLocation != null && resolvedRegion != null && !locationError && !isCheckingLocation + const handleSubmit = async () => { try { await validationSchema.validate(values, { abortEarly: false }) @@ -215,7 +229,7 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv setSubmitError('Please use your current location or select one on the map.') return } - if (!resolvedRegion || locationError) { + if (!resolvedRegion || locationError || isCheckingLocation) { setSubmitError(locationError || 'Please choose a town and a location inside it.') return } @@ -386,7 +400,7 @@ export function AddressForm({ onSuccess, initialValues, isEdit = false, onResolv fullWidth textContent={isSubmitting ? (isEdit ? 'Updating...' : 'Adding...') : (isEdit ? 'Update Address' : 'Add Address')} onClick={handleSubmit} - disabled={isSubmitting || !currentLocation} + disabled={isSubmitting || !isLocationServiceable} className="bg-brand-500 text-white" /> diff --git a/change-log.txt b/change-log.txt index a37bc5d..9850d21 100644 --- a/change-log.txt +++ b/change-log.txt @@ -10081,3 +10081,59 @@ record. Deploy target for dev: Worker `superadmin-ui-dev`. CLOUDFLARE_ENV=dev explanation, why prod hostnames stay out of top-level routes). Verified: superadmin.freshyo.in and devsuperadmin.freshyo.in both 307 -> /dashboard (200); CORS preflight from the dev origin now returns access-control-allow-origin. + +[2026-10-25 16:19 IST] Address form: disable submit unless the attached location resolved inside a +serviceable town. + +--- apps/user-ui/src/components/AddressForm.tsx +@@ ++ // True while a location/bound resolution request is in flight. ++ const [isCheckingLocation, setIsCheckingLocation] = useState(false); +@@ resolveLocation + setLocationError(null); ++ setIsCheckingLocation(true); + try { +@@ + } catch { + setResolvedRegion(null); + setLocationError('Could not verify this location. Please try again.'); ++ } finally { ++ setIsCheckingLocation(false); + } +@@ handleCityChange + setLocationError(null); ++ setIsCheckingLocation(true); + try { +@@ + } catch { + setLocationError('Your location is outside this town. Pick a point on the map inside it.'); ++ } finally { ++ setIsCheckingLocation(false); + } +@@ ++ // Submit is gated on a location that actually resolved inside a serviceable ++ // town: a point outside the delivery area (or one still being checked) must ++ // not be saveable. ++ const isLocationServiceable = ++ currentLocation != null && resolvedRegion != null && !locationError && !isCheckingLocation; +@@ onSubmit +- if (!resolvedRegion || locationError) { ++ if (!resolvedRegion || locationError || isCheckingLocation) { +@@ submit button +- style={tw`bg-indigo-600 p-3 rounded ${isSubmitting || !currentLocation ? 'opacity-50' : ''}`} ++ style={tw`bg-indigo-600 p-3 rounded ${isSubmitting || !isLocationServiceable ? 'opacity-50' : ''}`} + onPress={() => handleSubmit()} +- disabled={isSubmitting || !currentLocation} ++ disabled={isSubmitting || !isLocationServiceable} + +--- apps/user-ui/tests/components/address-form.test.tsx +Added ADDRF-40 (out-of-area attach disables submit) and ADDRF-41 (town mismatch keeps it +disabled). + +--- apps/web-ui/src/components/AddressForm.tsx +Same change as the user-ui form (web counterpart per the mirror rule): isCheckingLocation +state, try/finally around resolveLocation/handleCityChange, isLocationServiceable gate on +handleSubmit and MyButton disabled. + +--- apps/user-ui/JEST-TEST-CASES.md +Catalogue rows ADDRF-40/41 added.