This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
Sphere/app/components/projects/ProjectList.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-02-21 12:05:14 +00:00
import React from 'react-native';
import GiftedListView from 'react-native-gifted-listview';
2016-02-21 12:05:14 +00:00
import ProjectItem from './ProjectItem';
import { getProjects, getUserDetails } from '../../api/CircleCI';
import loaderHandler from 'react-native-busy-indicator/LoaderHandler';
2016-05-07 15:28:32 +01:00
import _ from 'underscore';
import sortObject from '../../helpers/sort-object';
2016-02-21 12:05:14 +00:00
2016-02-23 23:12:32 +00:00
const {
2016-02-21 12:05:14 +00:00
StyleSheet,
View
2016-02-21 12:05:14 +00:00
} = React;
2016-02-23 23:12:32 +00:00
const styles = StyleSheet.create({
listView: {
2016-02-21 12:05:14 +00:00
flex: 1,
2016-02-23 23:12:32 +00:00
flexDirection: 'column',
alignItems: 'stretch',
2016-02-21 12:05:14 +00:00
justifyContent: 'center',
flexWrap: 'wrap',
2016-02-24 18:00:13 +00:00
paddingHorizontal: 5
2016-02-21 12:05:14 +00:00
},
});
export default class ProjectList extends React.Component {
constructor(props) {
super(props);
this.state = {
userDetails: false,
2016-02-21 12:05:14 +00:00
};
this.renderRow = this.renderRow.bind(this);
this._fetch = this._fetch.bind(this);
}
2016-05-07 15:28:32 +01:00
componentWillMount() {
getUserDetails().then(function(userDetails) {
this.setState({ userDetails });
}.bind(this));
2016-02-21 12:05:14 +00:00
}
2016-02-23 23:12:32 +00:00
_sortByDate(projects) {
2016-05-07 15:28:32 +01:00
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();
2016-02-23 23:12:32 +00:00
}
2016-02-21 12:05:14 +00:00
renderRow(project) {
return <ProjectItem project={project} userDetails={this.state.userDetails} nav={this.props.nav} />;
}
_fetch(page, callback, options) {
2016-03-13 20:52:25 +00:00
getProjects().then((projects) => {
callback(this._sortByDate(projects));
loaderHandler.hideLoader();
});
2016-02-21 12:05:14 +00:00
}
render() {
if (!this.state.userDetails) {
return <View />;
}
2016-02-21 12:05:14 +00:00
return (
<View style={styles.listView}>
<GiftedListView
rowView={this.renderRow}
refreshable={true}
pagination={false}
onFetch={this._fetch} />
</View>
2016-02-21 12:05:14 +00:00
);
}
};