gaf-mobile
Version:
GAF mobile Web site
221 lines (197 loc) • 6.44 kB
JavaScript
'use strict';
angular.module('gafMobileApp')
/**
* @ngdoc directive
* @name gafMobileApp.skillSelect
* @description
* Component for updating logged-in user jobs
*/
.directive('skillSelect', function() {
return {
restrict: 'E',
templateUrl: 'components/skill-select/skill-select.html',
scope: {},
controller: 'SkillSelectCtrl',
controllerAs: 'ctrl',
bindToController: true
};
});
angular.module('gafMobileApp')
.controller('SkillSelectCtrl', function($q, $location, Jobs, Users) {
var _this = this;
/**
* @ngdoc property
* @name gafMobileApp.skillSelect#userJobIds
* @propertyOf gafMobileApp.skillSelect
* @description
* Array IDs of the user's current jobs
*/
var userJobIds = [];
/**
* @ngdoc property
* @name gafMobileApp.skillSelect#loading
* @propertyOf gafMobileApp.skillSelect
* @description
* Stores the current loading state of the page
*/
_this.loading = true;
/**
* @ngdoc property
* @name gafMobileApp.skillSelect#selectedCount
* @propertyOf gafMobileApp.skillSelect
* @description
* Stores the user's total number of jobs
*/
_this.selectedCount = 0;
/**
* @ngdoc property
* @name gafMobileApp.skillSelect#searchQuery
* @propertyOf gafMobileApp.skillSelect
* @description
* Stores the query string used for searching jobs
*/
_this.searchQuery = '';
/**
* @ngdoc property
* @name gafMobileApp.skillSelect#jobs
* @propertyOf gafMobileApp.skillSelect
* @description
* Stores all the available jobs with attributes:
* `selected` set to TRUE if job exists in user's current jobs
* `new` set is TRUE if job is selected from search
*/
_this.jobs = [];
/**
* @ngdoc method
* @name gafMobileApp.skillSelect#addJob
* @methodOf gafMobileApp.skillSelect
* @param {Object} job Job object to be added to user's jobs
* @description
* Sets `new` attribute of the job and increments `selectedCount`
*/
_this.addJob = function(job) {
if (_this.selectedCount < _this.jobLimit) {
job.selected = true;
job.new = true;
job.delete = false;
_this.selectedCount += 1;
}
};
/**
* @ngdoc method
* @name gafMobileApp.skillSelect#removeJob
* @methodOf gafMobileApp.skillSelect
* @param {Object} job Job object to be removed from user's selected jobs
* @description
* Sets `new` attribute of the job to FALSE and decrements `selectedCount`
*/
_this.removeJob = function(job) {
_this.selectedCount -= 1;
job.selected = false;
job.new = false;
job.delete = true;
};
/**
* @ngdoc method
* @name gafMobileApp.skillSelect#clearSearchField
* @methodOf gafMobileApp.skillSelect
* @description
* Clears `searchQuery`
*/
_this.clearSearchField = function() {
_this.searchQuery = '';
};
/**
* @ngdoc method
* @name gafMobileApp.skillSelect#saveSkills
* @methodOf gafMobileApp.skillSelect
* @description
* Updates the user jobs with the selected job ids
*/
_this.saveSkills = function() {
_this.error = {};
_this.savingSkills = true;
var task;
var jobIds = [];
angular.forEach(_this.jobs, function(job) {
if (job.new && userJobIds.indexOf(job.id) === -1) {
jobIds.push(job.id);
}
});
var deletejobIds = [];
angular.forEach(_this.jobs, function(job) {
if (job.delete && userJobIds.indexOf(job.id) !== -1) {
deletejobIds.push(job.id);
}
});
if(deletejobIds.length === 0 && jobIds.length === 0) {
if ($location.search().return &&
// Handle hash to go to bid form after skill selector
$location.search().return.indexOf('/projects') !== -1) {
$location.url($location.search().return + '#placebid');
} else {
$location.url('/dashboard');
}
}
task = deletejobIds.length > 0 ? Users.removeJobs(deletejobIds) :
Users.setJobs(jobIds);
return task.then(function() {
if ($location.search().return &&
// Handle hash to go to bid form after skill selector
$location.search().return.indexOf('/projects') !== -1) {
$location.url($location.search().return + '#placebid');
} else {
$location.url('/dashboard');
}
if (jobIds.length > 0) { Users.setJobs(jobIds); }
}).catch(function(error) {
_this.savingSkills = false;
_this.error.internalError = error.code;
});
};
/**
* @ngdoc method
* @name gafMobileApp.skillSelect#getSuggestedCount
* @methodOf gafMobileApp.skillSelect
* @description
* Returns the count of suggested yet unselected skills
*/
_this.getSuggestedCount = function() {
return _this.jobs.filter(function(job) {
return job.suggested && !job.selected;
}).length;
};
// From Jobs service, get list of jobs
// From Users service, get the users skills and membership details
$q.all([
Jobs.getList({ active_project_count_details: true }),
Users.getLoggedInUser({ jobs: true, membership_details: true })
]).then(function(res) {
_this.jobs = res[0].getList();
_this.user = res[1].get();
_this.loading = false;
_this.jobLimit = _this.user.membership_package.job_limit;
_this.selectedCount = _this.user.jobs.length;
angular.forEach(_this.user.jobs, function(userJob) {
userJobIds.push(userJob.id);
});
angular.forEach(_this.jobs, function(job) {
job.selected = userJobIds.indexOf(job.id) !== -1;
});
// If job params is provided (set of suggested job ids to add)
if($location.search().jobs) {
var jobParams = $location.search().jobs.split(',');
angular.forEach(jobParams, function(jobId) {
// Check if the user still does not have the job
if(userJobIds.indexOf(parseInt(jobId)) === -1) {
// Get the job object and add the job to selected skill list
angular.forEach(_this.jobs, function(job) {
if(job.id === parseInt(jobId)) {
job.suggested = true;
}
});
}
});
}
});
});