UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

161 lines 4.62 kB
import { FormError } from "../../utils/index.js"; import pointer from "../../utils/json-pointer/index.js"; import { fetchData, handleCountryPath, isSupportedCountryCode } from "../createContext.js"; export const supportedCountryCodes = ['NO', 'DK', 'SE', 'FI', 'NL', 'DE', 'US', 'BE', 'FO', 'GL', 'IS', 'SJ']; export const unsupportedCountryCodeMessage = 'Postal code verification is not supported for {countryCode}.'; export const preResponseResolver = ({ value }) => { if (!value) { return { postal_codes: [] }; } }; export const responseResolver = (data, handlerConfig) => { const resolver = handlerConfig?.responseResolver; if (typeof resolver === 'function') { return resolver(data); } const { postal_code, city } = data?.postal_codes?.[0] || {}; return { matcher: value => value === postal_code, payload: { city } }; }; export function autofill(generalConfig, handlerConfig) { const abortControllerRef = { current: null }; return async function autofillHandler(value, additionalArgs) { if (!(typeof value === 'string' && value.length >= 4)) { return; } const { countryCode } = handleCountryPath({ value, countryCode: handlerConfig?.countryCode, additionalArgs, handler: autofillHandler }); if (!isSupportedCountryCode(countryCode, supportedCountryCodes)) { return; } try { var _handlerConfig$preRes; const parameters = { countryCode: String(countryCode).toLowerCase() }; const { data } = await fetchData(value, { generalConfig, parameters, abortControllerRef, preResponseResolver: (_handlerConfig$preRes = handlerConfig?.preResponseResolver) !== null && _handlerConfig$preRes !== void 0 ? _handlerConfig$preRes : preResponseResolver }); const onMatch = payload => { const { cityPath } = handlerConfig || {}; if (cityPath) { if (!additionalArgs.dataContext) { throw new Error('No data context found in the postalCode connector'); } const { dataContext } = additionalArgs; const internalData = dataContext.internalDataRef.current; const value = pointer.has(internalData, cityPath) ? pointer.get(internalData, cityPath) : undefined; if (!value) { dataContext.handlePathChangeUnvalidated(cityPath, payload.city); } } }; const { matcher, payload } = responseResolver(data, handlerConfig); const match = matcher(value); if (match) { return onMatch(payload); } } catch (error) { return error; } }; } export function validator(generalConfig, handlerConfig) { const abortControllerRef = { current: null }; return async function validatorHandler(value, additionalArgs) { if (!(typeof value === 'string' && value.length >= 4)) { return; } const { countryCode } = handleCountryPath({ value, countryCode: handlerConfig?.countryCode, additionalArgs, handler: validatorHandler }); if (!isSupportedCountryCode(countryCode, supportedCountryCodes)) { return new Error(unsupportedCountryCodeMessage.replace('{countryCode}', countryCode)); } try { var _handlerConfig$preRes2; const parameters = { countryCode: String(countryCode).toLowerCase() }; const { data, status } = await fetchData(value, { generalConfig, parameters, abortControllerRef, preResponseResolver: (_handlerConfig$preRes2 = handlerConfig?.preResponseResolver) !== null && _handlerConfig$preRes2 !== void 0 ? _handlerConfig$preRes2 : preResponseResolver }); const onMatch = () => { return new FormError('PostalCodeAndCity.invalidCode'); }; const { matcher } = responseResolver(data, handlerConfig); const match = matcher(value); if (status !== 400 && !match) { return onMatch(); } } catch (error) { return error; } }; } export function getMockData(countryCode) { switch (String(countryCode).toUpperCase()) { case 'SE': return { postal_codes: [{ city: 'Stockholm', postal_code: '11432' }] }; case 'NO': default: return { postal_codes: [{ city: 'Vollen', postal_code: '1391' }] }; } } //# sourceMappingURL=postalCode.js.map