diff --git a/app/profile/about/about.controller.js b/app/profile/about/about.controller.js index fb67c2438..21d46984e 100644 --- a/app/profile/about/about.controller.js +++ b/app/profile/about/about.controller.js @@ -3,9 +3,9 @@ angular.module('tc.profile').controller('ProfileAboutController', ProfileAboutController); - ProfileAboutController.$inject = ['$log', '$scope', 'ProfileService', 'ExternalAccountService']; + ProfileAboutController.$inject = ['$log', '$scope', 'ProfileService', 'ExternalAccountService', 'UserService', 'CONSTANTS']; - function ProfileAboutController($log, $scope, ProfileService, ExternalAccountService, UserService) { + function ProfileAboutController($log, $scope, ProfileService, ExternalAccountService, UserService, CONSTANTS) { var vm = this; $log = $log.getInstance("ProfileAboutController"); var profileVm = $scope.$parent.profileVm; @@ -24,11 +24,35 @@ profileVm.externalLinksPromise.then(function() { vm.linkedExternalAccountsData = profileVm.linkedExternalAccountsData; - vm.linkedExternalAccounts = profileVm.linkedExternalAccounts; + // show section if user is viewing his/her own profile OR if we have data - vm.hasLinks = vm.linkedExternalAccounts.length; - //vm.hasLinks = _.any(_.valuesIn(_.omit(vm.linkedExternalAccountsData, ['userId', 'updatedAt','createdAt','createdBy','updatedBy','handle']))); + //vm.hasLinks = profileVm.linkedExternalAccounts.length; + vm.hasLinks = _.any(_.valuesIn(_.omit(vm.linkedExternalAccountsData, ['userId', 'updatedAt','createdAt','createdBy','updatedBy','handle']))); vm.displaySection.externalLinks = profileVm.showEditProfileLink || vm.hasLinks; + + // if user is authenticated, call for profiles end point + if (profileVm.isUser) { + var userId = UserService.getUserIdentity().userId; + ExternalAccountService.getLinkedExternalAccounts(userId).then(function(data) { + vm.linkedExternalAccounts = data; + profileVm.status.externalLinks = CONSTANTS.STATE_READY; + }).catch(function(err) { + profileVm.status.externalLinks = CONSTANTS.STATE_ERROR; + }); + } else { + vm.linkedExternalAccounts = []; + // remove all keys except the provider keys + var accounts = _.omit(vm.linkedExternalAccountsData, ['userId', 'updatedAt','createdAt','createdBy','updatedBy','handle']); + // populate the externalLinks for external-account-data directive with info from ext accounts data + for(var provider in accounts) { + if (accounts[provider]) { + vm.linkedExternalAccounts.push({ + providerType: provider + }); + } + } + profileVm.status.externalLinks = CONSTANTS.STATE_READY; + } }); profileVm.statsPromise.then(function() { diff --git a/app/profile/profile.controller.js b/app/profile/profile.controller.js index 51e8aea3a..2aea6d266 100644 --- a/app/profile/profile.controller.js +++ b/app/profile/profile.controller.js @@ -3,12 +3,12 @@ angular.module('tc.profile').controller('ProfileCtrl', ProfileCtrl); - ProfileCtrl.$inject = ['CONSTANTS', '$log', + ProfileCtrl.$inject = ['CONSTANTS', '$log', '$q', 'TcAuthService', 'UserService', 'ProfileService', 'ChallengeService', 'ExternalAccountService', 'userHandle', 'profile', 'ngDialog' ]; - function ProfileCtrl(CONSTANTS, $log, TcAuthService, UserService, ProfileService, ChallengeService, ExternalAccountService, userHandle, profile, ngDialog) { + function ProfileCtrl(CONSTANTS, $log, $q, TcAuthService, UserService, ProfileService, ChallengeService, ExternalAccountService, userHandle, profile, ngDialog) { var vm = this; // set profile to the object that was resolved vm.profile = profile; @@ -58,17 +58,12 @@ }); - vm.externalLinksPromise = ExternalAccountService.getLinkedExternalAccounts(profile.userId).then(function(data) { - vm.linkedExternalAccounts = data; - vm.status.externalLinks = CONSTANTS.STATE_READY; + vm.externalLinksPromise = ExternalAccountService.getLinkedExternalLinksData(vm.userHandle).then(function(data) { + vm.linkedExternalAccountsData = data.plain(); }).catch(function(err) { vm.status.externalLinks = CONSTANTS.STATE_ERROR; }); - ExternalAccountService.getLinkedExternalLinksData(vm.userHandle).then(function(data) { - vm.linkedExternalAccountsData = data.plain(); - }); - function activate() { $log.debug('Calling ProfileController activate()'); // show edit profile link if user is authenticated and is viewing their own profile