Skip to content

Commit

Permalink
feat: quote deatil checkout and download pdf func
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 authored and b3aton committed Feb 3, 2023
1 parent 6525445 commit a0ef3ca
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 70 deletions.
12 changes: 1 addition & 11 deletions apps/storefront/src/pages/pdp/PDP.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
B3SStorage,
snackbar,
isAllRequiredOptionFilled,
getDefaultCurrencyInfo,
} from '@/utils'

import {
Expand Down Expand Up @@ -173,17 +174,6 @@ const PDP = ({
const sku = (document.querySelector('[data-product-sku]')?.innerHTML ?? '').trim()
const form = document.querySelector('form[data-cart-item-add]')

const getDefaultCurrencyInfo = () => {
const currencies = B3SStorage.get('currencies')
if (currencies) {
const {
currencies: currencyArr,
} = currencies
const defaultCurrency = currencyArr.find((currency: any) => currency.is_default)
return defaultCurrency
}
}

const {
currency_code: currencyCode,
} = getDefaultCurrencyInfo()
Expand Down
5 changes: 4 additions & 1 deletion apps/storefront/src/pages/quote/QuoteDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getB2BQuoteDetail,
getBcQuoteDetail,
exportB2BQuotePdf,
exportBcQuotePdf,
} from '@/shared/service/b2b'

import {
Expand Down Expand Up @@ -139,8 +140,9 @@ const QuoteDetail = () => {
},
}

const quotePdf = await exportB2BQuotePdf(data)
const fn = +role === 99 ? exportBcQuotePdf : exportB2BQuotePdf

const quotePdf = await fn(data)
if (quotePdf) {
window.open(`${quotePdf.quotePdfExport.url}`, '_blank')
}
Expand Down Expand Up @@ -346,6 +348,7 @@ const QuoteDetail = () => {
<QuoteDetailFooter
quoteId={quoteDetail.id}
quoteDate={quoteDetail?.createdAt?.toString()}
role={role}
/>
)
}
Expand Down
25 changes: 1 addition & 24 deletions apps/storefront/src/pages/quote/QuoteDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
storeHash,
addQuoteDraftProduce,
snackbar,
getDefaultCurrencyInfo,
} from '@/utils'

import {
Expand Down Expand Up @@ -305,19 +306,6 @@ const QuoteDraft = ({
setEdit(true)
}

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

const defaultCurrency = currencyArr.find((currency: CustomFieldItems) => currency.is_default)

return defaultCurrency
}
}

const {
token: currencyToken,
} = getDefaultCurrencyInfo()
Expand Down Expand Up @@ -467,17 +455,6 @@ const QuoteDraft = ({
return items
})

const getDefaultCurrencyInfo = () => {
const currencies = B3SStorage.get('currencies')
if (currencies) {
const {
currencies: currencyArr,
} = currencies
const defaultCurrency = currencyArr.find((currency: any) => currency.is_default)
return defaultCurrency
}
}

const currency = getDefaultCurrencyInfo()

