Skip to content

Commit 4500bc9

Browse files
Copilotalimarchal
andcommitted
Add isDownloading state management to PDF download function
Co-authored-by: alimarchal <23615447+alimarchal@users.noreply.github.com>
1 parent 793ab63 commit 4500bc9

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

resources/js/components/BranchLocator.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ const BranchLocator: React.FC<Props> = ({ branches = [], regions = [], districts
194194
};
195195

196196
const downloadPDF = () => {
197-
// Create a printable version of the data
198-
const printContent = `
197+
setIsDownloading(true);
198+
try {
199+
// Create a printable version of the data
200+
const printContent = `
199201
<!DOCTYPE html>
200202
<html>
201203
<head>
@@ -285,16 +287,24 @@ const BranchLocator: React.FC<Props> = ({ branches = [], regions = [], districts
285287
</html>
286288
`;
287289

288-
const printWindow = window.open('', '_blank');
289-
if (printWindow) {
290-
printWindow.document.write(printContent);
291-
printWindow.document.close();
292-
293-
// Wait for content to load then trigger print
294-
setTimeout(() => {
295-
printWindow.print();
296-
// printWindow.close();
297-
}, 250);
290+
const printWindow = window.open('', '_blank');
291+
if (printWindow) {
292+
printWindow.document.write(printContent);
293+
printWindow.document.close();
294+
295+
// Wait for content to load then trigger print
296+
setTimeout(() => {
297+
printWindow.print();
298+
// printWindow.close();
299+
setIsDownloading(false);
300+
}, 250);
301+
} else {
302+
setIsDownloading(false);
303+
}
304+
} catch (error) {
305+
console.error('Error generating PDF:', error);
306+
alert('Failed to open print dialog. Please try again.');
307+
setIsDownloading(false);
298308
}
299309
};
300310

0 commit comments

Comments
 (0)