LoqateController
is a key component that interacts with the Loqate API to handle address capture, address verification, and validation for email and phone numbers. It serves as the gateway for these services, ensuring they are integrated within the commercetools environment.
Key features:
LoqateApi
client, configured with project settings like API key, address quality threshold, and validation timeoutscreateLoqateApi(actionContext: ActionContext): LoqateApi
LoqateApi
client instance using the project configuration. The API key, address verification threshold, and other settings are passed to the client. This ensures that each API call is correctly authenticated and configured according to the commercetools projectgetAddresses(request: Request, actionContext: ActionContext): Promise<Response>
request
: contains query parameters such as id
, address
, countryIsoCode
, and city
actionContext
: provides access to the current commercetools project configurationverifyAddress(request: Request, actionContext: ActionContext): Promise<Response>
request
: contains the address details (country
, postalCode
, city
, and address
) to be verifiedactionContext
: accesses the commercetools project configurationgetCountryByIp(request: Request, actionContext: ActionContext): Promise<Response>
x-forwarded-for headers
request
: contains the client IP address or retrieves it from headersactionContext
: provides the commercetools project configurationvalidateEmail(request: Request, actionContext: ActionContext): Promise<Response>
request
: contains the email address to be validatedactionContext
: accesses project configuration for Email Validation settingsvalidatePhone(request: Request, actionContext: ActionContext): Promise<Response>
request
: contains the phone number and the country code for validationactionContext
: provides project-specific settings for phone validationgetSettings(request: Request, actionContext: ActionContext): Promise<Response>
getRestrictedListedCountries(request: Request, actionContext: ActionContext): Promise<Response>
LoqateApi
getAddresses()
: fetches address suggestions based on user inputverifyAddress()
: verifies the accuracy of a provided addressgetCountryByIp()
: retrieves the user’s country based on their IP addressvalidateEmail()
: validates the format and deliverability of an email addressvalidatePhone()
: validates the format and correctness of a phone numberisValidAddress(matchScore, addressVerificationLevel, goodAddressMinimumMatchscore)
extractDescription(description)
extractCountryIsoCode(id)
LoqateContext
provides a centralized state management system for Loqate-related features such as address capture, address verification, email validation, and phone validation. It encapsulates multiple Loqate API integrations and makes them accessible to components throughout the application via React’s context API.
Key Responsibilities:
LoqateContext
:
Exposed functions
1. setLoqateAddress(value: LoqateAddress)
value
: the updated address object containing fields like text
, city
, countryIsoCode
, etc.verifyAddress(loqateAddress: LoqateAddress): Promise<LoqateIsValidAddress>
LoqateIsValidAddress
object, indicating whether the address is valid or notloqateAddress
: the address object that needs to be verifiedisValid: true
or false
depending on whether the address is validvalidateEmail(): Promise<boolean | undefined>
true
/false), or
undefined``` if validation is not enabledvalidatePhone(): Promise<boolean | undefined>
LoqateAddress
object using the Loqate API. If Phone Validation is enabled, it checks the number’s format and correctness based on the countrytrue
/false
), or undefined
if validation is not enabledsetBypassAddressVerification(bypass: boolean)
bypass
: a boolean value that, when set to true
, bypasses Address VerifysetLoqateSettings(loqateSettings: LoqateSettings)
loqateSettings
: the settings object that dictates how Loqate functionality is used (e.g., enabling/disabling email or phone validation)debounceTime
, distinctUntilChanged
, and switchMap
to manage user input efficiently and ensure smooth address verification and lookup processes.
AddressCapture
component is responsible for capturing and suggesting addresses based on user input. It integrates seamlessly with the Loqate Context and allows for dynamic address suggestions and selection.
Key features:
useRestrictedCountries
that retrieves and manages the list of countries that are not restricted, filtering out any countries that are restricted based on an external API call to the loqate/getRestrictedCountries
action via the commercetools
SDK.
countries
: this state holds the list of available countries, which is initialized with a static list of all countries (allCountries
)fetchingInProgress
: a boolean state to track if the fetching process is currently ongoingsuccessfullyFetched
: a boolean state indicating whether the fetching was successfully completedfetchRestrictedCountries
function asynchronously calls the loqate/getRestrictedCountries
API action using the commercetools SDK
. It expects a response containing restricted country codesallCountries
)fetchingInProgress
is set to true
and successfullyFetched
is updated based on whether the call succeeds or failsmap
, tap
, catchError
) to handle the API call, map the response, and manage error handlingsubscribe
function applies the country filtering logic and updates the countries
state accordingly