From d259c45b502e4fe590a6f13a799642198fb9e818 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 7 May 2016 15:28:32 +0100 Subject: [PATCH] Sort better --- app/components/projects/ProjectList.js | 19 ++++++++++--------- app/helpers/sort-object.js | 10 ++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 app/helpers/sort-object.js diff --git a/app/components/projects/ProjectList.js b/app/components/projects/ProjectList.js index 87842f4..9bee519 100644 --- a/app/components/projects/ProjectList.js +++ b/app/components/projects/ProjectList.js @@ -3,6 +3,8 @@ import GiftedListView from 'react-native-gifted-listview'; import ProjectItem from './ProjectItem'; import { getProjects, getUserDetails } from '../../api/CircleCI'; import loaderHandler from 'react-native-busy-indicator/LoaderHandler'; +import _ from 'underscore'; +import sortObject from '../../helpers/sort-object'; const { StyleSheet, @@ -30,21 +32,20 @@ export default class ProjectList extends React.Component { this._fetch = this._fetch.bind(this); } - componentDidMount() { + componentWillMount() { getUserDetails().then(function(userDetails) { this.setState({ userDetails }); }.bind(this)); } - _sortByDate(projects) { - return projects.sort(function (a, b) { - const keyA = a.username + '/' + a.reponame; - const keyB = b.reponame + '/' + b.username; - if (keyA < keyB) { return -1; } - if (keyA > keyB) { return 1; } - return 0; - }.bind(this)); + return _.sortBy(projects, function (project) { + let mostRecentBranch = sortObject(project.branches, function (branch) { + return branch.recent_builds[0].pushed_at; + }); + mostRecentBranch = mostRecentBranch[Object.keys(mostRecentBranch)[0]]; + return mostRecentBranch.recent_builds[0].pushed_at; + }).reverse(); } renderRow(project) { diff --git a/app/helpers/sort-object.js b/app/helpers/sort-object.js new file mode 100644 index 0000000..d2deeb5 --- /dev/null +++ b/app/helpers/sort-object.js @@ -0,0 +1,10 @@ +import _ from 'underscore'; + +export default function sortObject(obj, comparator) { + let keys = _.sortBy(_.keys(obj), function (key) { + return comparator ? comparator(obj[key], key) : key; + }); + return _.object(keys, _.map(keys, function (key) { + return obj[key]; + })); +}