From b6577eaba95f70e8ee347ce29eec4b3a3a887c14 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 5 May 2016 13:34:10 +0100 Subject: [PATCH] Convert allProjects page --- app/actions/index.js | 11 +++++++ app/components/projects/ProjectList.js | 41 +++++++++++++++----------- app/middleware/actionChain.js | 2 +- app/reducers/allProjects.js | 10 +++++++ app/reducers/index.js | 4 ++- 5 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 app/reducers/allProjects.js diff --git a/app/actions/index.js b/app/actions/index.js index 7668e75..0b3f6f1 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -14,3 +14,14 @@ export function login(token, callback) { { token, callback } ); } + +export const GET_PROJECTS = makeAsyncActionSet('GET_PROJECTS'); +export function getProjects(callback) { + return buildAsyncAction( + GET_PROJECTS, + endpoints.get('ALL_PROJECTS'), + methods.GET, + {}, + { callback } + ); +} diff --git a/app/components/projects/ProjectList.js b/app/components/projects/ProjectList.js index 87842f4..19820e3 100644 --- a/app/components/projects/ProjectList.js +++ b/app/components/projects/ProjectList.js @@ -1,7 +1,8 @@ import React from 'react-native'; import GiftedListView from 'react-native-gifted-listview'; import ProjectItem from './ProjectItem'; -import { getProjects, getUserDetails } from '../../api/CircleCI'; +import { connect } from 'react-redux'; +import { getProjects } from '../../actions'; import loaderHandler from 'react-native-busy-indicator/LoaderHandler'; const { @@ -20,23 +21,13 @@ const styles = StyleSheet.create({ }, }); -export default class ProjectList extends React.Component { +export class ProjectList extends React.Component { constructor(props) { super(props); - this.state = { - userDetails: false, - }; this.renderRow = this.renderRow.bind(this); this._fetch = this._fetch.bind(this); } - componentDidMount() { - getUserDetails().then(function(userDetails) { - this.setState({ userDetails }); - }.bind(this)); - } - - _sortByDate(projects) { return projects.sort(function (a, b) { const keyA = a.username + '/' + a.reponame; @@ -48,21 +39,24 @@ export default class ProjectList extends React.Component { } renderRow(project) { - return ; + return ; } _fetch(page, callback, options) { - getProjects().then((projects) => { - callback(this._sortByDate(projects)); + const actionCallback = function (data) { + this._sortByDate(data); + callback(data); loaderHandler.hideLoader(); - }); + }.bind(this); + this.props.getProjects(actionCallback); } render() { - if (!this.state.userDetails) { - return ; + if (!this.props.token) { + return null; // If there's no token, we can't do anything, so wait } + console.log('HAS TOKEN', this.props.userDetails.token); return ( next => action => { action.meta.callback && action.type.endsWith('SUCCESS') && typeof action.meta.callback === 'function') { - action.meta.callback(); + action.meta.callback(action.payload); } return next(action); }; diff --git a/app/reducers/allProjects.js b/app/reducers/allProjects.js new file mode 100644 index 0000000..98e0386 --- /dev/null +++ b/app/reducers/allProjects.js @@ -0,0 +1,10 @@ +import * as actions from '../actions'; +import { Map } from 'immutable'; + +export default function allProjects(state = Map(), action) { + console.log(action.type); + if (action.type === actions.GET_PROJECTS.SUCCESS) { + return Map(action.payload); + } + return state; +}; diff --git a/app/reducers/index.js b/app/reducers/index.js index 79943c3..37f684d 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -2,8 +2,10 @@ import { combineReducers } from 'redux'; import * as storage from 'redux-storage'; import user from './user'; +import allProjects from './allProjects'; export default storage.reducer(combineReducers({ - user + user, + allProjects }));