sprinque-js-sdk-wip
Version:
UI kit to implement Pay by Invoice with Sprinque
65 lines (64 loc) • 2.98 kB
JavaScript
import i18next from 'i18next';
import { COMPANY_NAME, COUNTRY_SELECT, REG_NUMBER, REG_NUMBER_ASTERISK } from '../selectors';
import { checkIfRegNumberIsRequired } from '../validation';
export const getSearchItemLayout = ({ business_name, registration_number, address, credit_bureau_id, }) => `
<div
class='sprinque-businesses-item cursour-pointer'
data-name='${business_name}'
data-reg='${registration_number}'
data-creditBureauId='${credit_bureau_id}'
data-address='${JSON.stringify(address)}'
>
<b>${business_name}</b><br/>
<small>${Object.values(address)
.filter(item => item)
.join(', ')}</small><br/>
<small>${registration_number}</small>
</div>
`;
export const getSearchNotFoundItemLayout = () => `
<div class='sprinque-businesses-item cursour-pointer'>
<b>${i18next.t('business.cantFind')}</b><br/>
<small>${i18next.t('business.cantFindInstruction')}</small><br/>
</div>
`;
export const setIpAndLocationData = async (shadowRoot, buyerId) => {
var _a, _b;
// fetch IP address (used https://stackoverflow.com/a/35123097/6082084)
const response = await fetch('https://api.db-ip.com/v2/free/self', {
method: 'GET',
});
const { ipAddress, continentCode, continentName, countryCode, countryName, city } = (await response.json());
const companyNameInput = shadowRoot.querySelector(COMPANY_NAME);
if (ipAddress) {
companyNameInput.dataset.ipv4 = ipAddress;
}
if (continentCode) {
companyNameInput.dataset.continentCode = continentCode;
}
if (continentName) {
companyNameInput.dataset.continentName = continentName;
}
if (countryCode) {
companyNameInput.dataset.countryCode = countryCode;
// if buyerId is not provided - preselect country by IP.
const countryIsSupported = shadowRoot.querySelector(`#country-${countryCode}`);
if (!buyerId && countryIsSupported) {
// set country
shadowRoot.querySelector(COUNTRY_SELECT).value = countryCode;
// enable company name and reg number inputs
(_a = shadowRoot === null || shadowRoot === void 0 ? void 0 : shadowRoot.querySelector(COMPANY_NAME)) === null || _a === void 0 ? void 0 : _a.removeAttribute('disabled');
(_b = shadowRoot === null || shadowRoot === void 0 ? void 0 : shadowRoot.querySelector(REG_NUMBER)) === null || _b === void 0 ? void 0 : _b.removeAttribute('disabled');
// set reg number as required if needed
const isRegNumberRequired = checkIfRegNumberIsRequired(shadowRoot);
const regNumberAsterisk = shadowRoot === null || shadowRoot === void 0 ? void 0 : shadowRoot.querySelector(REG_NUMBER_ASTERISK);
regNumberAsterisk.innerHTML = isRegNumberRequired ? '*' : '';
}
}
if (countryName) {
companyNameInput.dataset.countryName = countryName;
}
if (city) {
companyNameInput.dataset.city = city;
}
};