From d1e52d3b81043a9feb1ff69dc0e632a1445955d3 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 5 May 2016 14:18:07 +0100 Subject: [PATCH] Redux build list --- app/actions/index.js | 15 +++++++++++++++ app/components/builds/BuildList.js | 27 +++++++++++++++++++++------ app/reducers/index.js | 4 +++- app/reducers/recentBuilds.js | 9 +++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 app/reducers/recentBuilds.js diff --git a/app/actions/index.js b/app/actions/index.js index 9f41565..0e075df 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -25,3 +25,18 @@ export function getProjects(callback) { { callback } ); } + + +export const GET_PROJECT_RECENT_BUILDS = makeAsyncActionSet('GET_PROJECT_RECENT_BUILDS'); +export function getProjectRecentBuilds(user, repo, limit, offset, callback) { + return buildAsyncAction( + GET_PROJECT_RECENT_BUILDS, + endpoints.get('PROJECT_RECENTS').param({ user, repo }).query({ limit, offset }), + methods.GET, + {}, + { + callback, + ident: `${user}/${repo}` + } + ); +} diff --git a/app/components/builds/BuildList.js b/app/components/builds/BuildList.js index 4772a9d..e8cedce 100644 --- a/app/components/builds/BuildList.js +++ b/app/components/builds/BuildList.js @@ -1,10 +1,11 @@ import React from 'react-native'; import GiftedListView from 'react-native-gifted-listview'; import GlobalStyles from '../../settings/styles'; +import { getProjectRecentBuilds } from '../../actions'; import loaderHandler from 'react-native-busy-indicator/LoaderHandler'; import BuildItem from './BuildItem'; import BuildItemEnd from './BuildItemEnd'; -import { getProjectRecentBuilds } from '../../api/CircleCI'; +import { connect } from 'react-redux'; import _ from 'underscore'; const { @@ -23,7 +24,7 @@ const styles = StyleSheet.create({ }, }); -export default class BuildList extends React.Component { +export class BuildList extends React.Component { constructor(props) { super(props); this._fetch = this._fetch.bind(this); @@ -41,14 +42,20 @@ export default class BuildList extends React.Component { _fetch(page = 1, callback, options) { const project = this.props.project; - getProjectRecentBuilds(project.username, project.reponame, this.renderLimit, this.renderLimit * (page - 1)) - .then((recentBuilds) => { + const actionCallback = function (recentBuilds) { recentBuilds = _.sortBy(recentBuilds, (b) => b.stop_time).reverse(); - loaderHandler.hideLoader(); callback(recentBuilds, { allLoaded: recentBuilds.length < this.renderLimit }); - }); + loaderHandler.hideLoader(); + }.bind(this); + this.props.getProjectRecentBuilds( + project.username, + project.reponame, + this.renderLimit, + this.renderLimit * (page - 1), + actionCallback + ); } render() { @@ -63,3 +70,11 @@ export default class BuildList extends React.Component { ); } }; + +function mapStateToProps(state) { + return { + recentBuilds: state.recentBuilds, + }; +} + +export default connect(mapStateToProps, { getProjectRecentBuilds }) (BuildList); diff --git a/app/reducers/index.js b/app/reducers/index.js index 37f684d..214d0e7 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -3,9 +3,11 @@ import * as storage from 'redux-storage'; import user from './user'; import allProjects from './allProjects'; +import recentBuilds from './recentBuilds'; export default storage.reducer(combineReducers({ user, - allProjects + allProjects, + recentBuilds })); diff --git a/app/reducers/recentBuilds.js b/app/reducers/recentBuilds.js new file mode 100644 index 0000000..16912a5 --- /dev/null +++ b/app/reducers/recentBuilds.js @@ -0,0 +1,9 @@ +import * as actions from '../actions'; +import { Map } from 'immutable'; + +export default function recentBuilds(state = Map(), action) { + if (action.type === actions.GET_PROJECT_RECENT_BUILDS.SUCCESS) { + return state.set(action.meta.ident, action.payload); // Not ideal! + } + return state; +};