gaf-mobile
Version:
GAF mobile Web site
213 lines (189 loc) • 6.46 kB
JavaScript
module('gafMobileApp')
/**
* @ngdoc controller
* @name gafMobileApp.UserProfileCtrl
* @description
* Controller for the user profiles
*/
.controller('UserProfileCtrl', function($route, $q, $location, $scope,
Reviews, Pager, Currencies, Threads, Projects, Auth, Bids, Analytics,
loggedInUser, Experiments){
var _this = this;
var params = $location.search();
_this.showHireMeLoggedIn = false;
_this.shouldShowCreateMilestoneModal = false;
_this.user = $route.current.locals.user.get();
_this.userAvatar = $route.current.locals.user.getAvatarUrl();
_this.skills = [];
_this.reviews = [];
_this.currencies = [];
_this.project = {};
if (angular.isDefined(loggedInUser)) {
_this.userCurrency = loggedInUser.get().primary_currency;
}
if (Auth.isAuthenticated()) {
// TO DO: Clean up once OnlineOffline AB Test is done
_this.showOnlineOfflineStatus =
Experiments.activateTest('OnlineOfflineStatus', true);
}
var selectTab = $location.hash();
$scope.tab = {
name: selectTab ? selectTab : 'userDetails'
};
// Change tab and update the location.
_this.changeTab = function(tab) {
if ($scope.tab.name !== tab) {
$scope.tab.name = tab;
$location.hash(tab);
// We use replace() here to update the current history record
// instead of creating a new one.
$location.replace();
}
};
var loadReviews = Pager(Reviews.getListForUser.bind(Reviews), 5, 1);
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#loadUserReviews
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Load the list of reviews for this user
*/
this.loadUserReviews = function() {
return loadReviews.nextPage(_this.user.id, {
user_details: true,
'review_types[]': ['project', 'contest']
})
.then(function(response) {
_this.reviews = _this.reviews.concat(response.getReviews());
_this.hasMoreReviews = loadReviews.hasNext(_this.reviews);
});
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#showUserSkills
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Display 5 skills on the view
*/
this.showUserSkills = function() {
var currentNumberOfSkills = _this.skills.length;
_this.skills = _this.skills.concat(_this.user.jobs
.slice(currentNumberOfSkills, currentNumberOfSkills + 5));
_this.hasMoreSkills = _this.skills.length < _this.user.jobs.length;
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#loadCurrencyList
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Load the list of all currencies available on the website so we can
* populate the currency dropdown on the view
*/
this.loadCurrencyList = function() {
return Currencies.get()
.then(function(response) {
_this.currencyList = response.getList();
// Preselect US dollars if no other currency
if (angular.isUndefined(_this.userCurrency)) {
_this.userCurrency = response.getById(1).get();
}
});
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#isUserAuthenticated
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Returns true if user is authenticated
*/
this.isUserAuthenticated = function() {
return Auth.isAuthenticated();
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#isLoggedInUser
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Returns true if the logged in user is viewing their own profile.
*/
this.isLoggedInUser = function() {
return Auth.getUserId() === _this.user.id;
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#showHiremeModal
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Shows the Hireme modal if employer is logged in, else will redirect to the
* signup page. The specified user and currency objects will be used if given,
* else will use the default values.
*/
_this.showHiremeModal = function(currency, resetCurrentProject) {
if (!Auth.isAuthenticated()) {
$location.url('/signup?role=employer&return=' +
encodeURIComponent($location.path() + '?showhireme=true'));
} else {
// clear the info on the saved current project
if (resetCurrentProject) {
Projects.setCurrent();
}
_this.shouldShowHiremeModal = true;
_this.userCurrency = currency || _this.userCurrency;
}
};
_this.closeHiremeModalCallback = function() {
_this.shouldShowHiremeModal = false;
_this.project = Projects.getCurrent();
if (_this.project.get().id) { // project is not empty
_this.shouldShowCreateMilestoneModal = true;
}
};
/**
* @ngdoc method
* @name gafMobileApp.UserProfileCtrl#successCallback
* @methodOf gafMobileApp.UserProfileCtrl
* @description
* Redirects the user to PVP when create milestone modal was closed
*/
_this.closeCreateMilestoneModalCallback = function() {
_this.shouldShowCreateMilestoneModal = false;
$location.url('/projects/project-' + _this.project.get().id);
};
this.loadUserReviews();
this.showUserSkills();
this.loadCurrencyList();
// Continue creating Hireme project after deposit success
if (params.showhireme && params.deposit === 'success') {
var currentProject = Projects.getCurrent().get();
if (currentProject.hireme && currentProject.user &&
currentProject.user.id === _this.user.id && currentProject.currency) {
_this.showHiremeModal(currentProject.currency);
} else {
// This happens if the employer deposited using Paypal since the page is
// reloaded after a successful deposit, erasing the saved current project
// We should still show the Hireme modal with the default values
_this.showHiremeModal();
}
} else {
// When true, the hireme modal will be visible
if (!_this.isLoggedInUser() && params.showhireme) {
_this.showHiremeModal(null, true);
}
}
if(!_this.isLoggedInUser() && !params.showhireme &&
params.project && params.bid) {
// get freelancer's bid info
Bids.getList({ 'bids[]': [params.bid] })
.then(function(result) {
_this.bid = result.getBidsList()[0];
_this.projectSeoUrl = params.project;
})
.catch(function() {
_this.showHireMeLoggedIn = true;
});
} else {
_this.showHireMeLoggedIn = _this.isUserAuthenticated() &&
!_this.isLoggedInUser();
}
});
;
angular.