Skip to content

Commit

Permalink
feat: b2b print invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 committed Mar 20, 2023
1 parent e7dc741 commit 8ad65db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
12 changes: 6 additions & 6 deletions apps/storefront/src/pages/orderDetail/components/OrderAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
useContext,
Fragment,
} from 'react'

import {
useNavigate,
} from 'react-router-dom'
throttle,
} from 'lodash'

import {
Card,
Expand Down Expand Up @@ -40,6 +41,7 @@ import {

import {
snackbar,
b2bPrintInvoice,
} from '@/utils'

const OrderActionContainer = styled('div')(() => ({}))
Expand Down Expand Up @@ -153,8 +155,6 @@ const OrderCard = (props: OrderCardProps) => {
},
} = useContext(GlobaledContext)

const navigate = useNavigate()

const dialogData = [
{
dialogTitle: 'Re-order',
Expand Down Expand Up @@ -193,7 +193,7 @@ const OrderCard = (props: OrderCardProps) => {

const handleOpenDialog = (name: string) => {
if (name === 'viewInvoice') {
navigate('/invoiceDetail/1')
b2bPrintInvoice(orderId, 'b2b_print_invoice')
} else if (name === 'printInvoice') {
window.open(`/account.php?action=print_invoice&order_id=${orderId}`)
} else {
Expand Down Expand Up @@ -269,7 +269,7 @@ const OrderCard = (props: OrderCardProps) => {
key={button.key}
name={button.name}
variant={button.variant}
onClick={() => { handleOpenDialog(button.name) }}
onClick={throttle(() => { handleOpenDialog(button.name) }, 2000)}
>
{button.value}
</Button>
Expand Down
33 changes: 33 additions & 0 deletions apps/storefront/src/utils/b3PrintInvoice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const bindDom = (html: string, domId: string) => {
let iframeDom = document.getElementById(domId) as HTMLIFrameElement | null
if (!iframeDom) {
iframeDom = document.createElement('iframe')
iframeDom.src = 'about:blank'
iframeDom.id = domId
iframeDom.style.display = 'none'
document.body.appendChild(iframeDom)
}
const iframeDoc = iframeDom.contentWindow?.document
if (iframeDoc) {
iframeDoc.open()
iframeDoc.write(html)
iframeDoc.close()
}
iframeDom.style.display = 'block'
}

export const b2bPrintInvoice = async (orderId: string, domId: string) => {
await fetch(`/account.php?action=print_invoice&order_id=${orderId}`)
.then((response: Response) => {
if (response.ok) {
return response.text()
}
throw new Error('Network response was not ok.')
})
.then((html: string) => {
bindDom(html, domId)
})
.catch((error: Error) => {
console.error('Error Invoice:', error)
})
}
5 changes: 5 additions & 0 deletions apps/storefront/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ import {
showPageMask,
} from './b3PageMask'

import {
b2bPrintInvoice,
} from './b3PrintInvoice'

export {
addQuoteDraftProduce,
getModifiersPrice,
Expand Down Expand Up @@ -102,4 +106,5 @@ export {
getSearchVal,
getDefaultCurrencyInfo,
showPageMask,
b2bPrintInvoice,
}

0 comments on commit 8ad65db

Please sign in to comment.