Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Add donation form error handling
Browse files Browse the repository at this point in the history
Ensure valid dates and meeting points
  • Loading branch information
pjsilvestre committed Dec 2, 2019
1 parent bb701a1 commit 9215cec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
express-app/node_modules
express-app/secrets
.vscode/launch.json
.vscode/settings.json
19 changes: 18 additions & 1 deletion express-app/routes/donation-board.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ router.get('/', (req, res) => {
let donations = firebase.auth().onAuthStateChanged(async user => {
await database
.collection('donations')
.where('expired', '==', false)
.where('requested', '==', false)
.orderBy('expiration_date')
.get()
.then(snapshot => {
Expand Down Expand Up @@ -66,6 +68,21 @@ router.get('/', (req, res) => {
});

/* POST donation-board page */
router.post('/', (req, res) => {});
router.post('/', (req, res) => {
firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
let donation_id = req.body.donation_id;

} catch (error) {
res.render('index', { user: user, errorMessage: error });
}

res.redirect('/my-requests');
}
});
});

module.exports = router;
37 changes: 30 additions & 7 deletions express-app/routes/donation-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,39 @@ router.post('/', (req, res) => {
let pickup_date = req.body.pickup_date;
pickup_date = new Date(pickup_date);

// date validation
let currentDate = new Date(Date.now());
if (expiration_date < currentDate) {
throw new Error('Invalid expiration date.');
} else if (pickup_date < currentDate) {
throw new Error('Invalid pickup date.');
} else if (expiration_date < pickup_date) {
throw new Error('Expiry date must be after pickup date.');
}

let food_item = req.body.food_item;
let meeting_point = req.body.meeting_point;

let latitude = req.body.meeting_point_geopoint_latitude;
let longitude = req.body.meeting_point_geopoint_longitude;
latitude = parseFloat(latitude);
longitude = parseFloat(longitude);
let meeting_point_geopoint = new admin.firestore.GeoPoint(
latitude,
longitude
);
let meeting_point_geopoint;
try {
meeting_point_geopoint = new admin.firestore.GeoPoint(
latitude,
longitude
);
} catch (error) {
throw new Error(
'Please choose a meeting point from the dropdown menu.'
);
}

let status = 'unrequested';
let requested = false;
let expired = false;
let fulfilled_donator = false;
let fulfilled_donatee = false;

let halal = req.body.halal ? true : false;
let kosher = req.body.kosher ? true : false;
Expand All @@ -65,7 +85,10 @@ router.post('/', (req, res) => {
food_item: food_item,
meeting_point: meeting_point,
meeting_point_geopoint: meeting_point_geopoint,
status: status,
requested: requested,
expired: expired,
fulfilled_donator: fulfilled_donator,
fulfilled_donatee: fulfilled_donatee,

halal: halal,
kosher: kosher,
Expand All @@ -76,7 +99,7 @@ router.post('/', (req, res) => {

await database.collection('donations').add(data);
} catch (error) {
res.render('index', { user: user, errorMessage: error });
res.render('donation-form', { user: user, errorMessage: error });
}

res.redirect('donation-board');
Expand Down
11 changes: 6 additions & 5 deletions express-app/views/donation-board.pug
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ block content
h1 Listings:
div(class="card-deck")
each donation in donations
if donation.status == 'unrequested'
if !donation.requested && !donation.expired
div(class="card text-center")
h5(class="card-header")=`${donation.food_item}`
ul(class="list-group list-group-flush")
Expand All @@ -20,13 +20,14 @@ block content
li(class="list-group-item")=`Donated by: ${donation.donator}`
li(class="list-group-item")=`Meeting Point: ${donation.meeting_point}`
li(class="list-group-item")=`Pickup Date: ${donation.pickup_date}`
li(class="list-group-item")=`Expires: ${donation.expiration_date}`
li(class="list-group-item")=`Expiry Date: ${donation.expiration_date}`
div(class="card-footer")
if user
if donation.donator == user.uid
button(type="submit" class="btn btn-secondary" disabled) Request
else
input(type="hidden" name="donation" value=donation)
button(type="submit" class="btn btn-primary") Request
form(action="/donation-board" method="POST")
input(type="hidden" name="donation_id" value=donation.id)
button(type="submit" class="btn btn-primary") Request
else
button(type="submit" class="btn btn-secondary" disabled) Request
button(type="submit" class="btn btn-secondary" disabled) Login to Request

0 comments on commit 9215cec

Please sign in to comment.