This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathskill-picker.controller.js
227 lines (206 loc) · 6.85 KB
/
skill-picker.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import angular from 'angular'
import _ from 'lodash'
(function() {
'use strict'
angular.module('tc.skill-picker').controller('SkillPickerController', SkillPickerController)
SkillPickerController.$inject = ['$scope', 'CONSTANTS', 'ProfileService', '$state', 'userProfile', 'featuredSkills', 'logger', 'toaster', 'MemberCertService', '$q']
function SkillPickerController($scope, CONSTANTS, ProfileService, $state, userProfile, featuredSkills, logger, toaster, MemberCertService, $q) {
var vm = this
vm.ASSET_PREFIX = CONSTANTS.ASSET_PREFIX
vm.IOS_PROGRAM_ID = parseInt(CONSTANTS.SWIFT_PROGRAM_ID)
vm.PREDIX_PROGRAM_ID = parseInt(CONSTANTS.PREDIX_PROGRAM_ID)
vm.IBM_COGNITIVE_PROGRAM_ID = parseInt(CONSTANTS.IBM_COGNITIVE_PROGRAM_ID)
vm.submitSkills = submitSkills
vm.featuredSkills = featuredSkills
vm.userId = userProfile.userId
vm.username = userProfile.handle
vm.toggleSkill = toggleSkill
vm.tracks = {}
vm.mySkills = []
vm.disableDoneButton = false
vm.showCommunity = false
vm.loadingCommunities = false
vm.communities = {}
vm.isPageDirty = isPageDirty
vm.isTracksDirty = isTracksDirty
vm.isCommunitySelected = isCommunitySelected
///////
activate()
/**
* Activates the controller.
*/
function activate() {
initCommunities()
checkCommunityStatus()
}
/**
* Verfies if the page state has been modified by the user in any way.
*/
function isPageDirty() {
return isTracksDirty() || isCommunitiesDirty()
}
/**
* Verfies if the tracks section state has been modified by the user in any way.
*/
function isTracksDirty() {
return vm.tracks.DESIGN || vm.tracks.DEVELOP || vm.tracks.DATA_SCIENCE
}
/**
* Verfies if the communities section state has been modified by the user in any way.
*/
function isCommunitySelected() {
var community = _.find(vm.communities, {status: true, display: true})
return !!community
}
/**
* Verfies if the communities section state has been modified by the user in any way.
*/
function isCommunitiesDirty() {
var community = _.find(vm.communities, {dirty: true})
return !!community
}
/**
* Initializes the communities to show in the communities section.
*/
function initCommunities() {
vm.communities['ibm_cognitive'] = {
displayName: 'Cognitive',
programId: vm.IBM_COGNITIVE_PROGRAM_ID,
status: true,
dirty: true,
display: true
}
vm.communities['ios'] = {
displayName: 'iOS',
programId: vm.IOS_PROGRAM_ID,
status: false,
dirty: false,
display: true
}
vm.communities['predix'] = {
displayName: 'Predix',
programId: vm.PREDIX_PROGRAM_ID,
status: false,
dirty: false,
display: true
}
_addWatchToCommunity(vm.communities['ios'])
_addWatchToCommunity(vm.communities['predix'])
_addWatchToCommunity(vm.communities['ibm_cognitive'])
}
/**
* Helper method to add watch to given object.
*/
function _addWatchToCommunity(community) {
community.unregister = $scope.$watch(
function() { return community },
function(newValue, oldValue) {
if (oldValue && newValue.status !== oldValue.status) {
newValue.dirty = oldValue.dirty ? false : true
}
},
true
)
}
/**
* Checks registration status of each community and updates the state of each community.
*/
function checkCommunityStatus() {
var promises = []
for (var name in vm.communities) {
var community = vm.communities[name]
promises.push(MemberCertService.getMemberRegistration(vm.userId, community.programId))
}
vm.loadingCommunities = true
$q.all(promises)
.then(function(responses) {
vm.loadingCommunities = false
responses.forEach(function(program) {
if (program) {
var community = _.find(vm.communities, {programId: program.eventId})
if (community) {
// set display false to avoid showing already enabled/registered program
// we expect display property to be modified after first load of the page
// community.status = true
if (community.unregister){
community.unregister()
_addWatchToCommunity(community)
}
}
}
})
// if there exists at least 1 community which can be displayed, set showCommunity flag to true
var community = _.find(vm.communities, {display: true})
if (community) {
vm.showCommunity = true
}
})
.catch(function(err) {
logger.error('Could not load communities with member cert registration data', err)
vm.loadingCommunities = false
})
}
/**
* Toggles the given skill for the user. If it is not added, adds it and if already added, removes it.
*/
function toggleSkill(tagId) {
var _idx = vm.mySkills.indexOf(tagId.toString())
if (_idx > -1) {
// remove
vm.mySkills.splice(_idx, 1)
} else {
// add
vm.mySkills.push(tagId.toString())
}
}
/**
* Persists the user's altered information.
*/
function submitSkills() {
vm.saving = true
var promises = []
if (isTracksDirty()) {
// save tracks
userProfile.tracks = _.reduce(vm.tracks, function(result, isInterested, trackName) {
if (isInterested) {
result.push(trackName)
}
return result
}, [])
promises.push(userProfile.save())
}
if (vm.mySkills.length > 0) {
// save skills
var data = {}
for (var i = 0; i < vm.mySkills.length; i++) {
data[vm.mySkills[i]] = {
hidden: false
}
}
promises.push(ProfileService.updateUserSkills(vm.username, data))
}
if (isCommunitiesDirty()) {
for(var communityName in vm.communities) {
var community = vm.communities[communityName]
if (community.dirty === true) {
if (community.status === true) {
promises.push(MemberCertService.registerMember(vm.userId, community.programId))
}
}
}
}
$q.all(promises)
.then(function(responses) {
vm.saving = false
toaster.pop('success', 'Success!', 'Your skills have been updated.')
vm.disableDoneButton = true
$state.go('dashboard')
})
.catch(function(err) {
logger.error('Could not update update user skills or register members for community', err)
vm.saving = false
toaster.pop('error', 'Whoops!', 'Something went wrong. Please try again later.')
})
}
}
})()