Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

SUP-2728, Add iOS community to Onboarding page (Skill picker) #613

Merged
merged 2 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 143 additions & 40 deletions app/skill-picker/skill-picker.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,123 @@

angular.module('tc.skill-picker').controller('SkillPickerController', SkillPickerController);

SkillPickerController.$inject = ['CONSTANTS', 'ProfileService', '$state', 'userProfile', 'featuredSkills', '$log', 'toaster'];
SkillPickerController.$inject = ['$scope', 'CONSTANTS', 'ProfileService', '$state', 'userProfile', 'featuredSkills', '$log', 'toaster', 'MemberCertService', '$q'];

function SkillPickerController(CONSTANTS, ProfileService, $state, userProfile, featuredSkills, $log, toaster) {
function SkillPickerController($scope, CONSTANTS, ProfileService, $state, userProfile, featuredSkills, $log, toaster, MemberCertService, $q) {
var vm = this;
$log = $log.getInstance("SkillPickerController");
vm.ASSET_PREFIX = CONSTANTS.ASSET_PREFIX;
vm.IOS_PROGRAM_ID = CONSTANTS.SWIFT_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;
///////
activate();

/**
* Activates the controller.
*/
function activate() {
$log.debug("init")
$log.debug("init");
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 isCommunitiesDirty() {
var community = _.findWhere(vm.communities, {dirty: true});
return !!community;
}

/**
* Initializes the communities to show in the communities section.
*/
function initCommunities() {
vm.communities['ios'] = { displayName: 'iOS', programId: vm.IOS_PROGRAM_ID, status: false, dirty: false, display: true};
_addWatchToCommunity(vm.communities['ios']);
}

/**
* 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 = _.findWhere(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.display = false;
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 = _.findWhere(vm.communities, {display: true});
if (community) {
vm.showCommunity = true;
}
})
.catch(function(error) {
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) {
Expand All @@ -34,50 +131,56 @@
}
}

/**
* Persists the user's altered information.
*/
function submitSkills() {

vm.saving = true;
// save tracks
userProfile.tracks = _.reduce(vm.tracks, function(result, isInterested, trackName) {
if (isInterested) {
result.push(trackName);
}
return result;
}, []);

userProfile.save().then(function(data) {
if (vm.mySkills.length > 0) {
// save skills
var data = {};
for (var i = 0; i < vm.mySkills.length; i++) {
data[vm.mySkills[i]] = {
hidden: false
};
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));
}
$log.debug('isCommunitiesDirty: ' + isCommunitiesDirty());
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));
}
ProfileService.updateUserSkills(vm.username, data)
.then(function(resp) {
vm.saving = false;
toaster.pop('success', "Success!", "Your skills have been updated.");
vm.disableDoneButton = true;
$state.go('dashboard');
})
.catch(function(data) {
vm.saving = false;
toaster.pop('error', "Whoops", "Something went wrong. Please try again later.");
})
} else {
vm.saving = false;
toaster.pop('success', "Success!", "Your skills have been updated.");
vm.disableDoneButton = true;
$state.go('dashboard');
}
}
}

})
.catch(function(resp) {
vm.saving = false;
toaster.pop('error', "Whoops", "Something went wrong. Please try again later.");
})

$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(resp) {
vm.saving = false;
toaster.pop('error', "Whoops!", "Something went wrong. Please try again later.");
});
}
}
})();
21 changes: 20 additions & 1 deletion app/skill-picker/skill-picker.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@

p.instruction Hi {{vm.username}}! Your account is now active. To help other members get to know you, select the tracks in which you're interested, and specify some of your skills. You can edit this information later on your Profile.

.communities(ng-show="!vm.loadingCommunities && vm.showCommunity")
.communities-title Communities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the BEM naming convention! We should use double underscores to signify elements, e.g. .communities__title, community__title--disabled.

.communities-description Topcoder regularly establishes exclusive communities to help members develop expertise and earn money in particular technologies. Join a featured community to be notified of events and challenges.
.communities-list
.community(ng-repeat="(communityKey, community) in vm.communities", ng-class="{'disabled': !community.status}", ng-if="community.display")
.community-details
.community-icon(ng-class="{'disabled': !community.status}")
img(ng-if="communityKey == 'ios' && community.status", src="/images/ico-ios-community.svg")
img(ng-if="communityKey == 'ios' && !community.status", src="/images/ico-ios-community-grey.svg")

.community-text
span.community-title(class="{{!community.status && 'disabled'}}") {{community.displayName}}
.community-description
span(ng-if="communityKey == 'ios'") Mobile app design and development for iOS, with Swift emphasis

onoff-switch(model="community.status", unique-id="'community-' + communityKey")



.tracks-container
.title tracks
.description Topcoder's three categories of challenges… please pick at least one based on your skills and interests.
Expand Down Expand Up @@ -51,4 +70,4 @@
type="button",
tc-busy-button, tc-busy-when="vm.saving",
ng-click="vm.submitSkills()",
ng-disabled="vm.disableDoneButton || (!vm.tracks.DESIGN && !vm.tracks.DEVELOP && !vm.tracks.DATA_SCIENCE)") Done
ng-disabled="vm.disableDoneButton || !vm.isPageDirty()") Done
Loading