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

Feature/sup 2754 allow hiding external links #597

Merged
merged 11 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
.external-link-list
div.external-link-tile(ng-repeat="account in linkedAccountsData")
div.external-link-tile(ng-repeat="account in linkedAccountsData", ng-class="{'external-link-tile--editable' : editable}")
.top
.ext-link-tile_edit-header(ng-show="editable && account.provider === 'weblink'")
.ext-link-tile_edit-header_delete(ng-click="confirmDeletion(account)", ng-class="{'ext-link-tile_edit-header_delete--disabled': account.deletingAccount || account.status === 'PENDING'}")
div.logo
i.fa(ng-class="(account|providerData:'className') || 'fa-globe'")
h2 {{account|providerData:"displayName"}}

div.bottom(ng-switch="account.provider")
div.bottom(ng-if="account.deletingAccount")
.section-loading
div.bottom(ng-switch="account.provider", ng-if="!account.deletingAccount")

div(ng-switch-when="github")
.handle {{account.data.handle}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.deletion-confirmation
.deletion-confirmation-title Heads Up!
.deletion-confirmation-message Are you sure you want to delete the external link #[span.deletion-confirmation-account-title "{{vm.account.title}}"]? This action can't be undone later.
.deletion-confirmation-buttons
.deletion-confirmation-button-yes
button.tc-btn.tc-btn-s.tc-btn-ghost(ng-click="vm.deleteAccount() && closeThisDialog()") Yes, Delete Link
.deletion-confirmation-button-no
button.tc-btn.tc-btn-s(ng-click="closeThisDialog()") Cancel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(function () {

angular
.module('tcUIComponents')
.controller('ExternalLinkDeletionController', ExternalLinkDeletionController);

ExternalLinkDeletionController.$inject = ['ExternalWebLinksService', '$q', '$log', 'toaster', 'ngDialog', 'userHandle', 'account', 'linkedAccountsData'];

function ExternalLinkDeletionController(ExternalWebLinksService, $q, $log, toaster, ngDialog, userHandle, account, linkedAccountsData) {
var vm = this;
vm.account = account;
$log = $log.getInstance("ExternalLinkDeletionController");

vm.deleteAccount = function() {
$log.debug('Deleting Account...');
if (account && account.deletingAccount) {
$log.debug('Another deletion is already in progress.');
return;
}
if (account && account.provider === 'weblink') {
account.deletingAccount = true;
$log.debug('Deleting weblink...');
return ExternalWebLinksService.removeLink(userHandle, account.key).then(function(data) {
account.deletingAccount = false;
$log.debug("Web link removed: " + JSON.stringify(data));
var toRemove = _.findIndex(linkedAccountsData, function(la) {
return la.provider === 'weblink' && la.key === account.key;
});
if (toRemove > -1) {
// remove from the linkedAccountsData array
linkedAccountsData.splice(toRemove, 1);
}
toaster.pop('success', "Success", "Your link has been removed.");
})
.catch(function(resp) {
var msg = resp.msg;
if (resp.status === 'WEBLINK_NOT_EXIST') {
$log.info("Weblink does not exist");
msg = "Weblink is not linked to your account. If you think this is an error please contact <a href=\"mailTo:[email protected]\">[email protected]</a>.";
} else {
$log.error("Fatal error: _unlink: " + msg);
msg = "Sorry! We are unable to remove your weblink. If problem persists, please contact <a href=\"mailTo:[email protected]\">[email protected]</a>";
}

account.deletingAccount = false;
toaster.pop('error', "Whoops!", msg);
});
}
}
}
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/* jshint -W117, -W030 */
describe('External Link Deletion Controller', function() {
var scope;
var element;
var toasterSvc, extLinkSvc, ngDialogSvc;
var mockLinkedAccounts = [
{
provider: 'github',
data: {
handle: "github-handle",
followers: 1,
publicRepos: 1
}
},
{ provider: 'stackoverflow',
data: {
handle: 'so-handle',
reputation: 2,
answers: 2
}
},
{
provider: 'behance',
data: {
name: 'behance name',
projectViews: 3,
projectAppreciations: 3
}
},
{
provider: 'dribbble',
data: {
handle: 'dribbble-handle',
followers: 4,
likes: 4
}
},
{
provider: 'bitbucket',
data: {
username: 'bitbucket-username',
followers: 5,
repositories: 5
}
},
{
provider: 'twitter',
data: {
handle: 'twitter-handle',
noOfTweets: 6,
followers: 6
}
},
{
provider: 'linkedin',
data: {
status: 'pending'
}
},
{
provider: 'weblink',
key: 'somekey'
}
];
var createController = function(toDelete, linkedAccounts) {
return $controller('ExternalLinkDeletionController', {
ExternalWebLinksService : extLinkSvc,
toaster: toasterSvc,
userHandle: 'test',
account: toDelete,
linkedAccountsData: linkedAccounts
});
}

beforeEach(function() {
bard.appModule('topcoder');
bard.inject(this, '$compile', '$rootScope', 'toaster', 'ExternalWebLinksService', '$q', 'ngDialog', '$controller');
scope = $rootScope.$new();

extLinkSvc = ExternalWebLinksService;

sinon.stub(extLinkSvc, 'removeLink', function(handle, key) {
var $deferred = $q.defer();
if (key === 'throwNotExistsError') {
$deferred.reject({
status: 'WEBLINK_NOT_EXIST',
msg: 'profile not exists'
});
} else if(key === 'throwFatalError') {
$deferred.reject({
status: 'FATAL_ERROR',
msg: 'fatal error'
});
} else {
$deferred.resolve({
status: 'SUCCESS'
});
}
return $deferred.promise;
});

toasterSvc = toaster;
bard.mockService(toaster, {
pop: $q.when(true),
default: $q.when(true)
});

ngDialogSvc = ngDialog;
sinon.stub(ngDialog, 'open', function() {
ngDialog.deferredClose = $q.defer();
return { closePromise : ngDialog.deferredClose.promise };
});
sinon.stub(ngDialog, 'close', function() {
ngDialog.deferredClose.resolve('closing');
return
})
});

bard.verifyNoOutstandingHttpRequests();

describe('Linked external accounts', function() {
var linkedAccounts = null;
var externalLinksData;

beforeEach(function() {
linkedAccounts = angular.copy(mockLinkedAccounts);
});

afterEach(function() {
linkedAccounts = angular.copy(mockLinkedAccounts);
});

it('should remove weblink ', function() {
var toDelete = {key: 'somekey', provider: 'weblink'};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(toasterSvc.pop).to.have.been.calledWith('success').calledOnce;
expect(linkedAccounts).to.have.length(7);
});

it('should show success if controller doesn\'t have weblink but API returns success ', function() {
var toDelete = {key: 'somekey1', provider: 'weblink'};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(toasterSvc.pop).to.have.been.calledWith('success').calledOnce;
expect(linkedAccounts).to.have.length(8);
});

it('should NOT remove weblink with fatal error ', function() {
var toDelete = {key: 'throwFatalError', provider: 'weblink'};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('Sorry!')).calledOnce;
expect(linkedAccounts).to.have.length(8);
});

it('should NOT remove weblink with already removed weblink ', function() {
var toDelete = {key: 'throwNotExistsError', provider: 'weblink'};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('not linked')).calledOnce;
expect(linkedAccounts).to.have.length(8);
});

it('should not do any thing when already a deletion is in progress ', function() {
var toDelete = {key: 'somekey', provider: 'weblink', deletingAccount: true};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(extLinkSvc.removeLink).not.to.be.called;
expect(toasterSvc.pop).not.to.be.called;
expect(linkedAccounts).to.have.length(8);
});

it('should not do any thing for non weblink provider ', function() {
var toDelete = {key: 'somekey', provider: 'stackoverflow'};
ctrl = createController(toDelete, linkedAccounts);
ctrl.deleteAccount();
$rootScope.$apply();
expect(extLinkSvc.removeLink).not.to.be.called;
expect(toasterSvc.pop).not.to.be.called;
expect(linkedAccounts).to.have.length(8);
});

});
});
35 changes: 33 additions & 2 deletions app/directives/external-account/external-links-data.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,39 @@
restrict: 'E',
templateUrl: 'directives/external-account/external-link-data.directive.html',
scope: {
linkedAccountsData: '='
}
linkedAccountsData: '=',
editable: '=',
userHandle: '@'
},
controller: ['$log', '$scope', 'ExternalWebLinksService', 'toaster', 'ngDialog',
function($log, $scope, ExternalWebLinksService, toaster, ngDialog) {

$log = $log.getInstance("ExternalLinksDataCtrl");
$scope.deletionDialog = null;

$scope.confirmDeletion = function(account) {
$scope.deletionDialog = ngDialog.open({
className: 'ngdialog-theme-default tc-dialog',
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of passing account to be deleted & deleteAccount() on scope, provide a controller to handle this. Additional tracking with $scope.toDelete seems error prone and not "encapsulated".
https://github.com/likeastore/ngDialog#controller-string--array--object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@parthshah I thought of the same, but reason behind not using controller for this as of now, is that I was thinking of creating a generic directive for showing a confirmation popup. May be we can use ngDialog's openConfirm itself for this. However, I was not sure, if I can style it as per our requirements and it can take some more time, hence, I chose to implement directly in external links data directive. Please let me know, if you think it would be a good idea to have a generic directive for showing the confirmation popup.

template: 'directives/external-account/external-link-deletion-confirm.html',
controller: 'ExternalLinkDeletionController',
controllerAs: 'vm',
resolve: {
userHandle: function() {
return $scope.userHandle;
},
account: function() {
return account;
},
linkedAccountsData: function() {
return $scope.linkedAccountsData;
}
}
}).closePromise.then(function (data) {
$log.debug('Closing deletion confirmation dialog.');
});
}
}
]
};
return directive;
}
Expand Down
Loading