From 379b31820241397b14eface689bdfd8e4e58c703 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 11 Sep 2016 21:59:18 -0500 Subject: [PATCH] fix(client): Allow bulk task adding in challenges closes #6781 fixes #6723 --- .../client/js/controllers/challengesCtrl.js | 60 +++++++++++-------- website/client/js/controllers/tasksCtrl.js | 41 ++++--------- website/client/js/services/taskServices.js | 29 ++++++++- .../views/shared/tasks/task_view/add_new.jade | 4 +- 4 files changed, 75 insertions(+), 59 deletions(-) diff --git a/website/client/js/controllers/challengesCtrl.js b/website/client/js/controllers/challengesCtrl.js index 6d1f4f9dab..02ed54154f 100644 --- a/website/client/js/controllers/challengesCtrl.js +++ b/website/client/js/controllers/challengesCtrl.js @@ -257,28 +257,42 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', //------------------------------------------------------------ // Tasks //------------------------------------------------------------ - function addTask (addTo, listDef, challenge) { - var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type}); - //If the challenge has not been created, we bulk add tasks on save - if (challenge._id) Tasks.createChallengeTasks(challenge._id, task); - if (!challenge[task.type + 's']) challenge[task.type + 's'] = []; - challenge[task.type + 's'].unshift(task); - delete listDef.newTask; + function addChallengeTasks (listDef, challenge, tasks) { + var type = listDef.type; + + // If the challenge has not been created, we bulk add tasks on save + tasks = tasks.map(function (task) { + return Shared.taskDefaults({ + text: task, + type: type, + }); + }); + + type = type + 's'; + + if (challenge._id) { + Tasks.createChallengeTasks(challenge._id, tasks).then(function (res) { + addToList(challenge, type, res.data.data); + }); + } else { + addToList(challenge, type, tasks); + } }; - $scope.addTask = function(addTo, listDef, challenge) { - if (listDef.bulk) { - var tasks = listDef.newTask.split(/[\n\r]+/); - //Reverse the order of tasks so the tasks will appear in the order the user entered them - tasks.reverse(); - _.each(tasks, function(t) { - listDef.newTask = t; - addTask(addTo, listDef, challenge); - }); - listDef.bulk = false; - } else { - addTask(addTo, listDef, challenge); + function addToList (challenge, type, tasks) { + if (!_.isArray(tasks)) { + tasks = [tasks]; } + if (!challenge[type]) { + challenge[type] = []; + } + challenge[type].unshift.apply(challenge[type], tasks); + } + + $scope.addTask = function(listDef, challenge) { + Tasks.addTasks(listDef, function (listDef, tasks) { + addChallengeTasks(listDef, challenge, tasks); + }); } $scope.removeTask = function(task, challenge) { @@ -294,13 +308,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', task._editing = false; } - $scope.toggleBulk = function(list) { - if (typeof list.bulk === 'undefined') { - list.bulk = false; - } - list.bulk = !list.bulk; - list.focus = true; - }; + $scope.toggleBulk = Tasks.toggleBulk; /* -------------------------- diff --git a/website/client/js/controllers/tasksCtrl.js b/website/client/js/controllers/tasksCtrl.js index 77ee93b20d..2b8e0f3d66 100644 --- a/website/client/js/controllers/tasksCtrl.js +++ b/website/client/js/controllers/tasksCtrl.js @@ -30,42 +30,25 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N Analytics.updateUser(); }; - function addTask(addTo, listDef, tasks) { - tasks = _.isArray(tasks) ? tasks : [tasks]; - + function addUserTasks(listDef, tasks) { + tasks = tasks.map(function (task) { + return { + text: task, + type: listDef.type, + tags: _.keys(User.user.filters), + }; + }); User.addTask({ - body: tasks.map(function (task) { - return { - text: task, - type: listDef.type, - tags: _.keys(User.user.filters), - } - }), + body: tasks, }); } - $scope.addTask = function(addTo, listDef) { - if (listDef.bulk) { - var tasks = listDef.newTask.split(/[\n\r]+/); - //Reverse the order of tasks so the tasks will appear in the order the user entered them - tasks.reverse(); - addTask(addTo, listDef, tasks); - listDef.bulk = false; - } else { - addTask(addTo, listDef, listDef.newTask); - } - delete listDef.newTask; - delete listDef.focus; + $scope.addTask = function(listDef) { + Tasks.addTasks(listDef, addUserTasks); if (listDef.type=='daily') Guide.goto('intro', 2); }; - $scope.toggleBulk = function(list) { - if (typeof list.bulk === 'undefined') { - list.bulk = false; - } - list.bulk = !list.bulk; - list.focus = true; - }; + $scope.toggleBulk = Tasks.toggleBulk; $scope.editTask = Tasks.editTask; diff --git a/website/client/js/services/taskServices.js b/website/client/js/services/taskServices.js index 7396f9f749..54cba505ca 100644 --- a/website/client/js/services/taskServices.js +++ b/website/client/js/services/taskServices.js @@ -5,6 +5,29 @@ var TASK_KEYS_TO_REMOVE = ['_id', 'completed', 'date', 'dateCompleted', 'history angular.module('habitrpg') .factory('Tasks', ['$rootScope', 'Shared', '$http', function tasksFactory($rootScope, Shared, $http) { + function addTasks(listDef, addTaskFn) { + var tasks = listDef.newTask; + + if (listDef.bulk) { + tasks = tasks.split(/[\n\r]+/); + // Reverse the order of tasks so the tasks + // will appear in the order the user entered them + tasks.reverse(); + listDef.bulk = false; + } else { + tasks = [tasks]; + } + + addTaskFn(listDef, tasks); + + delete listDef.newTask; + delete listDef.focus; + } + + function toggleBulk (list) { + list.bulk = !list.bulk; + list.focus = true; + }; function getUserTasks (getCompletedTodos) { var url = '/api/v3/tasks/user'; @@ -33,11 +56,11 @@ angular.module('habitrpg') }); }; - function createChallengeTasks (challengeId, taskDetails) { + function createChallengeTasks (challengeId, tasks) { return $http({ method: 'POST', url: '/api/v3/tasks/challenge/' + challengeId, - data: taskDetails, + data: tasks, }); }; @@ -181,6 +204,8 @@ angular.module('habitrpg') } return { + addTasks: addTasks, + toggleBulk: toggleBulk, getUserTasks: getUserTasks, loadedCompletedTodos: false, createUserTasks: createUserTasks, diff --git a/website/views/shared/tasks/task_view/add_new.jade b/website/views/shared/tasks/task_view/add_new.jade index 2d4fd1c869..bdd50ac987 100644 --- a/website/views/shared/tasks/task_view/add_new.jade +++ b/website/views/shared/tasks/task_view/add_new.jade @@ -1,5 +1,5 @@ -form.task-add(name='new{{list.type}}form', ng-hide='obj._locked', ng-submit='addTask(obj[list.type+"s"], list, obj)', novalidate) - textarea(rows='6', focus-element='list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolderBulk}}', ng-if='list.bulk', ui-keydown='{"meta-enter ctrl-enter":"addTask(obj[list.type+\'s\'],list)"}', required) +form.task-add(name='new{{list.type}}form', ng-hide='obj._locked', ng-submit='addTask(list, obj)', novalidate) + textarea(rows='6', focus-element='list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolderBulk}}', ng-if='list.bulk', ui-keydown='{"meta-enter ctrl-enter":"addTask(list, obj)"}', required) input(type='text', focus-element='!list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolder}}', ng-if='!list.bulk', required) button(type='submit', ng-disabled='new{{list.type}}form.$invalid') div(ng-show='new{{list.type}}form.$invalid', tooltip=env.t("emptyTask"))