Skip to content

Commit

Permalink
trying button to view uploaded waiver pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyleal committed Jul 30, 2024
1 parent 64b3c6a commit 4d2d684
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions client/src/pages/FroshRetreat/FroshRetreat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const RetreatRegistration = () => {
const { setSnackbar } = useContext(SnackbarContext);
const { axios } = useAxios();
const isRetreat = user?.isRetreat === true;
const isWaiverUploaded = user?.waiver?.filename !== undefined;

const [file, setFile] = useState(null);
const [isUploaded, setIsUploaded] = useState(false);
Expand Down Expand Up @@ -315,6 +316,7 @@ const RetreatRegistration = () => {
}}
style={{ marginBottom: '25px' }}
/>

<div className="display-field">
<h3>UPLOAD SIGNED WAIVER:</h3>
<p>Only PDF files are accepted</p>
Expand All @@ -332,7 +334,21 @@ const RetreatRegistration = () => {
<p>Please view the waiver before uploading the signed copy.</p>
)}
</div>

{isWaiverUploaded ? (
<Button
label="View Uploaded Waiver"
isSecondary
onClick={() => {
window.open(`/view-waiver`, '_blank').focus();
}}
style={{ marginBottom: '25px' }}
/>
) : (
<></>
)}
</div>

{isRetreat ? (
<h2>You have already paid for Frosh Retreat!</h2>
) : outOfTickets ? (
Expand Down
31 changes: 31 additions & 0 deletions server/src/middlewares/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,35 @@ router.post('/upload-waiver', (req, res) => {
});
});

router.get('/view-waiver', async (req, res) => {
try {
if (!req.user) {
console.log('User not authenticated.');
return res.status(401).send('User not authenticated.');
}

const userId = req.user._id;
const user = await User.findById(userId); // get user from DB

if (!user) {
console.log('User not found.');
return res.status(404).send('User not found.');
}

if (!user.waiver) {
console.log('No waiver found for this user.');
return res.status(404).send('No waiver found for this user.');
}
const { filename, data, contentType } = user.waiver;

res.setHeader('Content-Disposition', `inline; filename="${filename}"`);
res.setHeader('Content-Type', contentType);
res.send(data);
} catch (error) {
console.error(error);
console.log('Error retrieving file.');
res.status(500).send('Error retrieving file.');
}
});

module.exports = router;

0 comments on commit 4d2d684

Please sign in to comment.