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

Commit c84af59

Browse files
author
Parth Shah
committed
major refactoring, moving data processing to services
1 parent dbaaab5 commit c84af59

22 files changed

+707
-445
lines changed

app/directives/external-account/external-account.directive.js

Lines changed: 21 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
(function() {
22
'use strict';
33
var _supportedAccounts = [
4-
{ provider: "dribbble", className: "fa-dribbble", displayName: "Dribbble", disabled: false, order: 6, colorClass: 'el-dribble'},
4+
{ provider: "dribbble", className: "fa-dribbble", displayName: "Dribbble", disabled: false, order: 6, colorClass: 'el-dribbble'},
55
{ provider: "linkedin", className: "fa-linkedin", displayName: "LinkedIn", disabled: true, order: 5, colorClass: 'el-linkedin'},
66
{ provider: "stackoverflow", className: "fa-stack-overflow", displayName: "Stack Overflow", disabled: false, order: 3, colorClass: 'el-stackoverflow'},
77
{ provider: "behance", className: "fa-behance", displayName: "Behance", disabled: true, order: 2, colorClass: 'el-behance'},
8-
// { provider: "google-oauth2", className: "fa-google-plus", displayName: "Google+", disabled: true, order: }, colorClass: 'el-dribble',
98
{ provider: "github", className: "fa-github", displayName: "Github", disabled: false, order: 1, colorClass: 'el-github'},
109
{ provider: "bitbucket", className: "fa-bitbucket", displayName: "Bitbucket", disabled: false, order: 7, colorClass: 'el-bitbucket'},
1110
{ provider: "twitter", className: "fa-twitter", displayName: "Twitter", disabled: true, order: 4, colorClass: 'el-twitter'},
12-
//{ provider: "weblinks", className: "fa-globe", displayName: "Web Links", disabled: true, order: 8, colorClass: 'el-weblinks'}
11+
{ provider: "weblink", className: "fa-globe", displayName: "Web Links", disabled: true, order: -1, colorClass: 'el-weblinks'}
1312
// TODO add more
1413
];
1514

@@ -20,28 +19,31 @@
2019
templateUrl: 'directives/external-account/external-account.directive.html',
2120
scope: {
2221
linkedAccounts: '=',
23-
linksData: '=',
22+
// linksData: '=',
2423
readOnly: '='
2524
},
2625
controller: ['$log', '$scope', 'ExternalAccountService', 'toaster',
2726
function($log, $scope, ExternalAccountService, toaster) {
27+
2828
$log = $log.getInstance("ExtAccountDirectiveCtrl")
29-
$scope.accountList = _.clone(_supportedAccounts, true);
29+
30+
var _accountList = _.clone(_supportedAccounts, true);
31+
_.remove(_accountList, function(al) { return al.order < 0});
3032
$scope.$watchCollection('linkedAccounts', function(newValue, oldValue) {
31-
for (var i=0;i<$scope.accountList.length;i++) {
32-
var _idx = _.findIndex(newValue, function(a) {
33-
return $scope.accountList[i].provider === a.providerType;
34-
});
35-
if (_idx == -1) {
36-
$scope.accountList[i].status = 'unlinked';
37-
} else {
38-
// check if data
39-
if ($scope.linksData[$scope.accountList[i].provider]) {
40-
$scope.accountList[i].status = 'linked';
33+
if (newValue && newValue != oldValue) {
34+
angular.forEach(_accountList, function(account) {
35+
var _linkedAccount = _.find(newValue, function(p) {
36+
return p.provider === account.provider
37+
});
38+
if (!_linkedAccount) {
39+
account.status = 'unlinked';
40+
} else if(_linkedAccount.status && _linkedAccount.status.toLowerCase() === 'pending') {
41+
account.status = 'pending';
4142
} else {
42-
$scope.accountList[i].status = 'pending';
43+
account.status = 'linked';
4344
}
44-
}
45+
});
46+
$scope.accountList = _accountList;
4547
}
4648
});
4749

@@ -89,11 +91,11 @@
8991
.then(function(resp) {
9092
$log.debug("Social account unlinked: " + JSON.stringify(resp));
9193
var toRemove = _.findIndex($scope.linkedAccounts, function(la) {
92-
return la.providerType === provider.provider;
94+
return la.provider === provider.provider;
9395
});
9496
// remove from both links array and links data array
9597
$scope.linkedAccounts.splice(toRemove, 1);
96-
delete $scope.linksData[provider.provider];
98+
// delete $scope.linksData[provider.provider];
9799
toaster.pop('success', "Success",
98100
String.supplant(
99101
"Your {provider} account has been unlinked.",
@@ -117,106 +119,6 @@
117119
]
118120
};
119121
})
120-
.directive('externalLinksData', function() {
121-
return {
122-
restrict: 'E',
123-
templateUrl: 'directives/external-account/external-link-data.directive.html',
124-
scope: {
125-
linkedAccountsData: '=',
126-
externalLinks: '='
127-
},
128-
controller: ['$log', '$scope', 'ExternalAccountService',
129-
function($log, $scope, ExternalAccountService) {
130-
$log = $log.getInstance('ExternalLinksDataDirective');
131-
function reCalcData(links, data) {
132-
$scope.linkedAccounts = [];
133-
angular.forEach(links, function(link) {
134-
var provider = link.providerType;
135-
136-
if (!data[provider]) {
137-
$scope.linkedAccounts.push({
138-
provider: provider,
139-
data: {
140-
handle: link.name,
141-
status: 'PENDING'
142-
}
143-
});
144-
} else {
145-
// add data
146-
$scope.linkedAccounts.push({
147-
provider: provider,
148-
data: data[provider]
149-
});
150-
}
151-
});
152-
}
153-
154-
155-
$scope.$watch('linkedAccountsData', function(newValue, oldValue) {
156-
reCalcData($scope.externalLinks, newValue);
157-
});
158-
159-
$scope.$watchCollection('externalLinks', function(newValue, oldValue) {
160-
reCalcData(newValue, $scope.linkedAccountsData);
161-
});
162-
}
163-
]
164-
}
165-
})
166-
.directive('externalWebLink', function() {
167-
return {
168-
restrict: 'E',
169-
templateUrl: 'directives/external-account/external-web-link.directive.html',
170-
scope: {
171-
linkedAccounts: '=',
172-
userData: "="
173-
},
174-
controller: ['$log', '$scope', 'ExternalAccountService',
175-
function($log, $scope, ExternalAccountService) {
176-
$log = $log.getInstance('ExternalWebLinkDirective');
177-
$scope.addingWebLink = false;
178-
$scope.errorMessage = null;
179-
180-
$log.debug("userData: " + $scope.userData.handle);
181-
182-
$scope.addWebLink = function() {
183-
$log.debug("URL: " + $scope.url);
184-
$scope.addingWebLink = true;
185-
$scope.errorMessage = null;
186-
ExternalAccountService.addWebLink($scope.userData.userId, $scope.userData.handle, $scope.url)
187-
.then(function(resp) {
188-
$scope.addingWebLink = false;
189-
$log.debug("Web link added: " + JSON.stringify(resp));
190-
$scope.linkedAccounts.push(resp.profile);
191-
toaster.pop('success', "Success",
192-
String.supplant(
193-
"Your link has been added. Data from your link will be visible on your profile shortly.",
194-
{provider: extAccountProvider}
195-
)
196-
);
197-
})
198-
.catch(function(resp) {
199-
$scope.addingWebLink = false;
200-
$log.debug(JSON.stringify(resp));
201-
if (resp.status === 'SOCIAL_PROFILE_ALREADY_EXISTS') {
202-
$log.info("Social profile already linked to another account");
203-
toaster.pop('error', "Whoops!",
204-
String.supplant(
205-
"This {provider} account is linked to another account. \
206-
If you think this is an error please contact <a href=\"mailTo:[email protected]\">[email protected]</a>.",
207-
{provider: extAccountProvider }
208-
)
209-
);
210-
} else {
211-
$log.info("Server error:" + resp.content);
212-
$scope.errorMessage = "Sorry, we are unable add web link. If problem persist please contact [email protected]";
213-
}
214-
});
215-
};
216-
}
217-
]
218-
}
219-
})
220122
.filter('providerData', function() {
221123
return function(input, field) {
222124
return _.result(_.find(_supportedAccounts, function(s) {

app/directives/external-account/external-account.directive.spec.js

Lines changed: 18 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,44 @@ describe('External Accounts Directive', function() {
1414
describe('Linked external accounts', function() {
1515
var linkedAccounts = [
1616
{
17-
providerType: 'linkedin',
17+
provider: 'linkedin',
1818
// don't care about other details
1919
},
2020
{
21-
providerType: 'github'
21+
provider: 'github'
2222
}
2323
];
24-
var linksData = {
25-
'linkedin' : {provider: 'linkedin', name: 'name-linkedin'},
26-
'github' : {provider: 'github', name: 'name-github'}
27-
};
2824
var externalAccounts;
2925

3026
beforeEach(function() {
3127
scope.linkedAccounts = linkedAccounts;
32-
scope.linksData = linksData;
33-
element = angular.element('<external-accounts linked-accounts="linkedAccounts", links-data="linksData"></external-accounts>)');
28+
element = angular.element('<external-accounts linked-accounts="linkedAccounts"></external-accounts>)');
3429
externalAccounts = $compile(element)(scope);
35-
scope.$digest();
36-
// scope.$apply();
30+
// externalAccounts.scope().$digest();
31+
// externalAccounts.scope().$apply();
3732
});
3833

3934
it('should have added account list to scope', function() {
40-
expect(element.isolateScope().accountList).to.exist;
41-
35+
inject(function($timeout) {
36+
scope.$digest();
37+
$timeout(function() {
38+
expect(element.isolateScope().accountList).to.exist;
39+
}, 0)
40+
});
4241
});
4342

4443
it('should have "linked" property set for github & linkedin', function() {
45-
var githubAccount = _.find(element.isolateScope().accountList, function(a) { return a.provider === 'github'});
46-
expect(githubAccount).to.have.property('status')
47-
.that.equals('linked');
44+
inject(function($timeout) {
45+
scope.$digest();
46+
$timeout(function() {
47+
var githubAccount = _.find(element.isolateScope().accountList, function(a) { return a.provider === 'github'});
48+
expect(githubAccount).to.have.property('status').that.equals('linked');
49+
}, 0)
50+
});
4851

4952
// var linkeindAccount = _.find(element.isolateScope().accountList, function(a) { return a.provider === 'linkedin'});
5053
// expect(linkeindAccount).to.have.property('linked')
5154
// .that.equals(true);
5255
});
5356
});
5457
});
55-
56-
describe('External Links Data Directive', function() {
57-
var scope;
58-
var element;
59-
60-
beforeEach(function() {
61-
bard.appModule('topcoder');
62-
bard.inject(this, '$compile', '$rootScope');
63-
scope = $rootScope.$new();
64-
});
65-
66-
bard.verifyNoOutstandingHttpRequests();
67-
68-
describe('Linked external accounts', function() {
69-
var externalLinks = [
70-
{
71-
providerType: 'linkedin',
72-
// don't care about other details
73-
},
74-
{
75-
providerType: 'github'
76-
},
77-
{
78-
providerType: 'behance'
79-
},
80-
{
81-
providerType: 'dribbble'
82-
},
83-
{
84-
providerType: 'bitbucket'
85-
}
86-
];
87-
var linkedAccounts = {
88-
github: {
89-
handle: "github-handle",
90-
followers: 1,
91-
publicRepos: 1
92-
},
93-
stackoverflow: {
94-
handle: 'so-handle',
95-
reputation: 2,
96-
answers: 2
97-
},
98-
behance: {
99-
name: 'behance name',
100-
projectViews: 3,
101-
projectAppreciations: 3
102-
},
103-
dribbble: {
104-
handle: 'dribble-handle',
105-
followers: 4,
106-
likes: 4
107-
},
108-
bitbucket: {
109-
username: 'bitbucket-username',
110-
followers: 5,
111-
repositories: 5
112-
},
113-
twitter: {
114-
handle: 'twitter-handle',
115-
noOfTweets: 6,
116-
followers: 6
117-
},
118-
linkedin: {
119-
name: 'linkedin name',
120-
title: 'linkedin title'
121-
}
122-
};
123-
var externalLinksData;
124-
125-
beforeEach(function() {
126-
scope.linkedAccounts = linkedAccounts;
127-
scope.externalLinks = externalLinks;
128-
element = angular.element('<external-links-data linked-accounts-data="linkedAccounts" external-links="externalLinks"></external-links-data>)');
129-
externalLinksData = $compile(element)(scope);
130-
scope.$digest();
131-
});
132-
133-
it('should have added linkedAccounts to scope', function() {
134-
expect(element.isolateScope().linkedAccounts).to.exist;
135-
// linkedAccounts should have 5 entries because externalLinks contains only 5 records
136-
expect(element.isolateScope().linkedAccounts).to.have.length(5);
137-
});
138-
139-
});
140-
});

app/directives/external-account/external-link-data.directive.jade

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.external-link-list
2-
div.external-link-tile(ng-repeat="account in linkedAccounts")
2+
div.external-link-tile(ng-repeat="account in linkedAccountsData")
33
.top
44
div.logo
55
i.fa(ng-class="(account|providerData:'className') || 'fa-globe'")
@@ -98,7 +98,7 @@
9898

9999
.title {{account.data.title}}
100100

101-
div(ng-switch-default)
101+
div(ng-switch-when="weblink")
102102
.handle My Portfoilo
103103

104-
.title www.TBD.com
104+
.title {{account.data.title}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function() {
2+
'use strict';
3+
4+
/**
5+
* @desc links data card directive
6+
* @example <external-links-data></external-links-data>
7+
*/
8+
angular
9+
.module('tcUIComponents')
10+
.directive('externalLinksData', externalLinksData);
11+
12+
function externalLinksData() {
13+
var directive = {
14+
restrict: 'E',
15+
templateUrl: 'directives/external-account/external-link-data.directive.html',
16+
scope: {
17+
linkedAccountsData: '='
18+
}
19+
};
20+
return directive;
21+
}
22+
})();

0 commit comments

Comments
 (0)