const data = {
Expand Down
45 changes: 37 additions & 8 deletions apps/storefront/src/pages/quote/components/QuoteDetailFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {
useNavigate,
} from 'react-router-dom'
import {
Box,
Button,
Expand All @@ -10,18 +7,28 @@ import {
useMobile,
} from '@/hooks'

import {
b2bQuoteCheckout,
bcQuoteCheckout,
} from '@/shared/service/b2b'

import {
snackbar,
} from '@/utils'

interface QuoteDetailFooterProps {
quoteId: string,
quoteDate: string,
role: string | number,
}

const QuoteDetailFooter = (props: QuoteDetailFooterProps) => {
const {
quoteId,
quoteDate,
role,
} = props
const [isMobile] = useMobile()
const navigate = useNavigate()

const containerStyle = isMobile ? {
alignItems: 'flex-end',
Expand All @@ -30,6 +37,30 @@ const QuoteDetailFooter = (props: QuoteDetailFooterProps) => {
alignItems: 'center',
}

const handleQuoteCheckout = async () => {
try {
const fn = +role === 99 ? bcQuoteCheckout : b2bQuoteCheckout

const res = await fn({
id: +quoteId,
})

const {
quoteCheckout: {
quoteCheckout: {
checkoutUrl,
},
},
} = res
localStorage.setItem('quoteCheckoutId', quoteId)
localStorage.setItem('quoteDate', quoteDate)

window.location.href = checkoutUrl
} catch (err: any) {
snackbar.error(err)
}
}

return (
<Box
sx={{
Expand All @@ -41,7 +72,7 @@ const QuoteDetailFooter = (props: QuoteDetailFooterProps) => {
padding: '0.8rem 1rem',
height: 'auto',
display: 'flex',
zIndex: '10',
zIndex: '999',
justifyContent: isMobile ? 'center' : 'flex-end',
displayPrint: 'none',
...containerStyle,
Expand All @@ -50,9 +81,7 @@ const QuoteDetailFooter = (props: QuoteDetailFooterProps) => {
<Button
variant="contained"
onClick={() => {
navigate('/checkout')
localStorage.setItem('quoteCheckoutId', quoteId)
localStorage.setItem('quoteDate', quoteDate)
handleQuoteCheckout()
}}
sx={{
width: isMobile ? '100%' : 'auto',
Expand Down
60 changes: 51 additions & 9 deletions apps/storefront/src/pages/quote/components/QuoteDetailTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,40 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
key: 'Price',
title: 'Price',
render: (row) => {
const price = +row.basePrice
const {
basePrice,
discount,
} = row

const price = +basePrice
const isDiscount = +discount > 0
const offeredPrice = +basePrice - +discount

return (
<Typography
sx={{
padding: '12px 0',
}}
>
{`${currencyToken}${price.toFixed(2)}`}
</Typography>
<>
{
isDiscount && (
<Typography
sx={{
padding: '12px 0',
textDecoration: 'line-through',
}}
>
{`${currencyToken}${price.toFixed(2)}`}
</Typography>
)
}

<Typography
sx={{
padding: '12px 0',
color: isDiscount ? '#2E7D32' : '#212121',
}}
>
{`${currencyToken}${offeredPrice.toFixed(2)}`}
</Typography>

</>
)
},
width: '15%',
Expand All @@ -220,17 +244,35 @@ const QuoteDetailTable = (props: ShoppingDetailTableProps, ref: Ref<unknown>) =>
const {
basePrice,
quantity,
discount,
} = row
const total = +basePrice * +quantity
const offeredPrice = +basePrice - +discount

const isDiscount = +discount > 0
const totalWithDiscount = +offeredPrice * +quantity

return (
<Box>
{
isDiscount && (
<Typography
sx={{
padding: '12px 0',
textDecoration: 'line-through',
}}
>
{`${currencyToken}${total.toFixed(2)}`}
</Typography>
)
}
<Typography
sx={{
padding: '12px 0',
color: isDiscount ? '#2E7D32' : '#212121',
}}
>
{`${currencyToken}${total.toFixed(2)}`}
{`${currencyToken}${totalWithDiscount.toFixed(2)}`}
</Typography>
</Box>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ import {

import {
snackbar,
B3SStorage,
getDefaultCurrencyInfo,
} from '@/utils'

import {
conversionProductsList,
ShoppingListInfoProps,
CustomerInfoProps,
ListItemProps,
CurrencyProps,
SearchProps,
ProductsProps,
} from './shared/config'
Expand Down Expand Up @@ -108,19 +107,6 @@ const ShoppingListDetails = () => {

const isReadForApprove = shoppingListInfo?.status === 40 || shoppingListInfo?.status === 20

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

const defaultCurrency = currencyArr.find((currency: CurrencyProps) => currency.is_default)

return defaultCurrency
}
}

const {
currency_code: currencyCode,
token: currencyToken,
Expand Down
30 changes: 30 additions & 0 deletions apps/storefront/src/shared/global/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ export interface Country {
states: State[]
}

export interface channelCurrenciesProps {
channel_id: number,
default_currency: string,
enabled_currencies: Array<string>,
}

export interface currencyProps {
auto_update: boolean,
country_iso2: string,
currency_code: string,
currency_exchange_rate: string,
decimal_places: number,
decimal_token: string,
default_for_country_codes: Array<string>,
enabled: boolean,
id: string,
is_default: boolean,
is_transactional: boolean,
last_updated: string,
name: string,
thousands_token: string,
token: string,
token_location: string,
}

export interface GlobalState {
isCheckout: boolean,
isCloseGotoBCHome: boolean,
Expand Down Expand Up @@ -99,6 +124,10 @@ export interface GlobalState {
saveText?: string,
saveFn?: () => void,
},
currencies: {
channelCurrencies: channelCurrenciesProps,
currencies: currencyProps,
},
}

const role = B3SStorage.get('B3Role')
Expand Down Expand Up @@ -151,6 +180,7 @@ export const initState = {
message: '',
cancelText: 'Cancel',
},
currencies: B3SStorage.get('currencies') || {},
}

export interface GlobalAction {
Expand Down
27 changes: 27 additions & 0 deletions apps/storefront/src/shared/service/b2b/graphql/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,21 @@ const exportQuotePdf = (data: {
}
}`

const quoteCheckout = (data: {
id: number,
}) => `mutation{
quoteCheckout(
id: ${data.id},
storeHash: "${storeHash}",
) {
quoteCheckout {
checkoutUrl,
cartId,
cartUrl,
}
}
}`

export const getBCCustomerAddresses = (): CustomFieldItems => B3Request.graphqlProxyBC({
query: getCustomerAddresses(),
})
Expand Down Expand Up @@ -317,3 +332,15 @@ export const getBcQuoteDetail = (data: { id: number, date: string }): CustomFiel
export const exportB2BQuotePdf = (data: { quoteId: number, currency: object }): CustomFieldItems => B3Request.graphqlB2B({
query: exportQuotePdf(data),
})

export const exportBcQuotePdf = (data: { quoteId: number, currency: object }): CustomFieldItems => B3Request.graphqlProxyBC({
query: exportQuotePdf(data),
})

export const b2bQuoteCheckout = (data: { id: number }): CustomFieldItems => B3Request.graphqlB2B({
query: quoteCheckout(data),
})

export const bcQuoteCheckout = (data: { id: number }): CustomFieldItems => B3Request.graphqlProxyBC({
query: quoteCheckout(data),
})
Loading

0 comments on commit a0ef3ca

Please sign in to comment.