chore: Refactor urlParamsHelper.js (#5639)
This commit is contained in:
parent
6bc34db932
commit
c542d2e0ff
2 changed files with 12 additions and 16 deletions
|
@ -31,7 +31,7 @@ describe('#getLocale', () => {
|
||||||
expect(getLocale('?test=1&cw_conv=2&website_token=3&locale=fr')).toEqual(
|
expect(getLocale('?test=1&cw_conv=2&website_token=3&locale=fr')).toEqual(
|
||||||
'fr'
|
'fr'
|
||||||
);
|
);
|
||||||
expect(getLocale('')).toEqual(undefined);
|
expect(getLocale('')).toEqual(null);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,13 @@
|
||||||
export const buildSearchParamsWithLocale = search => {
|
export const buildSearchParamsWithLocale = search => {
|
||||||
const locale = window.WOOT_WIDGET.$root.$i18n.locale;
|
const locale = window.WOOT_WIDGET.$root.$i18n.locale;
|
||||||
if (search) {
|
const params = new URLSearchParams(search);
|
||||||
search = `${search}&locale=${locale}`;
|
params.append('locale', locale);
|
||||||
} else {
|
|
||||||
search = `?locale=${locale}`;
|
return `?${params}`;
|
||||||
}
|
|
||||||
return search;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getLocale = (search = '') => {
|
export const getLocale = (search = '') => {
|
||||||
const searchParamKeyValuePairs = search.split('&');
|
return new URLSearchParams(search).get('locale');
|
||||||
return searchParamKeyValuePairs.reduce((acc, keyValuePair) => {
|
|
||||||
const [key, value] = keyValuePair.split('=');
|
|
||||||
if (key === 'locale') {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, undefined);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildPopoutURL = ({
|
export const buildPopoutURL = ({
|
||||||
|
@ -25,5 +16,10 @@ export const buildPopoutURL = ({
|
||||||
websiteToken,
|
websiteToken,
|
||||||
locale,
|
locale,
|
||||||
}) => {
|
}) => {
|
||||||
return `${origin}/widget?cw_conversation=${conversationCookie}&website_token=${websiteToken}&locale=${locale}`;
|
const popoutUrl = new URL('/widget', origin);
|
||||||
|
popoutUrl.searchParams.append('cw_conversation', conversationCookie);
|
||||||
|
popoutUrl.searchParams.append('website_token', websiteToken);
|
||||||
|
popoutUrl.searchParams.append('locale', locale);
|
||||||
|
|
||||||
|
return popoutUrl.toString();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue