Skip to content

Commit

Permalink
Added alerts for errors and successes
Browse files Browse the repository at this point in the history
  • Loading branch information
3milyfz committed Dec 1, 2023
1 parent b618b15 commit d6176ff
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/helper-components/pdf-manager-component/PdfManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function PdfFileManager() {
const fileInputRef = useRef(null);
const [pdfViewerInstance, setPdfViewerInstance] = useState(null);
const [alertOpen, setAlertOpen] = useState(false);
const [alertMessage, setAlertMessage] = useState('');
const [alertSeverity, setAlertSeverity] = useState('success'); // 'success' or 'error'

const handleButtonClick = () => {
fileInputRef.current.click();
Expand Down Expand Up @@ -60,10 +62,18 @@ function PdfFileManager() {
if (!uploadedPdfs.some(pdf => pdf.id === response.data)) {
setUploadedPdfs([...uploadedPdfs, documentItem]);
}
setAlertSeverity('success');
setAlertMessage('File uploaded successfully');
setAlertOpen(true);
setTimeout(() => setAlertOpen(false), 2000); // Close the alert after 2 seconds
})
.catch((error) => {
// Handle any errors (e.g., show an error message)
console.error('File upload failed:', error);
setAlertMessage('Error uploading file');
setAlertSeverity('error');
setAlertOpen(true);
setTimeout(() => setAlertOpen(false), 2000); // Close the alert after 2 seconds
});
};

Expand All @@ -89,34 +99,40 @@ function PdfFileManager() {
console.error('No data returned from exportPdf');
return;
}

const formData = new FormData();
formData.append("file", blob, `updated_${selectedPdf.name}`); // Ensure blob is a Blob object

Axios.put(`http://localhost:8080/files/update/${selectedPdf.id}`, formData)
.then(response => {
// Handle success
console.log('PDF updated successfully:', response.data);
setAlertMessage("PDF changes saved successfully");
setAlertSeverity('success');
setAlertOpen(true);
setTimeout(() => setAlertOpen(false), 2000); // Close the alert after 2 seconds
})
.catch(error => {
// Handle error
console.error('PDF update failed:', error);
setAlertMessage("Error saving PDF changes");
setAlertSeverity('error');
setAlertOpen(true);
setTimeout(() => setAlertOpen(false), 2000); // Close the alert after 2 seconds
});
});
}
};

return (
<FileManagerContainer>
<Snackbar
open={alertOpen}
<Snackbar
open={alertOpen}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
>
<Alert severity="success" sx={{ fontSize: '1.5rem' }}>
PDF changes saved successfully
</Alert>
<Alert
severity={alertSeverity}
sx={{ fontSize: '1.5rem' }}
>
{alertMessage}
</Alert>
</Snackbar>
<PdfNavbar>
<input
Expand Down

0 comments on commit d6176ff

Please sign in to comment.