Skip to content

Commit

Permalink
load all planets from swapi
Browse files Browse the repository at this point in the history
  • Loading branch information
glenjamin committed May 31, 2015
1 parent 5880cbb commit 8bc48fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 16 additions & 11 deletions examples/react-iso/client/stores/planets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ var swapi = require('../swapi');
var handlers = exports.handlers = {};

handlers.planets = (query, dispatch, callback) => {
if (query === 'all') {
swapi({ pathname: 'planets/' }, (err, res, body) => {
if (err) {
dispatch("PLANETS_ERROR", err);
return callback();
}
dispatch("PLANETS_DATA", body);
return callback();
});
} else {
if (query !== 'all') {
return callback();
}
var planets = [];
swapi({ pathname: 'planets/' }, appendResults);
function appendResults(err, res, body) {
if (err) {
dispatch("PLANETS_ERROR", err);
return callback();
}
planets = planets.concat(body.results);
if (!body.next) {
dispatch("PLANETS_DATA", planets);
return callback();
}
swapi({ uri: body.next }, appendResults);
}
};
handlers.planet = (id, dispatch, callback) => {
swapi({ pathname: 'planets/' + id + '/' }, (err, res, body) => {
Expand All @@ -37,7 +42,7 @@ exports.store = flux.createStore(
}),
{
PLANETS_DATA(state, planets) {
state.planets = planets.results.map(formatPlanet);
state.planets = planets.map(formatPlanet);
return state;
},
PLANET_DATA(state, {id, planet}) {
Expand Down
4 changes: 3 additions & 1 deletion examples/react-iso/client/swapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var request = require('browser-request');
var swapiBaseUrl = 'http://swapi.co/api/';
var swapiRequest = request.defaults({ json: true });
function swapi(options, ...args) {
options.uri = swapiBaseUrl + options.pathname;
if (options.pathname) {
options.uri = swapiBaseUrl + options.pathname;
}
return swapiRequest(options, ...args);
}

Expand Down

0 comments on commit 8bc48fc

Please sign in to comment.