Skip to content

Commit

Permalink
Refactor locale currency
Browse files Browse the repository at this point in the history
  • Loading branch information
jrlarano committed Sep 16, 2024
1 parent 43a2dae commit b022d4a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/kits/core-ui/components/common/menu-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ const MenuPopup = ({
scriptEls,
publicationType,
configs,
sgnData,
sgnViewer,
template: templates.offerList
}).render()
Expand Down
9 changes: 7 additions & 2 deletions lib/kits/core-ui/components/common/offer-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
formatPrice,
pushQueryParam,
translate,
closeSidebar
closeSidebar,
getLocaleCode
} from '../helpers/component';
import {transformScriptData} from '../helpers/transformers';
import './offer-list.styl';
Expand Down Expand Up @@ -46,6 +47,7 @@ const OfferList = ({
scriptEls,
publicationType,
configs,
sgnData,
sgnViewer,
template
}: {
Expand All @@ -57,6 +59,7 @@ const OfferList = ({
id?: string;
businessId?: string;
};
sgnData: any;
sgnViewer?: Viewer;
template?: Element | null;
}) => {
Expand All @@ -72,7 +75,9 @@ const OfferList = ({
let searchKey = '';

const translations = {
localeCode: translate('locale_code'),
localeCode: scriptEls.localeCode
? translate('locale_code')
: getLocaleCode(sgnData?.details?.dealer?.country?.id),
currency: translate('publication_viewer_currency'),
searchText: translate('publication_viewer_search_text')
};
Expand Down
21 changes: 11 additions & 10 deletions lib/kits/core-ui/components/common/offer-overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getPubStateMessage,
parseDateStr,
updateShoppingList,
displayOfferMessage
displayOfferMessage,
getLocaleCode
} from '../helpers/component';
import {request, V2Offer} from '../../../core';
import * as clientLocalStorage from '../../../../storage/client-local';
Expand Down Expand Up @@ -122,7 +123,9 @@ const OfferOverview = ({
let container: HTMLDivElement | null = null;

const translations = {
localeCode: translate('locale_code'),
localeCode: scriptEls.localeCode
? translate('locale_code')
: getLocaleCode(sgnData?.details?.dealer?.country?.id),
currency: translate('publication_viewer_currency'),
addToShoppingList: translate('publication_viewer_add_to_shopping_list'),
visitWebshopLink: translate('publication_viewer_visit_webshop_link'),
Expand Down Expand Up @@ -185,19 +188,18 @@ const OfferOverview = ({
(offer) => offer.id === product.id
);

const price = useOfferPrice
? offer.price || offer.pricing.price
: product?.price;

const priceCurrency =
offer.currency_code || offer.pricing?.currency || currency;

return {
...product,
link: product.link || offer.webshop_link,
formattedPrice: formatPrice(
useOfferPrice
? offer.price || offer.pricing.price
: product?.price,
localeCode,
priceCurrency
),
price,
formattedPrice: formatPrice(price, localeCode, priceCurrency),
currency: priceCurrency,
quantity: matchingOffer ? matchingOffer.quantity : 0
};
Expand Down Expand Up @@ -339,7 +341,6 @@ const OfferOverview = ({

return {
...offer,
// products: products,
price: formatPrice(
offer?.pricing?.price,
localeCode,
Expand Down
9 changes: 6 additions & 3 deletions lib/kits/core-ui/components/common/shopping-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
createModal,
formatPrice,
translate,
updateShoppingList
updateShoppingList,
getLocaleCode
} from '../helpers/component';
import './shopping-list.styl';

Expand Down Expand Up @@ -113,13 +114,15 @@ const defaultTemplate = `\
</div>\
`;

const ShoppingList = ({template}) => {
const ShoppingList = ({template, scriptEls, sgnData}) => {
template = template?.innerHTML || defaultTemplate;
const shoppingListBtn = document.querySelector('.sgn__offer-shopping');
let container: HTMLDivElement | null = null;

const translations = {
localeCode: translate('locale_code'),
localeCode: scriptEls.localeCode
? translate('locale_code')
: getLocaleCode(sgnData?.details?.dealer?.country?.id),
shoppingListLabel: translate('publication_viewer_shopping_list_label'),
currency: translate('publication_viewer_currency'),
deleteCrossedOutButton: translate(
Expand Down
12 changes: 12 additions & 0 deletions lib/kits/core-ui/components/helpers/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const formatPrice = (price, localeCode = 'en_US', currency = 'USD') => {
.format(price)
.replace(currency, '')
.replace('kr.', '')
.replace('kr', '')
.trim();
};

Expand Down Expand Up @@ -437,3 +438,14 @@ export const displayOfferMessage = (viewId, message) => {
}, 1500);
}
};

export const getLocaleCode = (countryId: string): string => {
const countryLocaleMap: {[key: string]: string} = {
US: 'en_US',
DK: 'da_DK',
SE: 'sv_SE',
NO: 'nb_NO'
};

return countryLocaleMap[countryId] || '';
};
6 changes: 5 additions & 1 deletion lib/kits/core-ui/incito-publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ const IncitoPublication = (
};

const renderShoppingList = () =>
ShoppingList({template: customTemplates.shoppingList}).render();
ShoppingList({
template: customTemplates.shoppingList,
scriptEls,
sgnData
}).render();

const renderSectionList = async () =>
document.querySelector('.sgn__sidebar-content-container')?.appendChild(
Expand Down
6 changes: 5 additions & 1 deletion lib/kits/core-ui/paged-publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ const PagedPublication = (
);

const renderShoppingList = () =>
ShoppingList({template: customTemplates.shoppingList}).render();
ShoppingList({
template: customTemplates.shoppingList,
scriptEls,
sgnData
}).render();

const renderMenuPopup = () =>
document
Expand Down

0 comments on commit b022d4a

Please sign in to comment.