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.page = 1;
$scope.publicPageCount = null;
@ -45,6 +45,7 @@
}
loadStarredRepos();
loadRepos();
deduplicateRepos();
}
}
);
@ -64,7 +65,7 @@
};
ApiService.createStar(data).then(function(result) {
repo.is_starred = true;
refreshStars();
$scope.starred_repositories.value.push(repo);
}, ApiService.errorDisplay('Could not star repository'));
};
@ -74,20 +75,36 @@
};
ApiService.deleteStar(null, data).then(function(result) {
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'));
};
var refreshStars = function() {
var starred = [];
$scope.namespaces.map(function(namespace) {
namespace.repositories.value.map(function(repository) {
if (repository.is_starred) {
starred.push(repository);
}
var deduplicateRepos = function() {
// Wait for namespaces to load.
$scope.namespaces.then(function() {
// Wait for both starred repos and each individual namespace's repos to load.
var waitList = [$scope.starred_repositories].concat($scope.namespaces.repositories);
$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() {