overwrite country code if possible

This commit is contained in:
Samuel Newman
2025-12-11 20:32:22 +02:00
parent f59b6a25cd
commit 4344119d80
2 changed files with 32 additions and 11 deletions
+12 -8
View File
@@ -19,6 +19,7 @@ export function processPhoneNumber(
| { | {
valid: true valid: true
formatted: string formatted: string
countryCode: CountryCode
} }
| { | {
valid: false valid: false
@@ -42,15 +43,18 @@ export function processPhoneNumber(
reason: t`Number should be a mobile number`, reason: t`Number should be a mobile number`,
} }
} }
if (phoneNumber.country !== country) { let countryCode = country
return { if (phoneNumber.country && phoneNumber.country !== country) {
valid: false, if (phoneNumber.country === 'AC' || phoneNumber.country === 'TA') {
reason: t`Country code does not match`, countryCode = 'SH'
} else {
countryCode = phoneNumber.country
} }
} }
return { return {
valid: true, valid: true,
formatted: formatInternationalWithoutCountryCode(phoneNumber), formatted: formatE164lWithoutCountryCode(phoneNumber),
countryCode,
} }
} catch (error) { } catch (error) {
if (error instanceof ParseError) { if (error instanceof ParseError) {
@@ -65,8 +69,8 @@ export function processPhoneNumber(
* Format a phone number as the international format with the prefix * Format a phone number as the international format with the prefix
* removed. * removed.
*/ */
function formatInternationalWithoutCountryCode(phoneNumber: PhoneNumber) { function formatE164lWithoutCountryCode(phoneNumber: PhoneNumber) {
const intl = phoneNumber.formatInternational() const intl = phoneNumber.format('E.164')
const prefix = '+' + phoneNumber.countryCallingCode const prefix = '+' + phoneNumber.countryCallingCode
return intl.replace(prefix, '').trim() return intl.replace(prefix, '').trim()
} }
@@ -113,7 +117,7 @@ export function getCountryCodeFromPastedNumber(
if (countryCode && countryCode !== 'AC' && countryCode !== 'TA') { if (countryCode && countryCode !== 'AC' && countryCode !== 'TA') {
return { return {
countryCode, countryCode,
rest: formatInternationalWithoutCountryCode(phoneNumber), rest: formatE164lWithoutCountryCode(phoneNumber),
} }
} else { } else {
return undefined return undefined
+20 -3
View File
@@ -13,7 +13,14 @@ import {
import {cleanError, isNetworkError} from '#/lib/strings/errors' import {cleanError, isNetworkError} from '#/lib/strings/errors'
import {logger} from '#/logger' import {logger} from '#/logger'
import {useAgent} from '#/state/session' import {useAgent} from '#/state/session'
import {android, atoms as a, tokens, useGutters, useTheme} from '#/alf' import {
android,
atoms as a,
platform,
tokens,
useGutters,
useTheme,
} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {InternationalPhoneCodeSelect} from '#/components/InternationalPhoneCodeSelect' import {InternationalPhoneCodeSelect} from '#/components/InternationalPhoneCodeSelect'
@@ -102,7 +109,14 @@ export function PhoneInput({
const result = processPhoneNumber(phoneNumber, countryCode) const result = processPhoneNumber(phoneNumber, countryCode)
if (result.valid) { if (result.valid) {
setPhoneNumber(result.formatted) setPhoneNumber(result.formatted)
submit({phoneCountryCode: countryCode, phoneNumber: result.formatted}) setCountryCode(result.countryCode)
if (!isFindContactsFeatureEnabled(result.countryCode)) return
submit({
phoneCountryCode: result.countryCode,
phoneNumber: result.formatted,
})
} else { } else {
setFormatError(result.reason ?? _(msg`Invalid phone number`)) setFormatError(result.reason ?? _(msg`Invalid phone number`))
} }
@@ -182,7 +196,10 @@ export function PhoneInput({
setPhoneNumber(text) setPhoneNumber(text)
}} }}
placeholder={null} placeholder={null}
keyboardType="number-pad" // we don't want people entering +() etc keyboardType={platform({
ios: 'number-pad',
android: 'phone-pad',
})}
autoComplete="tel" autoComplete="tel"
returnKeyType={android('next')} returnKeyType={android('next')}
onSubmitEditing={onSubmitNumber} onSubmitEditing={onSubmitNumber}