Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Last active August 29, 2015 14:14
Show Gist options
  • Save terakilobyte/ad31dca0b11d0ab20157 to your computer and use it in GitHub Desktop.
Save terakilobyte/ad31dca0b11d0ab20157 to your computer and use it in GitHub Desktop.
/**
* Jquery related stuffs
*/
function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) {
$('#complete-bonfire-dialog').modal('show');
// Only post to server if there is an authenticated user
if ($('.signup-btn-nav').length < 1) {
$.ajax({
type: 'POST',
data: {
bonfireInfo: {
completedWith : didCompleteWith,
solution: bonfireSolution,
bonfireHash: thisBonfireHash
}
},
url: '/completed-bonfire/'
});
}
}
$('.next-bonfire-button').on('click', function() {
var bonfireSolution = myCodeMirror.getValue();
var thisBonfireHash = passedBonfireHash || null;
var didCompleteWith = $('#completed-with').val() || null;
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
window.setTimeout(function() {
// TODO
window.location = '/bonfires';
}, 100);
});
/**
* The method that handles the post request
*/
app.post('/completed-bonfire/', function (req, res) {
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
var isCompletedDate = Math.round(+new Date() / 1000);
var bonfireHash = req.body.bonfireInfo.bonfireHash;
var isSolution = req.body.bonfireInfo.solution;
// TODO
debug(isCompletedWith, 'Is completed with');
if (isCompletedWith) {
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
paired.exec(function(err, pairedWith) {
if (err) {
return err;
} else {
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
pairedWith = pairedWith.pop();
index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
pairedWith.uncompletedBonfires.splice(index,1)
}
pairedWith.completedBonfires.push({
_id: bonfireHash,
completedWith: req.user._id,
completedDate: isCompletedDate,
solution: isSolution
})
req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: pairedWith._id,
completedDate: isCompletedDate,
solution: isSolution
})
req.user.save(function(err, user) {
pairedWith.save(function(err, paired) {
if (err) {
throw err;
}
if (user && paired) {
res.redirect('/bonfires');
}
})
});
}
})
} else {
req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: null,
completedDate: isCompletedDate,
solution: isSolution
})
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
req.user.save(function(err, user) {
if (err) {
throw err;
}
if (user) {
res.redirect('/bonfires');
}
});
}
});
function completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash) {
$('#complete-bonfire-dialog').modal('show');
// Only post to server if there is an authenticated user
if ($('.signup-btn-nav').length < 1) {
$.ajax({
type: 'POST',
data: {
bonfireInfo: {
completedWith : didCompleteWith,
solution: bonfireSolution,
bonfireHash: thisBonfireHash
}
},
url: '/completed-bonfire/'
}).done(function() {
window.location = '/bonfires';
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment