Skip to content

Instantly share code, notes, and snippets.

@aviraldg
Last active April 6, 2016 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviraldg/9bf2180cf8ae9ec848fbdb44d48edcae to your computer and use it in GitHub Desktop.
Save aviraldg/9bf2180cf8ae9ec848fbdb44d48edcae to your computer and use it in GitHub Desktop.
import fetch from 'isomorphic-fetch';
import receivePlaces from './receivePlaces'
import requestPlaces from './requestPlaces'
export default function() {
return dispatch => {
dispatch(requestPlaces());
fetch(`https://www.mozzobytes.com/api/places/`)
.then(response => response.json())
.then(json => dispatch(receivePlaces(json.results)));
}
}
import {RECEIVE_PLACES} from './../const';
export default function(places) {
return { type: RECEIVE_PLACES, places};
}
import React, {
Component,
PropTypes
} from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import PlaceListComponent from '../components/PlaceListComponent';
class PlaceList extends Component {
componentDidMount() {
this.props.actions.fetchPlaces();
}
render() {
const {actions} = this.props;
return <PlaceListComponent />;
}
}
PlaceList.propTypes = {
actions: PropTypes.object.isRequired,
places: PropTypes.object.isRequired
};
function mapStateToProps(state) {
/* Populated by react-webpack-redux:reducer */
const props = {
places: state.places
};
return props;
}
function mapDispatchToProps(dispatch) {
/* Populated by react-webpack-redux:action */
const actions = {
fetchPlaces: require('../actions/places/fetchPlaces').default,
receivePlaces: require('../actions/places/receivePlaces').default,
requestPlaces: require('../actions/places/requestPlaces').default
};
const actionMap = { actions: bindActionCreators(actions, dispatch) };
return actionMap;
}
export default connect(mapStateToProps, mapDispatchToProps)(PlaceList);
/* Define your initial state here.
*
* If you change the type from object to something else, do not forget to update
* src/container/App.js accordingly.
*/
import {RECEIVE_PLACES} from '../actions/const'
const initialState = {
places: []
};
export default function(state = initialState, action) {
/* Keep the reducer clean - do not mutate the original state. */
let nextState = Object.assign({}, state);
console.error(action);
switch(action.type) {
case RECEIVE_PLACES: {
// Modify next state depending on the action and return it
nextState.places = action.places;
return nextState;
} break;
default: {
/* Return original state if no actions were consumed. */
return state;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment