Skip to content

Commit

Permalink
give the visitor the opportunity to cancel the password prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Zir0h committed Oct 27, 2023
1 parent 8e5800b commit 1bd859d
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions src/components/media-types/pdf/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const PdfComponent = memo(function ({
const [pageNumber, setPageNumber] = useState(1)
const [renderedPageNumber, setRenderedPageNumber] = useState<number>()
const [loading, setLoading] = useState(displayView)
const [showDocument, setShowDocument] = useState(true);

const [height, setHeight] = useState<number>()

Expand Down Expand Up @@ -70,6 +71,32 @@ export const PdfComponent = memo(function ({
setRenderedPageNumber(pageNumber)
}

function onPassword(callback, reason) {
function callbackProxy(password) {
// Cancel button handler
if (password === null) {
// password will be null if user clicks on cancel
setShowDocument(false);
return
}
callback(password);
}

switch (reason) {
case 1: {
const password = prompt("Enter the password to open this PDF file.");
callbackProxy(password);
break;
}
case 2: {
const password = prompt("Invalid password. Please try again.");
callbackProxy(password);
break;
}
default:
}
}

const cover = (
<>
<ImageComponent
Expand All @@ -96,33 +123,45 @@ export const PdfComponent = memo(function ({
// })
// const loading = renderedPageNumber !== pageNumber
if (!displayView) return cover

return (
<div ref={container} className={styles.container}>
<Document
file={file}
loading={cover}
onLoadSuccess={onDocumentLoadSuccess}
onLoadError={onDocumentLoadError}
title={`PDF object ${nft.token_id}`}
options={options}
>
{renderedPageNumber && renderedPageNumber !== pageNumber && (
{!showDocument ? (
<Button
onClick={() => {
setShowDocument(true);
}}
>
Reload PDF with password
</Button>
) : (
<Document
file={file}
loading={cover}
onPassword={onPassword}
onLoadSuccess={onDocumentLoadSuccess}
onLoadError={onDocumentLoadError}
title={`PDF object ${nft.token_id}`}
options={options}
>
{renderedPageNumber && renderedPageNumber !== pageNumber && (
<Page
key={`${renderedPageNumber}`}
className={styles.previous_page}
pageNumber={renderedPageNumber}
height={height}
/>
)}
<Page
key={`${renderedPageNumber}`}
className={styles.previous_page}
pageNumber={renderedPageNumber}
key={pageNumber}
className={styles.page}
pageNumber={pageNumber}
onRenderSuccess={onRender}
height={height}
/>
)}
<Page
key={pageNumber}
className={styles.page}
pageNumber={pageNumber}
onRenderSuccess={onRender}
height={height}
/>
</Document>
{!loading && (
</Document>
)}
{!loading && showDocument && (
<div className={styles.pdfNav}>
<Button disabled={pageNumber <= 1} onClick={previousPage}>
{'Prev «'}
Expand Down

0 comments on commit 1bd859d

Please sign in to comment.