Created
November 15, 2017 17:47
-
-
Save VinayaSathyanarayana/1a6b5d94380268f971c641f615bb7ea2 to your computer and use it in GitHub Desktop.
Reservation Flow - KeystoneJS help
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var keystone = require('keystone'); | |
var Types = keystone.Field.Types; | |
/** | |
* Hybrid from the model from the example contact and from SydJS example | |
*/ | |
/** | |
* Reservation Model | |
* ============= | |
*/ | |
var Reserve = new keystone.List('Reserve', { | |
nocreate: true, | |
noedit: true, | |
}); | |
Reserve.add({ | |
user: { type: Types.Relationship, ref: 'User', index: true }, | |
}, 'Details', { | |
createdAt: { type: Date, default: Date.now }, | |
endedAt: { type: Date }, | |
party_size: {type: Types.Number}, | |
date: {type: Date}, | |
restaurant: { type: Types.Relationship, ref: 'Restaurant', index: true }, | |
}, 'State', { | |
state: { type: Types.Select, options: 'null, sent, confirmed, denied, ended', noedit: true }, | |
restResponse: { | |
confirm: {type: Types.Boolean}, | |
date: {type: Date} | |
}, | |
}); | |
Reserve.defaultSort = '-createdAt'; | |
Reserve.defaultColumns = 'user, restaurant, createdAt, response'; | |
Reserve.register(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var keystone = require('keystone'); | |
var Email = require('keystone-email'); | |
var Reserve = keystone.list('Reserve'); | |
var moment = require('moment'); | |
exports = module.exports = function (req, res) { | |
var view = new keystone.View(req, res); | |
var locals = res.locals; | |
// Set locals | |
locals.filters = { | |
rest: req.params.rest, // this i guess isn't doing anything... as i don't pass any parameters | |
}; | |
locals.formData = req.body || {}; | |
locals.reserveSubmitted = false; | |
locals.date = moment(locals.formData.day, 'DD-MM-YYYY').hours(locals.formData.hour); | |
view.on('post', {action: 'reserve'}, function (next) { | |
// handle form | |
var newReserve = new Reserve.model({ | |
user: locals.user.id, | |
party_size: locals.formData.party_size, | |
date: locals.date, | |
restaurant: locals.formData.restaurant | |
}), | |
updater = newReserve.getUpdateHandler(req, res, { | |
errorMessage: 'There was an error creating your new post:' | |
}); | |
// this is stupid, i know... but can't seem to set reserveSubmitted = true, after email sent!! | |
locals.reserveSubmitted = true; | |
if (locals.reserveSubmitted) { | |
newReserve.state = 'sent'; | |
} | |
updater.process(req.body, { | |
flashErrors: true, | |
logErrors: true, | |
fields: '' | |
}, function(err) { | |
if (err) { | |
locals.validationErrors = err.errors; | |
} else { | |
return res.redirect('/dashboard#reserves'); | |
} | |
next(); | |
}); | |
//Send email to restaurant | |
/** | |
* Is it possible to have this as a method in the Mongoose model?!? | |
**/ | |
new Email('test/test-email.pug', { | |
transport: 'mailgun' | |
}).send({ | |
firstName: 'Tiago', //user's first name | |
name: 'Vasconcelos', //user's last name | |
restName: locals.formData.restName, // the restaurant name | |
party: locals.formData.party_size, // how many people | |
day: locals.formData.day, // day | |
hour: locals.formData.hour, // hour | |
reserveID: newReserve.id, // the ID of this reserve | |
}, { | |
apiKey: process.env.MAILGUN_API_KEY, | |
domain: process.env.MAILGUN_DOMAIN, | |
to: '[email protected]', | |
from: { | |
name: 'Maitre', | |
email: '[email protected]', | |
}, | |
subject: 'Reserva Maitre no ' + locals.formData.restName, | |
}, function (err, result) { | |
if (err) { | |
console.error('🤕 Mailgun test failed with error:\n', err); | |
} else { | |
console.log('📬 Successfully sent Mailgun test with result:\n', result); | |
} | |
}); | |
}); | |
view.render('reserve'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// template with form that posts to the reserve route | |
// | |
extends ../layouts/default | |
block content | |
//-.hero-rest--bg(style='background-image: url(' + rest.image.url + ')') | |
.parallax-container | |
.parallax | |
img(src= rest.image.url) | |
.container | |
.restaurant--content | |
form(method='post', action='/reserve').card | |
.card-content | |
span.card-title.header= rest.name | |
.divider | |
.row.section | |
.col.s12.m4 | |
.center | |
i.small.material-icons restaurant | |
p.thin.grey-text Tipo de Cozinha | |
p= rest.cuisines | |
.col.s12.m4 | |
.center | |
i.small.material-icons location_city | |
p.thin.grey-text Localização | |
if rest.location.suburb | |
if rest.location.suburb != rest.location.state | |
p= rest.location.suburb | |
p= rest.location.state | |
.col.s12.m4 | |
.center | |
i.small.material-icons monetization_on | |
p.thin.grey-text Preço para 2 | |
p= '€' + rest.cost_for_two | |
.section | |
if rest.location.geo | |
.section--map. | |
<div id="map"></div> | |
<script> | |
function initMap() { | |
var uluru = {lat: #{rest.location.geo[1]}, lng: #{rest.location.geo[0]}}; | |
var map = new google.maps.Map(document.getElementById('map'), { | |
zoom: 16, | |
center: uluru, | |
scrollwheel: false | |
}); | |
var marker = new google.maps.Marker({ | |
position: uluru, | |
animation: google.maps.Animation.BOUNCE, | |
map: map | |
}); | |
} | |
</script> | |
<script async defer src="https://maps.googleapis.com/maps/api/js?key=#{process.env.GOOGLE_BROWSER_KEY}&callback=initMap"></script> | |
.section | |
span.card-title Descrição | |
div= rest.description | |
if rest.singular | |
.divider | |
.section | |
if rest.singular.chef | |
P= 'Chef: ' + rest.singular.chef | |
if rest.singular.awards | |
p= 'Prémios: ' + rest.singular.awards | |
if rest.singular.signatureDish | |
p= 'Prato recomendado: ' + rest.singular.signatureDish | |
.section | |
.row | |
.col.s12 | |
input(type='hidden', name='action', value='reserve') | |
input(type='hidden', name='restaurant', value=rest.id) | |
input(type='hidden', name='restName', value=rest.name) | |
input(type='hidden', name='restEmail', value=rest.contact.email) | |
.input-field.col.s12 | |
p.center-align Quantas pessoas | |
select(name='party_size') | |
option(value='2') 2 Pessoas | |
option(value='1') 1 Pessoa | |
option(value='3') 3 Pessoas | |
option(value='4') 4 Pessoas | |
option(value='5') 5 Pessoas | |
option(value='6') 6 Pessoas | |
option(value='7') 7 Pessoas | |
option(value='8') 8 Pessoas | |
option(value='9') 9 Pessoas | |
option(value='10') 10 Pessoas | |
.input-field.col.s12 | |
p.center-align Data | |
select(name='day') | |
each day, index in dates.pretty | |
option(value= dates.ugly[index])= day | |
.input-field.col.s12 | |
p.center-align Horario | |
select(name='hour') | |
option(value='12') 12:00 | |
option(value='13') 13:00 | |
option(value='14') 14:00 | |
option(value='15') 15:00 | |
option(value='16') 16:00 | |
option(value='17') 17:00 | |
option(value='18') 18:00 | |
option(value='19') 19:00 | |
option(value='20') 20:00 | |
option(value='21') 21:00 | |
option(value='22') 22:00 | |
option(value='23') 23:00 | |
.card-action | |
button(class='btn-flat', type='submit') Reservar | |
block js | |
script. | |
$(document).ready(function(){ | |
$('.parallax').parallax(); | |
$('select').material_select(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment