1
0
Fork 0

It makes more sense now

master
Amarpreet Minhas 5 years ago
parent dc33eed88b
commit 922299010c
  1. 34
      src/components/Post.js
  2. 8
      src/reducers/postsReducer.js

@ -5,23 +5,37 @@ import { fetchPost } from '../actions';
import { Link } from 'react-router-dom';
class Post extends React.Component {
constructor (props) {
super(props)
this.state = {
isLoading: true
}
}
componentDidMount() {
const { slug } = this.props.match.params
this.props.fetchPost(slug);
this.props.fetchPost(slug)
.then(() => this.setState({isLoading: false}))
}
render () {
console.log(this.props.posts)
return (
<div className="item" key="{post.id}">
<div className="content">
<div className="description">
<h1><b><u><Link to={"/posts/"}>""</Link></u></b></h1>
<ReactMarkdown source="{post.content}" />
const {posts} = this.props
const {isLoading} = this.state
if (isLoading) {
return <p>Loading</p>
} else {
const post = posts.entities[posts.currentId]
return (
<div className="item" key={post.id}>
<div className="content">
<div className="description">
<h1><b><u><Link to={"/posts/" + post.slug}>{post.title}</Link></u></b></h1>
<ReactMarkdown source={post.content} />
</div>
</div>
</div>
</div>
);
);
}
}
}

@ -1,5 +1,6 @@
const initialState = {
entities: {},
currentId: ""
};
const normalizeEntities = (entities, payloadData) => {
@ -9,12 +10,15 @@ const normalizeEntities = (entities, payloadData) => {
}
export default (state = initialState, action) => {
let mergedEntities = {}
switch (action.type) {
case 'FETCH_POSTS':
const mergedEntities = normalizeEntities(state.entities, action.payload)
mergedEntities = normalizeEntities(state.entities, action.payload)
return {...state, ...{entities: mergedEntities}}
case 'FETCH_POST':
return action.payload;
mergedEntities = normalizeEntities(state.entities, [action.payload])
return {...state, ...{entities: mergedEntities, currentId: action.payload.id}}
default:
return state;
}