Last active
April 6, 2016 04:14
-
-
Save aviraldg/9bf2180cf8ae9ec848fbdb44d48edcae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {RECEIVE_PLACES} from './../const'; | |
export default function(places) { | |
return { type: RECEIVE_PLACES, places}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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