Skip to content

Commit

Permalink
feat(BUN-2176): new storeConfig slice with currencies (#976)
Browse files Browse the repository at this point in the history
* feat(BUN-2176): new storeConfig slice with currencies

* fix(BUN-2176): typo in  storeConfig slice

* fix(BUN-2176): typo in  storeConfig reducer

* fix(BUN-2176): typo in  storeConfig selector

* feat(BUN-2176): enteredInclusiveTax callers refac

* fix(BUN-2176): improving readability

* fix(BUN-2176): removing log and shadowed var

* fix(BUN-2176): separating types
  • Loading branch information
bc-victor authored Mar 27, 2024
1 parent c076e51 commit fa1034f
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 146 deletions.
7 changes: 2 additions & 5 deletions apps/storefront/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
}
},
{
"files": [
"src/hooks/dom/*.ts",
"src/hooks/*.ts"
],
"files": ["src/hooks/dom/*.ts", "src/hooks/*.ts"],
"rules": {
"react-hooks/exhaustive-deps": 0
}
Expand Down Expand Up @@ -131,7 +128,7 @@
"rules": { "react/jsx-wrap-multilines": 0 }
},
{
"files": ["src/**/*.{ts,tsx}"],
"files": ["src/**/*.{ts,tsx}", "tests/**/*.{ts,tsx}"],
"rules": { "import/extensions": 0 }
},
{
Expand Down
7 changes: 5 additions & 2 deletions apps/storefront/src/components/upload/B3Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useState,
} from 'react'
import { DropzoneArea } from 'react-mui-dropzone'
import { useSelector } from 'react-redux'
import styled from '@emotion/styled'
import InsertDriveFile from '@mui/icons-material/InsertDriveFile'
import { Alert, Box, Link, useTheme } from '@mui/material'
Expand All @@ -21,7 +22,8 @@ import {
BcProductsBulkUploadCSV,
guestProductsBulkUploadCSV,
} from '@/shared/service/b2b'
import { b2bLogger, getDefaultCurrencyInfo } from '@/utils'
import { defaultCurrencyCodeSelector } from '@/store'
import { b2bLogger } from '@/utils'

import B3Dialog from '../B3Dialog'

