Created
May 13, 2017 20:22
-
-
Save andru1989/d7c66b7d889ff563fba15308cda8b86a 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 axios from 'axios'; | |
export const FETCH_POSTS = 'FETCH_POSTS'; | |
export const CREATE_POST = 'CREATE_POST'; | |
export const FETCH_POST = 'FETCH_POST'; | |
const ROOT_URL = 'http://reduxblog.herokuapp.com/api'; | |
const API_KEY = '?key=andru1989'; | |
export function fetchPosts() { | |
const request = axios.get(`${ROOT_URL}/posts${API_KEY}`); | |
return { | |
type: FETCH_POSTS, | |
payload: request | |
}; | |
} | |
export function createPost(values, callback) { | |
const request = axios.post(`${ROOT_URL}/posts${API_KEY}`, values) | |
.then(() => callback()); | |
return { | |
type: CREATE_POST, | |
payload: request | |
}; | |
} | |
export function fetchPost(id) { | |
const request = axios.get(`${ROOT_URL}/posts/${id}${API_KEY}`); | |
return { | |
type: FETCH_POST, | |
payload: request | |
}; | |
} |
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 } from 'react'; | |
import { connect } from 'react-redux'; | |
import { fetchPost } from '../actions'; | |
class PostsShow extends Component { | |
componentDidMount() { | |
const { id } = this.props.match.params; // from react-router-dom | |
this.props.fetchPost(id); | |
} | |
render() { | |
const { post } = this.props; // from mapStateToProps | |
if (!post) { | |
return <div>Loading...</div>; | |
} | |
return ( | |
<div> | |
<h3>{post.title}</h3> | |
<h6>Categories: {post.categories}</h6> | |
<p>{post.content}</p> | |
</div> | |
); | |
} | |
} | |
function mapStateToProps({ posts }, ownProps) { // ownProps === this(PostsShow).props | |
return { post: posts[ownProps.match.params.id] }; | |
} | |
export default connect(mapStateToProps, { fetchPost })(PostsShow); |
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 from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { Provider } from 'react-redux'; | |
import { createStore, applyMiddleware } from 'redux'; | |
// Switch searches the first Route component path match with current browser route | |
import { BrowserRouter, Route, Switch } from 'react-router-dom'; | |
import promise from 'redux-promise'; | |
import reducers from './reducers'; | |
import PostsIndex from './components/posts_index'; | |
import PostsNew from './components/posts_new'; | |
import PostsShow from './components/posts_show'; | |
const createStoreWithMiddleware = applyMiddleware(promise)(createStore); | |
ReactDOM.render( | |
<Provider store={createStoreWithMiddleware(reducers)}> | |
<BrowserRouter> | |
<div> | |
<Switch> | |
<Route path="/posts/new" component={PostsNew} /> | |
<Route path="/posts/:id" component={PostsShow} /> | |
<Route path="/" component={PostsIndex} /> | |
</Switch> | |
</div> | |
</BrowserRouter> | |
</Provider> | |
, document.querySelector('.container')); |
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 _ from 'lodash'; | |
import { FETCH_POSTS, FETCH_POST } from '../actions'; | |
export default function(state = {}, action) { | |
switch (action.type) { | |
case FETCH_POST: | |
// const post = ation.payload.data; | |
// const newState = { ...state }; | |
// newState[post.id] = post; | |
// return newState; | |
return { ...state, [action.payload.id]: action.payload.data }; | |
case FETCH_POSTS: | |
return _.mapKeys(action.payload.data, 'id'); | |
// Gets an array and creates and object => {"4":{"id":4,"title":"hi"},"36":{"id":36,"title":"hey"}} | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment