Add user posts to their pages
This commit is contained in:
parent
587b3a26c1
commit
9ab288daee
4 changed files with 36 additions and 15 deletions
|
@ -11,11 +11,9 @@ export const fetchPost = (slug) => async (dispatch) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const fetchUserProfile = (user) => async (dispatch) => {
|
export const fetchUserProfile = (user) => async (dispatch) => {
|
||||||
const response = await sudoscientist.get('/users/' + user);
|
const profile = await sudoscientist.get('/users/' + user);
|
||||||
dispatch({ type: 'FETCH_USER_PROFILE', payload: response.data })
|
const userPosts = await sudoscientist.get('/blog/by-author/' + user);
|
||||||
};
|
const test = {posts: userPosts.data}
|
||||||
|
const response = { ...profile.data, ...test }
|
||||||
export const fetchUserPosts = (user) => async (dispatch) => {
|
dispatch({ type: 'FETCH_USER', payload: response })
|
||||||
const response = await sudoscientist.get('/blog/by-author/' + user);
|
|
||||||
dispatch({ type: 'FETCH_USER_POSTS', payload: response.data })
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { connect } from 'react-redux';
|
||||||
import { fetchPosts } from '../actions';
|
import { fetchPosts } from '../actions';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
|
|
||||||
class PostList extends React.Component {
|
class PostList extends React.Component {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.fetchPosts();
|
this.props.fetchPosts();
|
||||||
|
@ -15,16 +16,17 @@ class PostList extends React.Component {
|
||||||
const post = posts.entities[id]
|
const post = posts.entities[id]
|
||||||
return (
|
return (
|
||||||
<div className="item" key={post.id}>
|
<div className="item" key={post.id}>
|
||||||
|
<h1>Blog Posts</h1>
|
||||||
<div className="content">
|
<div className="content">
|
||||||
<div className="description">
|
<div className="description">
|
||||||
<h1><b><u><Link to={"/posts/" + post.slug}>{post.title}</Link></u></b></h1>
|
<h2><b><u><Link to={"/posts/" + post.slug}>{post.title}</Link></u></b></h2>
|
||||||
<h3><b>By <Link to={"/users/"+ post.author}>{post.author}</Link></b></h3>
|
<h3><b>By <Link to={"/users/"+ post.author}>{post.author}</Link></b></h3>
|
||||||
<ReactMarkdown source={post.content} />
|
<ReactMarkdown source={post.content} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
}).reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import ReactMarkdown from 'react-markdown';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { fetchUserProfile } from '../actions';
|
import { fetchUserProfile } from '../actions';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
class User extends React.Component {
|
class User extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
|
@ -14,6 +16,22 @@ class User extends React.Component {
|
||||||
this.props.fetchUserProfile(user)
|
this.props.fetchUserProfile(user)
|
||||||
.then(() => this.setState({isLoading: false}))
|
.then(() => this.setState({isLoading: false}))
|
||||||
}
|
}
|
||||||
|
renderList() {
|
||||||
|
const posts = this.props.user.currentUser.posts
|
||||||
|
return posts.map(post => {
|
||||||
|
return (
|
||||||
|
<div className="item" key={post.id}>
|
||||||
|
<div className="content">
|
||||||
|
<div className="description">
|
||||||
|
<h2><b><u><Link to={"/posts/" + post.slug}>{post.title}</Link></u></b></h2>
|
||||||
|
<h3><b>By <Link to={"/users/"+ post.author}>{post.author}</Link></b></h3>
|
||||||
|
<ReactMarkdown source={post.content} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const {user} = this.props
|
const {user} = this.props
|
||||||
|
@ -24,13 +42,16 @@ class User extends React.Component {
|
||||||
} else {
|
} else {
|
||||||
const currentUser = user.currentUser
|
const currentUser = user.currentUser
|
||||||
return (
|
return (
|
||||||
<div className="item" key={currentUser.username}>
|
|
||||||
<div className="content">
|
<div className="content">
|
||||||
|
<div className="item" key={currentUser.username}>
|
||||||
<div className="description">
|
<div className="description">
|
||||||
<h1><b>{currentUser.username}</b></h1>
|
<h1><b>This is the page of: {currentUser.username}</b></h1>
|
||||||
<h3><u>Email: {currentUser.email}</u></h3>
|
<h4><u>Email: {currentUser.email}</u></h4>
|
||||||
<div>Location: {currentUser.location}</div>
|
<div>Location: {currentUser.location}</div>
|
||||||
<div>About Me: {currentUser.bio}</div>
|
<div>About Me: {currentUser.bio}</div>
|
||||||
|
<div className="ui relaxed divided list">
|
||||||
|
{this.renderList()}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const initialState = {
|
const initialState = {
|
||||||
currentUser: ""
|
currentUser: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (state = initialState, action) => {
|
export default (state = initialState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'FETCH_USER_PROFILE':
|
case 'FETCH_USER':
|
||||||
return {currentUser: action.payload}
|
return {currentUser: action.payload}
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
|
|
Reference in a new issue