Skip to content

Commit

Permalink
Merge pull request #18 from M6Web/fix-statuses-priority
Browse files Browse the repository at this point in the history
Add priority on statuses for more wise status' color showing
  • Loading branch information
Florent Dubost committed Jun 8, 2015
2 parents 3802b6e + fb33378 commit ffef5f9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/json3/lib/json3.js"></script>
<script src="bower_components/lodash/lodash.js"></script>
<!-- endbower -->
<!-- endbuild -->

Expand Down
44 changes: 44 additions & 0 deletions app/scripts/controllers/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global _ */

'use strict';

angular.module('gtrApp')
Expand All @@ -12,9 +14,51 @@ angular.module('gtrApp')
$scope.descendingOrder = true;
}

var statePriorities = {
'error': 4,
'failure': 3,
'pending': 2,
'success': 1
};

/**
* Sort statuses by priority
*
* @param array statuses
*/
var sortStatuses = function (statuses) {

// Group statuses by their context
var statusesGrouped = _.groupBy(statuses, function (status) {
return status.context;
});

// Get first status of each context
statuses = _.toArray(statusesGrouped).map(function (statuses) {
return statuses[0];
});

// Sort statuses by state priority
statuses.sort(function (a, b) {
var aPriority = statePriorities[a.state];
var bPriority = statePriorities[b.state];

if (aPriority === bPriority) {
return 0;
}

return (aPriority > bPriority ? -1 : 1);
});

return statuses;
};

$scope.toArray = function (items) {
var array = [];
angular.forEach(items, function(item) {

item.statuses = sortStatuses(item.statuses);

array.push(item);
});

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"angular-route": "~1.3.0",
"json3": "~3.3.1",
"es5-shim": "~3.1.0",
"normalize-css": "~3.0.2"
"normalize-css": "~3.0.2",
"lodash": "~3.9.3"
},
"devDependencies": {
"angular-mocks": "~1.3.0"
Expand Down
21 changes: 17 additions & 4 deletions test/e2e/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,28 @@ describe('Test GTR screen', function () {
backend.whenGET('/api/v3/repos/m6web/service-polls/statuses/54').respond([]);
backend.whenGET('/api/v3/repos/m6web/service-polls/statuses/55').respond([]);
backend.whenGET('/api/v3/repos/m6web/service-polls/statuses/56').respond([{
'context': 'default',
'state': 'success'
}]);
backend.whenGET('/api/v3/repos/replay/bundle-polls-client/statuses/49').respond([{
'state': 'failure'
'context': 'default',
'state': 'success'
}, {
'context': 'foobar',
'state': 'pending'
}, {
'context': 'foobar',
'state': 'success'
}]);
backend.whenGET('/api/v3/repos/replay/bundle-polls-client/statuses/50').respond([{
'state': 'pending'
'context': 'default',
'state': 'failure'
}, {
'context': 'default',
'state': 'success'
}, {
'context': 'foobar',
'state': 'success'
}]);

// Others
Expand Down Expand Up @@ -247,7 +260,7 @@ describe('Test GTR screen', function () {
{
index: 1,
text: '#50 PR 50\nreplay/bundle-polls-client 10/08/2014',
class: 'pending',
class: 'failure',
avatar: 'http://example.com/karlouche.jpg',
pullUrl: 'http://example.com/replay/bundle-polls-client/pull/50'
},
Expand All @@ -261,7 +274,7 @@ describe('Test GTR screen', function () {
{
index: 3,
text: '#49 PR 49\nreplay/bundle-polls-client 28/07/2014',
class: 'failure',
class: 'pending',
avatar: 'http://example.com/bieber.jpg',
pullUrl: 'http://example.com/replay/bundle-polls-client/pull/49'
}
Expand Down

0 comments on commit ffef5f9

Please sign in to comment.