repo-list: dedupe repos

This commit is contained in:
Jimmy Zelinskie 2015-02-26 13:58:29 -05:00
parent 6e1877084d
commit d4884d47b4

View file

@ -16,7 +16,7 @@
}]); }]);
function RepoListCtrl($scope, $sanitize, Restangular, UserService, ApiService) { function RepoListCtrl($scope, $sanitize, $q, Restangular, UserService, ApiService) {
$scope.namespace = null; $scope.namespace = null;
$scope.page = 1; $scope.page = 1;
$scope.publicPageCount = null; $scope.publicPageCount = null;
@ -45,6 +45,7 @@
} }
loadStarredRepos(); loadStarredRepos();
loadRepos(); loadRepos();
deduplicateRepos();
} }
} }
); );
@ -64,7 +65,7 @@
}; };
ApiService.createStar(data).then(function(result) { ApiService.createStar(data).then(function(result) {
repo.is_starred = true; repo.is_starred = true;
refreshStars(); $scope.starred_repositories.value.push(repo);
}, ApiService.errorDisplay('Could not star repository')); }, ApiService.errorDisplay('Could not star repository'));
}; };
@ -74,20 +75,36 @@
}; };
ApiService.deleteStar(null, data).then(function(result) { ApiService.deleteStar(null, data).then(function(result) {
repo.is_starred = false; repo.is_starred = false;
refreshStars(); $scope.starred_repositories.value = $scope.starred_repositories.value.filter(function(repo) {
return repo.is_starred;
});
}, ApiService.errorDisplay('Could not unstar repository')); }, ApiService.errorDisplay('Could not unstar repository'));
}; };
var refreshStars = function() { var deduplicateRepos = function() {
var starred = []; // Wait for namespaces to load.
$scope.namespaces.map(function(namespace) { $scope.namespaces.then(function() {
namespace.repositories.value.map(function(repository) { // Wait for both starred repos and each individual namespace's repos to load.
if (repository.is_starred) { var waitList = [$scope.starred_repositories].concat($scope.namespaces.repositories);
starred.push(repository); $q.all(waitList).then(function() {
var starred = {};
// Cache starred repos.
$scope.starred_repositories.value.map(function(repo) {
starred[repo.namspace + '/' + repo.name] = repo;
});
// If we find one of the starred repos elsewhere, replace it with
// the same object.
$scope.namespaces.value.map(function(namespace) {
namespace.repositories.value.map(function(repo) {
var found = starred[repo.namespace + '/' + repo.name];
if (found) {
repo = found;
} }
}); });
}); });
$scope.starred_repositories.value = starred; });
});
}; };
var loadStarredRepos = function() { var loadStarredRepos = function() {