Skip to content

Instantly share code, notes, and snippets.

@PistachioPony
Created March 11, 2014 15:17
Show Gist options
  • Save PistachioPony/9487953 to your computer and use it in GitHub Desktop.
Save PistachioPony/9487953 to your computer and use it in GitHub Desktop.
Creating the modal html - routes
<!--In the Dashboard.njs-->
<div class="dashboard-item-offer box {{"invisible" if offer.isHidden}} {{"offer-to-rate" if offer.state == "rescinded" and not offer.ratedBySeller }}">
<div class="col col-1of4-md col-1of1-xs">
{% include "partials/userthumb.njs" %}
{% include "partials/modals/message.njs" %} *****This tells the button where to go
<div class="offerer">
<p class="offerer-name">{{offer.offerer.name}}</p>
<p class="offerer-location">{{offer.offerer.location.city}}, {{offer.offerer.location.state}}</p>
<a><button type="submit" class="bttn6 seller-message" data-toggle="modal" data-target="#modalSellerMessage"> Message</button></a>
</div>
</div>
<!--In the partial messge.njs-->
<div class="modal fade bs-example-modal-sm" id="modalSellerMessage" tabindex="-1" role="dialog" aria-labelledby="message" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<a class="close" data-dismiss="modal" aria-hidden="true">&#10005;</a>
<form id="modalSellerMessage" method="POST" action="/users/{{offer.offerer.offererId}}/messages/send">
<label class="" for="message">Your Message</label>
<input type="text" id="seller-message" name="message" placeholder= "message" required aria-required="true">
<label class="" for="message">{{dashboard.item._id}}</label>
<input type="hidden" id="itemId" name="item" value="{{listing.item._id}}">
*******In label classabove and below, we are creating key value pairs for the JSON (name and value), we hide it
so user wont see it but this is how we can pass this information to the json through route.
<label class="" for="message">{{dashboard.item.offers.price}}</label>
<input type="hidden" id="offerId" name="offer" value="{{offer._id}}">
<p class="help-inline">E.g., 'Hi, I have an item to swap'</p>
<button type="submit" class="">Send Message</button>
</input>
</form>
</div>
</div>
</div>
<!-- Method in the route -->
var sendMessage = function (req, res, next) {
var newMessage = new Message();
var thread = new ObjectId();
*******This below takes the info from the form (req.body) and adds it to the JSON**********
newMessage.messenger.messengerId = req.user._id;
newMessage.messenger.name = req.user.displayName;
newMessage.receiver.receiverId = req.params.userId;
newMessage.receiver.name = req.contents.user.displayName;
newMessage.content = req.body.message;
newMessage.thread = thread;
newMessage.item = req.body.item;
newMessage.offer = req.body.offer;
newMessage.save(function(err, message) {
if (err) return next({type: "HolyFuck"});
res.redirect('/messages/' + message._id + '?json');
});
};
<!--And the actual route-->
app.post('/users/:userId/messages/send',
security.ensureAuthenticated,
user.fetchUser,
block.prohibitBlockedUser,
sendMessage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment