|
3 | 3 |
|
4 | 4 | angular.module('tc.skill-picker').controller('SkillPickerController', SkillPickerController);
|
5 | 5 |
|
6 |
| - SkillPickerController.$inject = ['CONSTANTS', 'ProfileService', '$state', 'userProfile', 'featuredSkills', '$log', 'toaster']; |
| 6 | + SkillPickerController.$inject = ['$scope', 'CONSTANTS', 'ProfileService', '$state', 'userProfile', 'featuredSkills', '$log', 'toaster', 'MemberCertService', '$q']; |
7 | 7 |
|
8 |
| - function SkillPickerController(CONSTANTS, ProfileService, $state, userProfile, featuredSkills, $log, toaster) { |
| 8 | + function SkillPickerController($scope, CONSTANTS, ProfileService, $state, userProfile, featuredSkills, $log, toaster, MemberCertService, $q) { |
9 | 9 | var vm = this;
|
10 | 10 | $log = $log.getInstance("SkillPickerController");
|
11 | 11 | vm.ASSET_PREFIX = CONSTANTS.ASSET_PREFIX;
|
| 12 | + vm.IOS_PROGRAM_ID = CONSTANTS.SWIFT_PROGRAM_ID; |
12 | 13 | vm.submitSkills = submitSkills;
|
13 | 14 | vm.featuredSkills = featuredSkills;
|
| 15 | + vm.userId = userProfile.userId; |
14 | 16 | vm.username = userProfile.handle;
|
15 | 17 | vm.toggleSkill = toggleSkill;
|
16 | 18 | vm.tracks = {};
|
17 | 19 | vm.mySkills = [];
|
18 | 20 | vm.disableDoneButton = false;
|
| 21 | + vm.showCommunity = false; |
| 22 | + vm.loadingCommunities = false; |
| 23 | + vm.communities = {}; |
| 24 | + vm.isPageDirty = isPageDirty; |
19 | 25 | ///////
|
20 | 26 | activate();
|
21 | 27 |
|
| 28 | + /** |
| 29 | + * Activates the controller. |
| 30 | + */ |
22 | 31 | function activate() {
|
23 |
| - $log.debug("init") |
| 32 | + $log.debug("init"); |
| 33 | + initCommunities(); |
| 34 | + checkCommunityStatus(); |
24 | 35 | }
|
25 | 36 |
|
| 37 | + /** |
| 38 | + * Verfies if the page state has been modified by the user in any way. |
| 39 | + */ |
| 40 | + function isPageDirty() { |
| 41 | + return isTracksDirty() || isCommunitiesDirty(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Verfies if the tracks section state has been modified by the user in any way. |
| 46 | + */ |
| 47 | + function isTracksDirty() { |
| 48 | + return vm.tracks.DESIGN || vm.tracks.DEVELOP || vm.tracks.DATA_SCIENCE; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Verfies if the communities section state has been modified by the user in any way. |
| 53 | + */ |
| 54 | + function isCommunitiesDirty() { |
| 55 | + var community = _.findWhere(vm.communities, {dirty: true}); |
| 56 | + return !!community; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Initializes the communities to show in the communities section. |
| 61 | + */ |
| 62 | + function initCommunities() { |
| 63 | + vm.communities['ios'] = { displayName: 'iOS', programId: vm.IOS_PROGRAM_ID, status: false, dirty: false, display: true}; |
| 64 | + _addWatchToCommunity(vm.communities['ios']); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Helper method to add watch to given object. |
| 69 | + */ |
| 70 | + function _addWatchToCommunity(community) { |
| 71 | + community.unregister = $scope.$watch( |
| 72 | + function() { return community; }, |
| 73 | + function(newValue, oldValue) { |
| 74 | + if (oldValue && newValue.status !== oldValue.status) { |
| 75 | + newValue.dirty = oldValue.dirty ? false : true; |
| 76 | + } |
| 77 | + }, |
| 78 | + true |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks registration status of each community and updates the state of each community. |
| 84 | + */ |
| 85 | + function checkCommunityStatus() { |
| 86 | + var promises = []; |
| 87 | + for (var name in vm.communities) { |
| 88 | + var community = vm.communities[name]; |
| 89 | + promises.push(MemberCertService.getMemberRegistration(vm.userId, community.programId)); |
| 90 | + } |
| 91 | + vm.loadingCommunities = true; |
| 92 | + $q.all(promises).then(function(responses) { |
| 93 | + vm.loadingCommunities = false; |
| 94 | + responses.forEach(function(program) { |
| 95 | + if (program) { |
| 96 | + var community = _.findWhere(vm.communities, {programId: program.eventId}); |
| 97 | + if (community) { |
| 98 | + // set display false to avoid showing already enabled/registered program |
| 99 | + // we expect display property to be modified after first load of the page |
| 100 | + community.display = false; |
| 101 | + community.status = true; |
| 102 | + if (community.unregister){ |
| 103 | + community.unregister(); |
| 104 | + _addWatchToCommunity(community); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + // if there exists at least 1 community which can be displayed, set showCommunity flag to true |
| 110 | + var community = _.findWhere(vm.communities, {display: true}); |
| 111 | + if (community) { |
| 112 | + vm.showCommunity = true; |
| 113 | + } |
| 114 | + }) |
| 115 | + .catch(function(error) { |
| 116 | + vm.loadingCommunities = false; |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Toggles the given skill for the user. If it is not added, adds it and if already added, removes it. |
| 122 | + */ |
26 | 123 | function toggleSkill(tagId) {
|
27 | 124 | var _idx = vm.mySkills.indexOf(tagId.toString());
|
28 | 125 | if (_idx > -1) {
|
|
34 | 131 | }
|
35 | 132 | }
|
36 | 133 |
|
| 134 | + /** |
| 135 | + * Persists the user's altered information. |
| 136 | + */ |
37 | 137 | function submitSkills() {
|
38 | 138 |
|
39 | 139 | vm.saving = true;
|
40 |
| - // save tracks |
41 |
| - userProfile.tracks = _.reduce(vm.tracks, function(result, isInterested, trackName) { |
42 |
| - if (isInterested) { |
43 |
| - result.push(trackName); |
44 |
| - } |
45 |
| - return result; |
46 |
| - }, []); |
47 | 140 |
|
48 |
| - userProfile.save().then(function(data) { |
49 |
| - if (vm.mySkills.length > 0) { |
50 |
| - // save skills |
51 |
| - var data = {}; |
52 |
| - for (var i = 0; i < vm.mySkills.length; i++) { |
53 |
| - data[vm.mySkills[i]] = { |
54 |
| - hidden: false |
55 |
| - }; |
| 141 | + var promises = []; |
| 142 | + if (isTracksDirty()) { |
| 143 | + // save tracks |
| 144 | + userProfile.tracks = _.reduce(vm.tracks, function(result, isInterested, trackName) { |
| 145 | + if (isInterested) { |
| 146 | + result.push(trackName); |
| 147 | + } |
| 148 | + return result; |
| 149 | + }, []); |
| 150 | + promises.push(userProfile.save()); |
| 151 | + } |
| 152 | + if (vm.mySkills.length > 0) { |
| 153 | + // save skills |
| 154 | + var data = {}; |
| 155 | + for (var i = 0; i < vm.mySkills.length; i++) { |
| 156 | + data[vm.mySkills[i]] = { |
| 157 | + hidden: false |
| 158 | + }; |
| 159 | + } |
| 160 | + promises.push(ProfileService.updateUserSkills(vm.username, data)); |
| 161 | + } |
| 162 | + $log.debug('isCommunitiesDirty: ' + isCommunitiesDirty()); |
| 163 | + if (isCommunitiesDirty()) { |
| 164 | + for(var communityName in vm.communities) { |
| 165 | + var community = vm.communities[communityName]; |
| 166 | + if (community.dirty === true) { |
| 167 | + if (community.status === true) { |
| 168 | + promises.push(MemberCertService.registerMember(vm.userId, community.programId)); |
56 | 169 | }
|
57 |
| - ProfileService.updateUserSkills(vm.username, data) |
58 |
| - .then(function(resp) { |
59 |
| - vm.saving = false; |
60 |
| - toaster.pop('success', "Success!", "Your skills have been updated."); |
61 |
| - vm.disableDoneButton = true; |
62 |
| - $state.go('dashboard'); |
63 |
| - }) |
64 |
| - .catch(function(data) { |
65 |
| - vm.saving = false; |
66 |
| - toaster.pop('error', "Whoops", "Something went wrong. Please try again later."); |
67 |
| - }) |
68 |
| - } else { |
69 |
| - vm.saving = false; |
70 |
| - toaster.pop('success', "Success!", "Your skills have been updated."); |
71 |
| - vm.disableDoneButton = true; |
72 |
| - $state.go('dashboard'); |
73 | 170 | }
|
| 171 | + } |
| 172 | + } |
74 | 173 |
|
75 |
| - }) |
76 |
| - .catch(function(resp) { |
77 |
| - vm.saving = false; |
78 |
| - toaster.pop('error', "Whoops", "Something went wrong. Please try again later."); |
79 |
| - }) |
80 |
| - |
| 174 | + $q.all(promises).then(function(responses) { |
| 175 | + vm.saving = false; |
| 176 | + toaster.pop('success', "Success!", "Your skills have been updated."); |
| 177 | + vm.disableDoneButton = true; |
| 178 | + //$state.go('dashboard'); |
| 179 | + }) |
| 180 | + .catch(function(resp) { |
| 181 | + vm.saving = false; |
| 182 | + toaster.pop('error', "Whoops!", "Something went wrong. Please try again later."); |
| 183 | + }); |
81 | 184 | }
|
82 | 185 | }
|
83 | 186 | })();
|
0 commit comments