From b022d4abad8055e71e52229f1cf65a970b8aebac Mon Sep 17 00:00:00 2001 From: Jevy Larano <32891190+jrlarano@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:33:25 +0800 Subject: [PATCH] Refactor locale currency --- .../core-ui/components/common/menu-popup.ts | 1 + .../core-ui/components/common/offer-list.ts | 9 ++++++-- .../components/common/offer-overview.ts | 21 ++++++++++--------- .../components/common/shopping-list.ts | 9 +++++--- .../core-ui/components/helpers/component.ts | 12 +++++++++++ lib/kits/core-ui/incito-publication.ts | 6 +++++- lib/kits/core-ui/paged-publication.ts | 6 +++++- 7 files changed, 47 insertions(+), 17 deletions(-) diff --git a/lib/kits/core-ui/components/common/menu-popup.ts b/lib/kits/core-ui/components/common/menu-popup.ts index 4e8ba282..c551191d 100644 --- a/lib/kits/core-ui/components/common/menu-popup.ts +++ b/lib/kits/core-ui/components/common/menu-popup.ts @@ -227,6 +227,7 @@ const MenuPopup = ({ scriptEls, publicationType, configs, + sgnData, sgnViewer, template: templates.offerList }).render() diff --git a/lib/kits/core-ui/components/common/offer-list.ts b/lib/kits/core-ui/components/common/offer-list.ts index 83be181f..270e5404 100644 --- a/lib/kits/core-ui/components/common/offer-list.ts +++ b/lib/kits/core-ui/components/common/offer-list.ts @@ -6,7 +6,8 @@ import { formatPrice, pushQueryParam, translate, - closeSidebar + closeSidebar, + getLocaleCode } from '../helpers/component'; import {transformScriptData} from '../helpers/transformers'; import './offer-list.styl'; @@ -46,6 +47,7 @@ const OfferList = ({ scriptEls, publicationType, configs, + sgnData, sgnViewer, template }: { @@ -57,6 +59,7 @@ const OfferList = ({ id?: string; businessId?: string; }; + sgnData: any; sgnViewer?: Viewer; template?: Element | null; }) => { @@ -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') }; diff --git a/lib/kits/core-ui/components/common/offer-overview.ts b/lib/kits/core-ui/components/common/offer-overview.ts index 814c8f98..82146eb1 100644 --- a/lib/kits/core-ui/components/common/offer-overview.ts +++ b/lib/kits/core-ui/components/common/offer-overview.ts @@ -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'; @@ -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'), @@ -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 }; @@ -339,7 +341,6 @@ const OfferOverview = ({ return { ...offer, - // products: products, price: formatPrice( offer?.pricing?.price, localeCode, diff --git a/lib/kits/core-ui/components/common/shopping-list.ts b/lib/kits/core-ui/components/common/shopping-list.ts index 6ec43b6f..7c61205b 100644 --- a/lib/kits/core-ui/components/common/shopping-list.ts +++ b/lib/kits/core-ui/components/common/shopping-list.ts @@ -4,7 +4,8 @@ import { createModal, formatPrice, translate, - updateShoppingList + updateShoppingList, + getLocaleCode } from '../helpers/component'; import './shopping-list.styl'; @@ -113,13 +114,15 @@ const defaultTemplate = `\ \ `; -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( diff --git a/lib/kits/core-ui/components/helpers/component.ts b/lib/kits/core-ui/components/helpers/component.ts index d0710fdd..a0858459 100644 --- a/lib/kits/core-ui/components/helpers/component.ts +++ b/lib/kits/core-ui/components/helpers/component.ts @@ -67,6 +67,7 @@ export const formatPrice = (price, localeCode = 'en_US', currency = 'USD') => { .format(price) .replace(currency, '') .replace('kr.', '') + .replace('kr', '') .trim(); }; @@ -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] || ''; +}; diff --git a/lib/kits/core-ui/incito-publication.ts b/lib/kits/core-ui/incito-publication.ts index d97727f5..cf335485 100644 --- a/lib/kits/core-ui/incito-publication.ts +++ b/lib/kits/core-ui/incito-publication.ts @@ -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( diff --git a/lib/kits/core-ui/paged-publication.ts b/lib/kits/core-ui/paged-publication.ts index f758e2e7..41c47966 100644 --- a/lib/kits/core-ui/paged-publication.ts +++ b/lib/kits/core-ui/paged-publication.ts @@ -131,7 +131,11 @@ const PagedPublication = ( ); const renderShoppingList = () => - ShoppingList({template: customTemplates.shoppingList}).render(); + ShoppingList({ + template: customTemplates.shoppingList, + scriptEls, + sgnData + }).render(); const renderMenuPopup = () => document