diff --git a/public/js/controllers/groupsCtrl.js b/public/js/controllers/groupsCtrl.js
index 8edb73809e..815001dbb4 100644
--- a/public/js/controllers/groupsCtrl.js
+++ b/public/js/controllers/groupsCtrl.js
@@ -192,8 +192,8 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
}])
- .controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope', '$state', '$location', '$compile',
- function($scope, Groups, User, $rootScope, $state, $location, $compile) {
+ .controller("GuildsCtrl", ['$scope', 'Groups', 'User', 'Challenges', '$rootScope', '$state', '$location', '$compile',
+ function($scope, Groups, User, Challenges, $rootScope, $state, $location, $compile) {
$scope.groups = {
guilds: Groups.myGuilds(),
"public": Groups.publicGuilds()
@@ -240,7 +240,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
$scope.leave = function(keep) {
if (keep == 'cancel') {
- $scope.selectedChal = undefined;
+ $scope.selectedGroup = undefined;
} else {
var group = $scope.selectedGroup;
Groups.Group.leave({gid: group._id, keep:keep}, undefined, function(){
@@ -263,16 +263,30 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
$scope.clickLeave = function(group, $event){
$scope.selectedGroup = group;
$scope.popoverEl = $($event.target);
- var html = $compile(
+ var html, title;
+ Challenges.Challenge.query(function(challenges) {
+ challenges = _.pluck(_.filter(challenges, function(c) {
+ return c.group._id == group._id;
+ }), '_id');
+ if (_.intersection(challenges, User.user.challenges).length > 0) {
+ html = $compile(
'Remove Tasks
\nKeep Tasks
\nCancel
'
)($scope);
+ title = "Leave group challenges and...";
+ } else {
+ html = $compile(
+ 'Confirm
\nCancel
'
+ )($scope);
+ title = "Leave group?"
+ }
$scope.popoverEl.popover('destroy').popover({
html: true,
placement: 'top',
trigger: 'manual',
- title: 'Leaving group challenges and...',
+ title: title,
content: html
}).popover('show');
+ });
}
$scope.reject = function(guild){
diff --git a/src/controllers/groups.js b/src/controllers/groups.js
index f2d8faa36f..35f096873f 100644
--- a/src/controllers/groups.js
+++ b/src/controllers/groups.js
@@ -316,6 +316,8 @@ api.leave = function(req, res, next) {
// Unlink the challenge tasks from user
async.waterfall(challenges.map(function(chal) {
return function(cb) {
+ var i = user.challenges.indexOf(chal._id)
+ if (~i) user.challenges.splice(i,1);
user.unlink({cid:chal._id, keep:keep}, function(err){
if (err) return cb(err);
cb(null);