Expand Down Expand Up @@ -91,7 +93,8 @@ export default function B3Upload(props: B3UploadProps) {
const [fileName, setFileName] = useState('')
const [fileErrorText, setFileErrorText] = useState('')

const { currency_code: currencyCode } = getDefaultCurrencyInfo()
const currency = useSelector(defaultCurrencyCodeSelector)
const { currency_code: currencyCode } = currency

const getRejectMessage = (
rejectedFile: File,
Expand Down
28 changes: 7 additions & 21 deletions apps/storefront/src/pages/invoice/Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
import { InvoiceList, InvoiceListNode } from '@/types/invoice'
import {
b2bLogger,
B3SStorage,
currencyFormat,
currencyFormatInfo,
displayFormat,
getUTCTimestamp,
handleGetCorrespondingCurrencyToken,
snackbar,
} from '@/utils'

Expand Down Expand Up @@ -80,7 +80,6 @@ function Invoice() {
const [isMobile] = useMobile()
const paginationTableRef = useRef<PaginationTableRefProps | null>(null)

const allCurrencies = B3SStorage.get('currencies')
const { decimal_places: decimalPlaces = 2 } = currencyFormatInfo()

const [isRequestLoading, setIsRequestLoading] = useState<boolean>(false)
Expand Down Expand Up @@ -152,21 +151,6 @@ function Invoice() {
setFilterLists(copyCacheFilterList)
}

const handleGetCorrespondingCurrency = (code: string) => {
const { currencies: currencyArr } = allCurrencies
let token = '$'
const correspondingCurrency =
currencyArr.find(
(currency: CustomFieldItems) => currency.currency_code === code
) || {}

if (correspondingCurrency) {
token = correspondingCurrency.token
}

return token
}

const handleStatisticsInvoiceAmount = async () => {
try {
setIsRequestLoading(true)
Expand Down Expand Up @@ -569,7 +553,7 @@ function Invoice() {
const { originalBalance } = item
const originalAmount = (+originalBalance.value).toFixed(decimalPlaces)

const token = handleGetCorrespondingCurrency(originalBalance.code)
const token = handleGetCorrespondingCurrencyToken(originalBalance.code)

return `${token}${originalAmount || 0}`
},
Expand All @@ -583,7 +567,7 @@ function Invoice() {
const { openBalance } = item

const openAmount = (+openBalance.value).toFixed(decimalPlaces)
const token = handleGetCorrespondingCurrency(openBalance.code)
const token = handleGetCorrespondingCurrencyToken(openBalance.code)

return `${token}${openAmount || 0}`
},
Expand Down Expand Up @@ -632,7 +616,7 @@ function Invoice() {
position="start"
sx={{ padding: '8px 0', marginTop: '0 !important' }}
>
{handleGetCorrespondingCurrency(currentCode)}
{handleGetCorrespondingCurrencyToken(currentCode)}
</InputAdornment>
),
}}
Expand Down Expand Up @@ -870,7 +854,9 @@ function Invoice() {
setInvoiceId={setCurrentInvoiceId}
handleOpenHistoryModal={setIsOpenHistorys}
selectedPay={selectedPay}
handleGetCorrespondingCurrency={handleGetCorrespondingCurrency}
handleGetCorrespondingCurrency={
handleGetCorrespondingCurrencyToken
}
addBottom={list.length - 1 === index}
/>
)}
Expand Down
22 changes: 3 additions & 19 deletions apps/storefront/src/pages/invoice/components/InvoiceFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
BcCartDataLineItem,
InvoiceListNode,
} from '@/types/invoice'
import { B3SStorage, snackbar } from '@/utils'
import { handleGetCorrespondingCurrencyToken, snackbar } from '@/utils'

import { gotoInvoiceCheckoutUrl } from '../utils/payment'

Expand All @@ -22,7 +22,6 @@ interface InvoiceFooterProps {
function InvoiceFooter(props: InvoiceFooterProps) {
const platform = useAppSelector(({ global }) => global.storeInfo.platform)
const b3Lang = useB3Lang()
const allCurrencies = B3SStorage.get('currencies')
const [isMobile] = useMobile()
const [selectedAccount, setSelectedAccount] = useState<number | string>(0)
const [currentToken, setCurrentToken] = useState<string>('$')
Expand Down Expand Up @@ -82,21 +81,6 @@ function InvoiceFooter(props: InvoiceFooterProps) {

useEffect(() => {
if (selectedPay.length > 0) {
const handleGetCorrespondingCurrency = (code: string) => {
const { currencies: currencyArr } = allCurrencies
let token = '$'
const correspondingCurrency =
currencyArr.find(
(currency: CustomFieldItems) => currency.currency_code === code
) || {}

if (correspondingCurrency) {
token = correspondingCurrency.token
}

return token
}

const handleStatisticsInvoiceAmount = (checkedArr: CustomFieldItems) => {
let amount = 0

Expand All @@ -113,11 +97,11 @@ function InvoiceFooter(props: InvoiceFooterProps) {
node: { openBalance },
} = selectedPay[0]

const token = handleGetCorrespondingCurrency(openBalance.code)
const token = handleGetCorrespondingCurrencyToken(openBalance.code)
setCurrentToken(token)
handleStatisticsInvoiceAmount(selectedPay)
}
}, [allCurrencies, decimalPlaces, selectedPay])
}, [decimalPlaces, selectedPay])

return (
<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useRef,
useState,
} from 'react'
import { useSelector } from 'react-redux'
import { useB3Lang } from '@b3/lang'
import { Box, styled, TextField, Typography } from '@mui/material'

Expand All @@ -21,11 +22,11 @@ import {
searchB2BProducts,
searchBcProducts,
} from '@/shared/service/b2b'
import { defaultCurrencyCodeSelector } from '@/store'
import {
currencyFormat,
displayFormat,
distanceDay,
getDefaultCurrencyInfo,
getProductPriceIncTax,
snackbar,
} from '@/utils'
Expand Down Expand Up @@ -162,7 +163,8 @@ function QuickorderTable({

const b3Lang = useB3Lang()

const { currency_code: currencyCode } = getDefaultCurrencyInfo()
const currency = useSelector(defaultCurrencyCodeSelector)
const { currency_code: currencyCode } = currency

const handleGetProductsById = async (listProducts: ListItemProps[]) => {
if (listProducts.length > 0) {
Expand Down
17 changes: 8 additions & 9 deletions apps/storefront/src/pages/quote/QuoteDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
searchB2BProducts,
searchBcProducts,
} from '@/shared/service/b2b'
import { TaxZoneRates, useAppSelector } from '@/store'
import {
getDefaultCurrencyInfo,
getSearchVal,
getVariantInfoOOSAndPurchase,
snackbar,
} from '@/utils'
defaultCurrencyCodeSelector,
TaxZoneRates,
useAppSelector,
} from '@/store'
import { Currency } from '@/types'
import { getSearchVal, getVariantInfoOOSAndPurchase, snackbar } from '@/utils'
import { conversionProductsList } from '@/utils/b3Product/shared/config'

import Message from './components/Message'
Expand Down Expand Up @@ -69,16 +69,14 @@ function QuoteDetail() {
})
const [isRequestLoading, setIsRequestLoading] = useState(false)
const [isShowFooter, setIsShowFooter] = useState(false)
const { currency_code: currencyCode } = getDefaultCurrencyInfo()

const [quoteDetailTax, setQuoteDetailTax] = useState(0)

const [noBuyerProductName, setNoBuyerProductName] = useState({
oos: '',
nonPurchasable: '',
})

const location = useLocation()
const currency = useAppSelector(defaultCurrencyCodeSelector)
const taxZoneRates = useAppSelector(({ global }) => global.taxZoneRates)
const enteredInclusiveTax = useAppSelector(
({ global }) => global.enteredInclusive
Expand Down Expand Up @@ -200,6 +198,7 @@ function QuoteDetail() {
const getProducts = isB2BUser ? searchB2BProducts : searchBcProducts

try {
const { currency_code: currencyCode } = currency as Currency
const { productsSearch } = await getProducts({
productIds,
currencyCode,
Expand Down
20 changes: 9 additions & 11 deletions apps/storefront/src/pages/quote/components/QuoteDetailSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ interface QuoteDetailSummaryProps {
isHideQuoteCheckout: boolean
}

export default function QuoteDetailSummary(props: QuoteDetailSummaryProps) {
export default function QuoteDetailSummary({
quoteSummary: { originalSubtotal, discount, tax, shipping, totalAmount },
quoteDetailTax = 0,
status,
quoteDetail,
isHideQuoteCheckout,
}: QuoteDetailSummaryProps) {
const b3Lang = useB3Lang()
const {
quoteSummary: { originalSubtotal, discount, tax, shipping, totalAmount },
quoteDetailTax = 0,
status,
quoteDetail,
isHideQuoteCheckout,
} = props
const enteredInclusiveTax = useAppSelector(
({ global }) => global.enteredInclusive
)
const showInclusiveTaxPrice = useAppSelector(
({ global }) => global.showInclusiveTaxPrice
)

const subtotalPrice = +originalSubtotal
const quotedSubtotal = +originalSubtotal - +discount

const getCurrentPrice = (price: number, quoteDetailTax: number) => {
if (enteredInclusiveTax) {
return showInclusiveTaxPrice ? price : price - quoteDetailTax
Expand Down Expand Up @@ -102,6 +98,8 @@ export default function QuoteDetailSummary(props: QuoteDetailSummaryProps) {
return price
}

const subtotalPrice = +originalSubtotal
const quotedSubtotal = +originalSubtotal - +discount
return (
<Card>
<CardContent>
Expand Down
4 changes: 2 additions & 2 deletions apps/storefront/src/shared/global/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ export const initState = {
shoppingListEnabled: false,
registerEnabled: true,
quoteConfig: [],
currencies: B3SStorage.get('currencies') || {},
currencies: {},
openAPPParams: {
quoteBtn: '',
shoppingListBtn: '',
},
showPageMask: false,
timeFormat: B3SStorage.get('timeFormat') || {},
enteredInclusiveTax: B3SStorage.get('enteredInclusiveTax') || false,
enteredInclusiveTax: false,
blockPendingAccountOrderCreation:
B3SStorage.get('blockPendingAccountOrderCreation') || true,
quoteDetailHasNewMessages: false,
Expand Down
2 changes: 2 additions & 0 deletions apps/storefront/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { configureStore } from '@reduxjs/toolkit'

import global from './slices/global'
import lang from './slices/lang'
import storeConfigs from './slices/storeConfigs'
import theme from './slices/theme'

export const middlewareOptions = {
Expand All @@ -24,6 +25,7 @@ export const store = configureStore({
reducer: {
global,
lang,
storeConfigs,
theme,
},
middleware: (getDefaultMiddleware) => getDefaultMiddleware(middlewareOptions),
Expand Down
13 changes: 11 additions & 2 deletions apps/storefront/src/store/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { createSelector } from '@reduxjs/toolkit'

import { Currency } from '@/types'

import { RootState } from './reducer'

const themeSelector = (state: RootState) => state.theme
const storeConfigSelector = (state: RootState) => state.storeConfigs

// More selectors will be added
// eslint-disable-next-line
export const themeFrameSelector = createSelector(
themeSelector,
(theme) => theme.themeFrame
)

export const defaultCurrencyCodeSelector = createSelector(
storeConfigSelector,
(storeConfigs) =>
storeConfigs.currencies?.currencies.find(
(currency: Currency) => currency.is_default
)
)
Loading

0 comments on commit fa1034f

Please sign in to comment.