@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
174 lines (173 loc) • 5.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.autofill = autofill;
exports.getMockData = getMockData;
exports.unsupportedCountryCodeMessage = exports.supportedCountryCodes = exports.responseResolver = exports.preResponseResolver = void 0;
exports.validator = validator;
var _index = require("../../utils/index.js");
var _index2 = _interopRequireDefault(require("../../utils/json-pointer/index.js"));
var _createContext = require("../createContext.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const supportedCountryCodes = exports.supportedCountryCodes = ['NO', 'DK', 'SE', 'FI', 'NL', 'DE', 'US', 'BE', 'FO', 'GL', 'IS', 'SJ'];
const unsupportedCountryCodeMessage = exports.unsupportedCountryCodeMessage = 'Postal code verification is not supported for {countryCode}.';
const preResponseResolver = ({
value
}) => {
if (!value) {
return {
postal_codes: []
};
}
};
exports.preResponseResolver = preResponseResolver;
const responseResolver = (data, handlerConfig) => {
var _data$postal_codes;
const resolver = handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.responseResolver;
if (typeof resolver === 'function') {
return resolver(data);
}
const {
postal_code,
city
} = (data === null || data === void 0 || (_data$postal_codes = data.postal_codes) === null || _data$postal_codes === void 0 ? void 0 : _data$postal_codes[0]) || {};
return {
matcher: value => value === postal_code,
payload: {
city
}
};
};
exports.responseResolver = responseResolver;
function autofill(generalConfig, handlerConfig) {
const abortControllerRef = {
current: null
};
return async function autofillHandler(value, additionalArgs) {
if (!(typeof value === 'string' && value.length >= 4)) {
return;
}
const {
countryCode
} = (0, _createContext.handleCountryPath)({
value,
countryCode: handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.countryCode,
additionalArgs,
handler: autofillHandler
});
if (!(0, _createContext.isSupportedCountryCode)(countryCode, supportedCountryCodes)) {
return;
}
try {
var _handlerConfig$preRes;
const parameters = {
countryCode: String(countryCode).toLowerCase()
};
const {
data
} = await (0, _createContext.fetchData)(value, {
generalConfig,
parameters,
abortControllerRef,
preResponseResolver: (_handlerConfig$preRes = handlerConfig === null || handlerConfig === void 0 ? void 0 : 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 = _index2.default.has(internalData, cityPath) ? _index2.default.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;
}
};
}
function validator(generalConfig, handlerConfig) {
const abortControllerRef = {
current: null
};
return async function validatorHandler(value, additionalArgs) {
if (!(typeof value === 'string' && value.length >= 4)) {
return;
}
const {
countryCode
} = (0, _createContext.handleCountryPath)({
value,
countryCode: handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.countryCode,
additionalArgs,
handler: validatorHandler
});
if (!(0, _createContext.isSupportedCountryCode)(countryCode, supportedCountryCodes)) {
return new Error(unsupportedCountryCodeMessage.replace('{countryCode}', countryCode));
}
try {
var _handlerConfig$preRes2;
const parameters = {
countryCode: String(countryCode).toLowerCase()
};
const {
data,
status
} = await (0, _createContext.fetchData)(value, {
generalConfig,
parameters,
abortControllerRef,
preResponseResolver: (_handlerConfig$preRes2 = handlerConfig === null || handlerConfig === void 0 ? void 0 : handlerConfig.preResponseResolver) !== null && _handlerConfig$preRes2 !== void 0 ? _handlerConfig$preRes2 : preResponseResolver
});
const onMatch = () => {
return new _index.FormError('PostalCodeAndCity.invalidCode');
};
const {
matcher
} = responseResolver(data, handlerConfig);
const match = matcher(value);
if (status !== 400 && !match) {
return onMatch();
}
} catch (error) {
return error;
}
};
}
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