From d4884d47b44809e8f2dbc8352d792cc79fd6d27e Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 26 Feb 2015 13:58:29 -0500 Subject: [PATCH] repo-list: dedupe repos --- static/js/pages/repo-list.js | 39 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/static/js/pages/repo-list.js b/static/js/pages/repo-list.js index 4b9fdc7e3..74f33ae61 100644 --- a/static/js/pages/repo-list.js +++ b/static/js/pages/repo-list.js @@ -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() {