Skip to content

Commit 6659d81

Browse files
942243: Updated comments index.js
1 parent f93d43d commit 6659d81

File tree

1 file changed

+46
-29
lines changed
  • How to/Download PDF based on confirmation in the toolbar click/React-Client/src

1 file changed

+46
-29
lines changed

How to/Download PDF based on confirmation in the toolbar click/React-Client/src/index.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
FormDesigner,
1818
Inject,
1919
} from '@syncfusion/ej2-react-pdfviewer';
20+
2021
import {
2122
createSpinner,
2223
showSpinner,
@@ -26,64 +27,79 @@ import {
2627
function Default() {
2728
let viewer;
2829

30+
// Create spinner once component mounts
2931
React.useEffect(() => {
3032
createSpinner({
3133
target: document.getElementById('PDfviewProgress'),
3234
});
3335
}, []);
3436

37+
// Show spinner during download validation
3538
const show = () => {
3639
showSpinner(document.getElementById('PDfviewProgress'));
3740
};
3841

42+
// Hide spinner after validation completes
3943
const hide = () => {
4044
hideSpinner(document.getElementById('PDfviewProgress'));
4145
};
4246

47+
// Called when download starts
4348
const downloadStart = () => {
4449
show();
4550
};
4651

52+
// Called when download ends
4753
const downloadEnd = () => {
4854
hide();
4955
};
5056

57+
// Custom toolbar button configuration
5158
var downloadOption = {
52-
prefixIcon: 'e-pv-download-document-icon e-pv-icon',
53-
id: 'download_pdf',
54-
tooltipText: 'Download file',
55-
align: 'right',
59+
prefixIcon: 'e-pv-download-document-icon e-pv-icon', // Syncfusion icon class
60+
id: 'download_pdf', // Unique ID for custom button
61+
tooltipText: 'Download file', // Tooltip shown on hover
62+
align: 'right', // Aligns button to the right of toolbar
5663
};
5764

65+
// Handles toolbar button click events
5866
const toolbarClick = async (args) => {
59-
if (args.item && args.item.id === 'download_pdf') {
60-
const userConfirmed = window.confirm('Do you want to download this PDF?');
61-
if (!userConfirmed) return;
62-
63-
const requestParams = { canDownload: true };
64-
try {
65-
const response = await fetch('https://localhost:5001/pdfviewer/CheckDownload', {
66-
method: 'POST',
67-
headers: { 'Content-Type': 'application/json' },
68-
body: JSON.stringify(requestParams),
69-
});
70-
71-
if (!response.ok) throw new Error('Error validating download');
72-
73-
const result = await response.json();
74-
if (result) {
75-
const viewerInstance = document.getElementById('container').ej2_instances[0];
76-
viewerInstance.download();
67+
// Check if the clicked item is our custom download button
68+
if (args.item && args.item.id === 'download_pdf') {
69+
// Prompt user for confirmation
70+
const userConfirmed = window.confirm('Do you want to download this PDF?');
71+
if (!userConfirmed) return;
72+
73+
// Prepare request payload
74+
const requestParams = { canDownload: true };
75+
76+
try {
77+
// Send request to server to validate download permission
78+
const response = await fetch('https://localhost:5001/pdfviewer/CheckDownload', {
79+
method: 'POST',
80+
headers: { 'Content-Type': 'application/json' },
81+
body: JSON.stringify(requestParams),
82+
});
83+
84+
// Handle server error
85+
if (!response.ok) throw new Error('Error validating download');
86+
87+
// Parse server response
88+
const result = await response.json();
89+
90+
// If server allows download, trigger viewer's download method
91+
if (result) {
92+
const viewerInstance = document.getElementById('container').ej2_instances[0];
93+
viewerInstance.download();
94+
}
95+
} catch (error) {
96+
console.error('Download validation failed:', error);
7797
}
78-
} catch (error) {
79-
console.error('Download validation failed:', error);
8098
}
81-
}
82-
};
99+
};
83100

84101
return (
85102
<div>
86-
{/* PDF Viewer Component */}
87103
<div className="control-section">
88104
<PdfViewerComponent
89105
ref={(scope) => {
@@ -104,7 +120,7 @@ function Default() {
104120
'SelectionTool',
105121
'SearchOption',
106122
'PrintOption',
107-
downloadOption,
123+
downloadOption, // Inject custom download button
108124
'UndoRedoTool',
109125
'AnnotationEditTool',
110126
'FormDesignerEditTool',
@@ -134,13 +150,14 @@ function Default() {
134150
</PdfViewerComponent>
135151
</div>
136152

137-
{/* Spinner container below the PDF Viewer */}
153+
{/* Spinner container */}
138154
<div id="PDfviewProgress"></div>
139155
</div>
140156
);
141157
}
142158

143159
export default Default;
144160

161+
// Render the component
145162
const root = createRoot(document.getElementById('sample'));
146163
root.render(<Default />);

0 commit comments

Comments
 (0)