Skip to content
Permalink
Browse files

Simplify Firestore access

  • Loading branch information
pjsilvestre committed Dec 7, 2019
1 parent 71c5475 commit 2f66f55256264eac0c88eec4a74aa44001987d79
Showing with 26 additions and 57 deletions.
  1. +10 −19 express-app/routes/donation-board.js
  2. +9 −20 express-app/routes/my-donations.js
  3. +7 −18 express-app/routes/my-requests.js
@@ -41,6 +41,7 @@ router.use(async (req, res, next) => {
router.get('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
let donations;

await database
.collection('donations')
.where('expired', '==', false)
@@ -52,23 +53,12 @@ router.get('/', (req, res) => {
let donation = document.data();
donation.id = document.id;

let date_added = donation.date_added;
let pickup_date = donation.pickup_date;
let expiration_date = donation.expiration_date;

// Firestore timestamp -> JavaScript date object
date_added = date_added.toDate();
pickup_date = pickup_date.toDate();
expiration_date = expiration_date.toDate();

// JavaScript date objects -> DOM strings
date_added = date_added.toLocaleString();
pickup_date = pickup_date.toLocaleString();
expiration_date = expiration_date.toLocaleString();

donation.date_added = date_added;
donation.pickup_date = pickup_date;
donation.expiration_date = expiration_date;
// Firestore timestamp -> JavaScript date object -> DOM strings
donation.date_added = donation.date_added.toDate().toLocaleString();
donation.pickup_date = donation.pickup_date.toDate().toLocaleString();
donation.expiration_date = donation.expiration_date
.toDate()
.toLocaleString();

let dietary_restrictions = '';
if (donation.halal) dietary_restrictions += 'Halal ';
@@ -81,7 +71,8 @@ router.get('/', (req, res) => {

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

if (user) {
res.render('donation-board', { user: user, donations });
@@ -101,7 +92,7 @@ router.post('/', (req, res) => {
} else {
try {
let donation_id = req.body.donation_id;
let donatee = await user.uid;
let donatee = user.uid;

await database
.collection('donations')
@@ -48,15 +48,22 @@ router.get('/', (req, res) => {
await database
.collection('donations')
.where('donator', '==', user.uid)
.orderBy('request_accepted', 'desc') // if a donation request has been accepted, show it first...
.orderBy('requested', 'desc') // ...then any donations that have yet to be accepted
.orderBy('request_accepted', 'desc') // show accepted requests
.orderBy('requested', 'desc') // show pending requests
.orderBy('expiration_date')
.get()
.then(snapshot => {
donations = snapshot.docs.map(document => {
let donation = document.data();
donation.id = document.id;

// Firestore timestamp -> JavaScript date object -> DOM strings
donation.date_added = donation.date_added.toDate().toLocaleString();
donation.pickup_date = donation.pickup_date.toDate().toLocaleString();
donation.expiration_date = donation.expiration_date
.toDate()
.toLocaleString();

let dietary_restrictions = '';
if (donation.halal) dietary_restrictions += 'Halal ';
if (donation.kosher) dietary_restrictions += 'Kosher ';
@@ -66,24 +73,6 @@ router.get('/', (req, res) => {

donation.dietary_restrictions = dietary_restrictions;

let date_added = donation.date_added;
let pickup_date = donation.pickup_date;
let expiration_date = donation.expiration_date;

// Firestore timestamp -> JavaScript date object
date_added = date_added.toDate();
pickup_date = pickup_date.toDate();
expiration_date = expiration_date.toDate();

// JavaScript date objects -> DOM strings
date_added = date_added.toLocaleString();
pickup_date = pickup_date.toLocaleString();
expiration_date = expiration_date.toLocaleString();

donation.date_added = date_added;
donation.pickup_date = pickup_date;
donation.expiration_date = expiration_date;

return donation;
});
});
@@ -55,6 +55,13 @@ router.get('/', (req, res) => {
let donation = document.data();
donation.id = document.id;

// Firestore timestamp -> JavaScript date object -> DOM strings
donation.date_added = donation.date_added.toDate().toLocaleString();
donation.pickup_date = donation.pickup_date.toDate().toLocaleString();
donation.expiration_date = donation.expiration_date
.toDate()
.toLocaleString();

let dietary_restrictions = '';
if (donation.halal) dietary_restrictions += 'Halal ';
if (donation.kosher) dietary_restrictions += 'Kosher ';
@@ -64,24 +71,6 @@ router.get('/', (req, res) => {

donation.dietary_restrictions = dietary_restrictions;

let date_added = donation.date_added;
let pickup_date = donation.pickup_date;
let expiration_date = donation.expiration_date;

// Firestore timestamp -> JavaScript date object
date_added = date_added.toDate();
pickup_date = pickup_date.toDate();
expiration_date = expiration_date.toDate();

// JavaScript date objects -> DOM strings
date_added = date_added.toLocaleString();
pickup_date = pickup_date.toLocaleString();
expiration_date = expiration_date.toLocaleString();

donation.date_added = date_added;
donation.pickup_date = pickup_date;
donation.expiration_date = expiration_date;

return donation;
});
});

0 comments on commit 2f66f55

Please sign in to comment.
You can’t perform that action at this time.