Skip to content

Commit

Permalink
fix: display price by current active curreny
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlLiu2023 authored and BrianJiang2021 committed Apr 26, 2023
1 parent 6e083fa commit cbd39d2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
21 changes: 21 additions & 0 deletions apps/storefront/src/shared/service/bc/graphql/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import B3Request from '../../request/b3Fetch'

const BcCurrencies = () => `{
site{
currencies{
edges{
node{
isActive
entityId
}
}
}
}
}`

const getActiveBcCurrency = (): CustomFieldItems =>
B3Request.graphqlBC({
query: BcCurrencies(),
})

export default getActiveBcCurrency
1 change: 1 addition & 0 deletions apps/storefront/src/shared/service/bc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './api/cart'
export * from './api/login'
export { default as getBCProductVariantId } from './api/product'
export { default as getBCRegisterCustomFields } from './api/register'
export { default as getActiveBcCurrency } from './graphql/currency'
export * from './graphql/login'
export { default as getCustomerInfo } from './graphql/user'
21 changes: 12 additions & 9 deletions apps/storefront/src/utils/b3CurrencyFormat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getDefaultCurrencyInfo from './currencyUtils'
import { getActiveCurrencyInfo } from './currencyUtils'

interface MoneyFormat {
currency_location: 'left' | 'right'
Expand All @@ -10,16 +10,19 @@ interface MoneyFormat {
}

const currencyFormat = (price: string | number, showCurrencyToken = true) => {
const currentCurrency = getDefaultCurrencyInfo()
const currentCurrency = getActiveCurrencyInfo()

const moneyFormat: MoneyFormat = {
currency_location: currentCurrency?.token_location || 'left',
currency_token: currentCurrency?.token || '$',
decimal_token: currentCurrency?.decimal_token || '.',
decimal_places: currentCurrency?.decimal_places || 2,
thousands_token: currentCurrency?.thousands_token || ',',
currency_location: currentCurrency.token_location || 'left',
currency_token: currentCurrency.token || '$',
decimal_token: currentCurrency.decimal_token || '.',
decimal_places:
currentCurrency.decimal_places === 0
? 0
: currentCurrency.decimal_places || 2,
thousands_token: currentCurrency.thousands_token || ',',
currency_exchange_rate:
currentCurrency?.currency_exchange_rate || '1.0000000000',
currentCurrency.currency_exchange_rate || '1.0000000000',
}

try {
Expand All @@ -31,7 +34,7 @@ const currencyFormat = (price: string | number, showCurrencyToken = true) => {
const newPrice = `${integerPart.replace(
/\B(?=(\d{3})+(?!\d))/g,
moneyFormat.thousands_token
)}${moneyFormat.decimal_token}${decimalPart}`
)}${decimalPart ? `${moneyFormat.decimal_token}${decimalPart}` : ''}`
const priceStr =
moneyFormat.currency_location === 'left'
? `${showCurrencyToken ? moneyFormat.currency_token : ''}${newPrice}`
Expand Down
37 changes: 30 additions & 7 deletions apps/storefront/src/utils/currencyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,42 @@ interface CurrencyProps {
token_location: 'left' | 'right'
}

interface ActiveCurrencyProps {
node: {
entityId: number
isActive: boolean
}
}

const getActiveCurrencyInfo = () => {
const { currencies } = B3SStorage.get('currencies')
const { node: activeCurrencyObj }: ActiveCurrencyProps =
B3SStorage.get('activeCurrency')
const activeCurrency: CurrencyProps = currencies.find(
(currency: CurrencyProps) => +currency.id === activeCurrencyObj.entityId
)

return activeCurrency
}

const getDefaultCurrencyInfo = () => {
const currencies = B3SStorage.get('currencies')
if (currencies) {
const { currencies: currencyArr } = currencies
const { currencies: currencyArr } = currencies

const activeCurrency = getActiveCurrencyInfo()

const defaultCurrency = currencyArr.find(
let defaultCurrency: CurrencyProps
if (activeCurrency.enabled) {
defaultCurrency = currencyArr.find(
(currency: CurrencyProps) => +currency.id === +activeCurrency.id
)
} else {
defaultCurrency = currencyArr.find(
(currency: CurrencyProps) => currency.is_default
)

return defaultCurrency
}

return undefined
return defaultCurrency
}

export default getDefaultCurrencyInfo
export { getActiveCurrencyInfo, getDefaultCurrencyInfo }
3 changes: 2 additions & 1 deletion apps/storefront/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { B3LStorage, B3SStorage } from './b3Storage'
import { globalSnackbar, snackbar } from './b3Tip'
import getCookie from './b3utils'
import { captchaSetkey, storeHash } from './basicConfig'
import getDefaultCurrencyInfo from './currencyUtils'
import { getActiveCurrencyInfo, getDefaultCurrencyInfo } from './currencyUtils'
import {
convertArrayToGraphql,
convertObjectToGraphql,
Expand Down Expand Up @@ -58,6 +58,7 @@ export {
displayExtendedFormat,
displayFormat,
distanceDay,
getActiveCurrencyInfo,
getCookie,
getCurrentCustomerInfo,
getCurrentJwt,
Expand Down
22 changes: 20 additions & 2 deletions apps/storefront/src/utils/storefrontConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getStorefrontConfigs,
getTaxZoneRates,
} from '@/shared/service/b2b'
import { getActiveBcCurrency } from '@/shared/service/bc'
import { setTaxZoneRates, store } from '@/store'
import { B3SStorage } from '@/utils'

Expand Down Expand Up @@ -36,6 +37,13 @@ interface StoreforntKeysProps {
name: string
}

interface CurrencyNodeProps {
node: {
entityId: number
isActive: boolean
}
}

const storeforntKeys: StoreforntKeysProps[] = [
{
key: 'quote_on_product_page',
Expand Down Expand Up @@ -168,10 +176,20 @@ const setStorefrontConfig = async (
const {
storefrontConfig: { config: storefrontConfig },
} = await getStorefrontConfig()

const { currencies } = await getCurrencies(currentChannelId)
B3SStorage.set('currencies', currencies)
const {
data: {
site: {
currencies: { edges },
},
},
} = await getActiveBcCurrency()

B3SStorage.set('currencies', currencies)
B3SStorage.set(
'activeCurrency',
edges.find((item: CurrencyNodeProps) => item.node.isActive)
)
dispatch({
type: 'common',
payload: {
Expand Down

0 comments on commit cbd39d2

Please sign in to comment.