Skip to content
Permalink
Browse files

added messaging system2

  • Loading branch information
ryanhhong committed Dec 4, 2019
1 parent 425d37a commit 6fc94045efb996081b97b345ddb818ff8f63c805
BIN +6 KB .DS_Store
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,11 @@
div.card-deck {
display: grid;
grid-template-columns: auto;
}

div.card-deck div.card {
margin: 25px 0 25px;
}



@@ -0,0 +1,32 @@
div.card-deck {
display: grid;
grid-template-columns: auto;
}

div.card-deck div.card {
margin: 25px 0 25px;
}

#labels {
color: deeppink;
}


#label-border {
border-color: deepskyblue;
border-width: 4px;
}

#expiration-date {
border-color: deepskyblue;
border-width: 4px;
}

#publish-button {
font-size: auto;
color: deeppink;
background-color: midnightblue;
border-width: 4px;
border-color: deepskyblue;
}

@@ -0,0 +1,89 @@
const express = require('express');
const router = express.Router();
const admin = require('../config/firebase-config-admin');
const firebase = require('../config/firebase-config-client');

const database = admin.firestore();


/* GET message-inbox page */
router.get('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
let messages;
await database
.collection('messages')
.orderBy('sent_date','desc')
.get()
.then(snapshot => {
messages = snapshot.docs.map(document => {
let message = document.data();
message.id = document.id;

let sent_date = message.sent_date;

let recipient = message.recipient;
let sender = message.sender;
let the_message = message.the_message;
let listing_name = message.listing_name;


// Firestore timestamp -> JavaScript date object
sent_date = sent_date.toDate();

// JavaScript date objects -> DOM strings
sent_date = sent_date.toLocaleString();

message.sent_date = sent_date;


return message;
});
});

if (user) {
res.render('message-inbox', { user: user, messages });
} else {
res.render('message-inbox', { messages });
}
});

unsubscribe();
});

/* POST message-inbox page */
/*
the plan is to reroute to message-send then that page will pick up any database items where sender == your uid and sent_date is null
maybe have a special page just to reply??
*/

router.post('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
let message_id = req.body.message_id;
let sender = await user.uid;
let listing_name = message.listing_name;
let data = {
listing_name
};
await database
.collection('messages')
.doc(message_id)
.update({
donatee: donatee,
requested: true,
});
} catch (error) {
res.render('index', { user: user, errorMessage: error });
}

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

unsubscribe();
});

module.exports = router;
@@ -0,0 +1,65 @@
const express = require('express');
const router = express.Router();
const admin = require('../config/firebase-config-admin');
const firebase = require('../config/firebase-config-client');

const database = admin.firestore();


/* GET message-send page */
router.get('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if(!user){
res.redirect('/');
}else{
res.render('message-send');
}

});

unsubscribe();
});

/* POST donation-board page */

router.post('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
//let sender = await user.id;
let recipient = req.body.recipient;
let the_message = req.body.message;
let listing_name = req.body.listing_name;

//let sent_date = message.sent_date;
// Firestore timestamp -> JavaScript date object
//sent_date = sent_date.toDate();

// JavaScript date objects -> DOM strings
//sent_date = sent_date.toLocaleString();

//message.sent_date = sent_date;

let data ={
listing_name: listing_name,
message: the_message,
recipient: recipient,
sender: user.uid,
sent_date: new Date(Date.now()),
};

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

res.redirect('/message-inbox');
}
});

unsubscribe();
});

module.exports = router;
@@ -0,0 +1,29 @@
extends layout

block content
link(rel="stylesheet" href="/stylesheets/message-inbox.css")
if user
include partials/navbar-authenticated
else
include partials/navbar-unauthenticated
div(class="container")
if user
//- show the page
h1 Your Messages:
div(class="card-deck")
each message in messages
if message.recipient == user.uid
div(class="card text-center")
h5(class="card-header")=`Listing Name: ${message.listing_name}`
ul(class="list-group list-group-flush")
li(class="list-group-item")=`Message: ${message.message}`
li(class="list-group-item")=`From: ${message.sender}`
li(class="list-group-item")=`Sent Date: ${message.sent_date}`
div(class="card-footer")
form(action="/message-inbox" method="POST")
input(type="hidden" name="message_id" value=message.id)
button(type="submit" class="btn btn-primary") Reply

else
h1 Please login to view your messages!

@@ -0,0 +1,29 @@
extends layout

block content
link(rel="stylesheet" href="/stylesheets/message-send.css")
include partials/navbar-authenticated

div(class="container")

include partials/error-message
form(action="/message-send" method="POST")
div(class="row justify-content-between")
div(class="col-6")
div(class="form-group")
label(for="recipient" id="labels") To
input(type="text" class="form-control" name="recipient" placeholder="Enter recipient username" id="recipient" required)
div(class="form-group" id="labels")
div(class="form-group" id="labels") Listing Name
input(type="text" class="form-control" name="listing_name" placeholder="Enter listing name" id="listing_name" required)


div(class="col-6")
div(class="form-group" id="labels")
label(for="recipient" id="labels") Message
div(class="form-group" id="labels")
textarea(type="text" class="form-control" name="message" placeholder="Enter message" rows="4" cols="50" id="message" required)
button(type="submit" class="btn btn-primary float-right" id="send-button") Send



0 comments on commit 6fc9404

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