Skip to content

Commit

Permalink
feat: quote print and download pdf optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianJiang2021 authored and libruce committed Jan 9, 2024
1 parent 818b0a8 commit 436a6d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
60 changes: 24 additions & 36 deletions apps/storefront/src/pages/quote/QuoteDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,37 +233,39 @@ function QuoteDetail() {
}
}

const fetchPdfUrl = async () => {
const fetchPdfUrl = async (bool: boolean) => {
setIsRequestLoading(true)
const {
id,
currency: { currencyExchangeRate, token },
} = quoteDetail
const { id, createdAt } = quoteDetail
try {
const data = {
quoteId: +id,
currency: {
currencyExchangeRate,
token,
},
createdAt,
isPreview: bool,
lang: 'en',
}

const fn = +role === 99 ? exportBcQuotePdf : exportB2BQuotePdf

const quotePdf = await fn(data)

if (quotePdf) {
return quotePdf.quotePdfExport.url
return {
url: quotePdf.quoteFrontendPdf.url,
content: quotePdf.quoteFrontendPdf.content,
}
}
} catch (err: any) {
snackbar.error(err)
}
return undefined
return {
url: '',
content: '',
}
}

const exportPdf = async () => {
try {
const quotePdfUrl = await fetchPdfUrl()
const { url: quotePdfUrl } = await fetchPdfUrl(false)
if (quotePdfUrl) {
window.open(`${quotePdfUrl}`, '_blank')
}
Expand All @@ -276,30 +278,16 @@ function QuoteDetail() {

const printQuote = async () => {
try {
const quotePdfUrl = await fetchPdfUrl()

if (quotePdfUrl) {
const xhr = new XMLHttpRequest()
xhr.open('GET', quotePdfUrl, true)
xhr.responseType = 'arraybuffer'
xhr.onload = () => {
if (xhr.status === 200) {
const pdfData = new Uint8Array(xhr.response)
const pdfBlob = new Blob([pdfData], {
type: 'application/pdf',
})
const pdfUrl = URL.createObjectURL(pdfBlob)
const iframe = document.createElement('iframe')
iframe.setAttribute('src', pdfUrl)
iframe.setAttribute('style', 'display:none;')
document.getElementById('bundle-container')?.appendChild(iframe)
setIsRequestLoading(false)
iframe?.contentWindow?.print()
}
}

xhr.send()
}
const { content } = await fetchPdfUrl(true)

const iframe = document.createElement('iframe')
iframe.setAttribute('style', 'display:none;')
document.getElementById('bundle-container')?.appendChild(iframe)
iframe.contentDocument?.open()
iframe.contentDocument?.write(content)
iframe.contentDocument?.close()
setIsRequestLoading(false)
iframe.contentWindow?.print()
} catch (err: any) {
snackbar.error(err)
}
Expand Down
19 changes: 14 additions & 5 deletions apps/storefront/src/shared/service/b2b/graphql/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,19 @@ const getQuoteInfo = (data: { id: number; date: string }) => `{

const exportQuotePdf = (data: {
quoteId: number
currency: object
createdAt: number
isPreview: boolean
lang: string
}) => `mutation{
quotePdfExport(
quoteFrontendPdf(
quoteId: ${data.quoteId},
currency: ${convertObjectToGraphql(data.currency || {})},
storeHash: "${storeHash}",
createdAt: ${data.createdAt},
lang: "${data.lang}",
isPreview: ${data.isPreview}
) {
url,
content,
}
}`

Expand Down Expand Up @@ -408,15 +413,19 @@ export const getBcQuoteDetail = (data: {

export const exportB2BQuotePdf = (data: {
quoteId: number
currency: object
createdAt: number
isPreview: boolean
lang: string
}): CustomFieldItems =>
B3Request.graphqlB2B({
query: exportQuotePdf(data),
})

export const exportBcQuotePdf = (data: {
quoteId: number
currency: object
createdAt: number
isPreview: boolean
lang: string
}): CustomFieldItems =>
B3Request.graphqlB2B({
query: exportQuotePdf(data),
Expand Down

0 comments on commit 436a6d5

Please sign in to comment.