9.07 Making Post Selectable
FullPost.js
FullPost.js
import React, { Component } from "react";
import "./FullPost.css";
class FullPost extends Component {
render() {
let post = <p style={{ textAlign: "center" }}>Please select a Post!</p>;
if (this.props.id) {
post = (
<div className="FullPost">
<h1>Title</h1>
<p>Content</p>
<div className="Edit">
<button className="Delete">Delete</button>
</div>
</div>
);
}
return post;
}
}
export default FullPost;
Blog.js
Blog.js
import axios from "axios";
import React, { Component } from "react";
import FullPost from "../../components/FullPost/FullPost";
import NewPost from "../../components/NewPost/NewPost";
import Post from "../../components/Post/Post";
import "./Blog.css";
class Blog extends Component {
state = {
posts: [],
selectedPostId: null,
};
componentDidMount() {
axios.get("https://jsonplaceholder.typicode.com/posts").then((response) => {
const posts = response.data.slice(0, 4);
const updatedPosts = posts.map((post) => {
return {
...post,
author: "Pash",
};
});
this.setState({ posts: updatedPosts });
// console.log(response);
});
}
postSelectedHandler = (id) => {
this.setState({ selectedPostId: id });
};
render() {
const posts = this.state.posts.map((post) => {
return (
<Post
key={post.id}
title={post.title}
author={post.author}
clicked={() => this.postSelectedHandler(post.id)}
/>
);
});
return (
<div>
<section className="Posts">{posts}</section>
<section>
<FullPost id={this.state.selectedPostId} />
</section>
<section>
<NewPost />
</section>
</div>
);
}
}
export default Blog;
Post.js
Post.js
import React from "react";
import "./Post.css";
const post = (props) => (
<article className="Post" onClick={props.clicked}>
<h1>{props.title}</h1>
<div className="Info">
<div className="Author">{props.author}</div>
</div>
</article>
);
export default post;