From 3cff4e9879ab9ed9a2850a175954fd98cf17b653 Mon Sep 17 00:00:00 2001 From: Michael Q Larson Date: Thu, 2 Apr 2015 17:23:45 -0700 Subject: [PATCH] add logic to add campers to nonprofit projects of interest to them --- controllers/nonprofits.js | 35 +++++++++++++++++++++------ models/Nonprofit.js | 2 +- views/account/show.jade | 2 -- views/nonprofits/show.jade | 38 ++++++++++++++++++++---------- views/partials/universal-head.jade | 2 ++ 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/controllers/nonprofits.js b/controllers/nonprofits.js index d409cf1fbd..91aadfc037 100644 --- a/controllers/nonprofits.js +++ b/controllers/nonprofits.js @@ -115,6 +115,20 @@ exports.returnIndividualNonprofit = function(req, res, next) { if (dashedNameFull != dashedName) { return res.redirect('../nonprofit/' + dashedNameFull); } + var buttonActive = false; + if (req.user) { + if (req.user.uncompletedBonfires.length === 0) { + if (req.user.completedCoursewares.length > 63) { + var hasShownInterest = nonprofit.interestedCampers.filter(function ( obj ) { + return obj.username === req.user.profile.username; + }); + console.log(hasShownInterest); + if (hasShownInterest.length === 0) { + buttonActive = true; + } + } + } + } res.render('nonprofits/show', { dashedName: dashedNameFull, title: nonprofit.name, @@ -132,7 +146,9 @@ exports.returnIndividualNonprofit = function(req, res, next) { websiteLink: nonprofit.websiteLink, imageUrl: nonprofit.imageUrl, whatDoesNonprofitDo: nonprofit.whatDoesNonprofitDo, - interestedCampers: nonprofit.interestedCampers + interestedCampers: nonprofit.interestedCampers, + assignedCampers: nonprofit.assignedCampers, + buttonActive: buttonActive }); }); }; @@ -144,13 +160,18 @@ exports.showAllNonprofits = function(req, res) { }; exports.interestedInNonprofit = function(req, res) { - - if (req.user.uncompletedBonfires !== [] && req.user.uncompletedCoursewares !== []) { - Nonprofit.find({name: req.params.nonprofitName.replace(/-/, ' ')}, function(err, nonprofit) { + if (req.user) { + Nonprofit.findOne({name: new RegExp(req.params.nonprofitName.replace(/-/, ' '), 'i')}, function(err, nonprofit) { if (err) { return next(err); } - nonprofit.interestedCampers.push({"name": req.user.username, "picture": req.user.picture, "timeOfInterest": Date.now()}); - req.flash('success', { msg: 'Thanks for expressing interest in this nonprofit project!' }); + nonprofit.interestedCampers.push({"username": req.user.profile.username, + "picture": req.user.profile.picture, + "timeOfInterest": Date.now() + }); + nonprofit.save(function(err) { + if (err) { return done(err); } + req.flash('success', { msg: "Thanks for expressing interest in this nonprofit project! We've added you to this project as an interested camper!" }); + res.redirect('back'); + }); }); } - res.redirect('back'); }; diff --git a/models/Nonprofit.js b/models/Nonprofit.js index 4a1c76e574..39f77085e0 100644 --- a/models/Nonprofit.js +++ b/models/Nonprofit.js @@ -6,7 +6,7 @@ var secrets = require('../config/secrets'); * @type {exports.Schema} */ - +var Long = mongoose.Types.Long; var nonprofitSchema = new mongoose.Schema({ name: String, requestedDeliverables: Array, diff --git a/views/account/show.jade b/views/account/show.jade index 49348ce1fb..0389bb0283 100644 --- a/views/account/show.jade +++ b/views/account/show.jade @@ -98,8 +98,6 @@ block content .hidden-xs.col-sm-12 #cal-heatmap.img-center - script(src="//d3js.org/d3.v3.min.js") - script(src="//cdn.jsdelivr.net/cal-heatmap/3.3.10/cal-heatmap.min.js") script. var cal = new CalHeatMap(); var calendar = !{JSON.stringify(calender)}; diff --git a/views/nonprofits/show.jade b/views/nonprofits/show.jade index 80e0ffeb92..ed90b630c6 100644 --- a/views/nonprofits/show.jade +++ b/views/nonprofits/show.jade @@ -21,11 +21,13 @@ block content h4= whatDoesNonprofitDo h4 a(href=websiteLink)= websiteLink + .spacer h3 Project Description: .col-xs-12 h4.negative-15 #{projectDescription} (About #{estimatedHours} hours per camper) + .spacer h3 This project involves building: - .col-xs-12 + h4.negative-15.col-xs-12 if (approvedWebsite) .ion-android-globe   Website if (approvedDonor) @@ -42,18 +44,30 @@ block content .ion-university   E-learning Platform if (approvedOther) .ion-settings   Other tools + if (interestedCampers && interestedCampers.length > 0) + h3 Interested campers: + .col-xs-12.text-left + for interestedCamper in interestedCampers + a(href='/' + interestedCamper.username class="interested-camper-image") + img.profile-picture.float-right(src=interestedCamper.picture) + if (assignedCampers && assignedCampers.length > 0) + h3 Assigned campers: + .col-xs-12.text-left + for assignedCamper in assignedCampers + a(href='/' + assignedCamper.username class="interested-camper-image") + img.profile-picture.float-right(src=assignedCamper.picture) .spacer - .col-xs-12.text-center - h3 Interested campers: - for interestedCamper in interestedCampers - a(href='/' + interestedCamper.username class="interested-camper-image") - img.profile-picture.float-right(src=interestedUser.profile.picture) - .col-xs-12.text-center - h3 Assigned campers: - .col-xs-12.col-sm-8.col-sm-offset-2 - .text-center - a.btn.btn-primary.btn-big.btn-block(href='/nonprofits/interested-in-nonprofit/#{dashedName}') I'm interested in building this project - #showAllButton.btn.btn-info.btn-big.btn-block Show all Nonprofits Projects + if (!buttonActive) + .col-xs-12.col-sm-8.col-sm-offset-2 + .text-center + a.btn.btn-primary.btn-big.btn-block.disabled(href='/nonprofits/interested-in-nonprofit/#{dashedName}') I'm interested in building this project * + p * Complete all our Bonfires, 10 Ziplines, and 3 Basejumps to unlock this. + #showAllButton.btn.btn-info.btn-big.btn-block Show all Nonprofits Projects + if (buttonActive) + .col-xs-12.col-sm-8.col-sm-offset-2 + .text-center + a.btn.btn-primary.btn-big.btn-block(href='/nonprofits/interested-in-nonprofit/#{dashedName}') I'm interested in building this project + #showAllButton.btn.btn-info.btn-big.btn-block Show all Nonprofits Projects .row .col-xs-12.text-center if !user diff --git a/views/partials/universal-head.jade b/views/partials/universal-head.jade index 27ef5ba82e..4928e35797 100644 --- a/views/partials/universal-head.jade +++ b/views/partials/universal-head.jade @@ -1,6 +1,8 @@ script(src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js") script(src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js") script(src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.0/ui-bootstrap-tpls.min.js") +script(src="//d3js.org/d3.v3.min.js") +script(src="//cdn.jsdelivr.net/cal-heatmap/3.3.10/cal-heatmap.min.js") include meta title #{title} | Free Code Camp meta(charset='utf-8')