connect to redux store

This commit is contained in:
Ruidy Nemausat 2020-05-20 14:31:06 +02:00
parent 52680f0bdf
commit 5d1bd0f8f9

View file

@ -1,45 +1,60 @@
import React, {FC} from 'react'; import React, {FC} from 'react';
import Post, {dummyPost as post} from '../models/Post'; // Routing
import {useParams, Link} from 'react-router-dom';
// Redux
import {compose} from '@reduxjs/toolkit';
import {connect} from 'react-redux';
import {firestoreConnect} from 'react-redux-firebase';
import {RootState} from '../store';
import Collections from '../constants/collections';
// Typing
import Post from '../models/Post';
import Comment from '../types/Comment'; import Comment from '../types/Comment';
import Routes from '../constants/routes';
interface IProps {
post: Post;
}
/** /**
* Display a Post and the related comments. Shows a form to add a comment. * Display a Post and the related comments. Shows a form to add a comment.
*/ */
const PostPage: FC<Post> = () => ( const PostPage: FC<IProps> = ({post}) => {
<section className="container"> console.log(post);
<a href="posts.html" className="btn btn-light"> return (
Back To Posts <section className="container">
</a> <Link to={Routes.POSTS} className="btn btn-light">
Back To Posts
</Link>
<div className="post bg-white p-1 my-1"> <div className="post bg-white p-1 my-1">
<div> <div>
<a href="profile.html"> <Link to={`${Routes.PROFILE}/${post.userID}`}>
<img className="round-img" src={post.avatarUrl} alt={post.name} /> <img className="round-img" src={post.avatarUrl} alt={post.name} />
<h4>{post.name}</h4> <h4>{post.name}</h4>
</a> </Link>
</div>
<div>
<p className="my-1">{post.text}</p>
</div>
</div> </div>
<div>
<p className="my-1">{post.text}</p>
</div>
</div>
<div className="post-form"> <div className="post-form">
<div className="post-form-header bg-primary"> <div className="post-form-header bg-primary">
<h3>Leave A Comment</h3> <h3>Leave A Comment</h3>
</div>
<form className="form my-1">
<textarea
name="text"
cols={30}
rows={5}
placeholder="Comment on this post"
></textarea>
<input type="submit" className="btn btn-dark my-1" value="Submit" />
</form>
</div> </div>
<form className="form my-1">
<textarea
name="text"
cols={30}
rows={5}
placeholder="Comment on this post"
></textarea>
<input type="submit" className="btn btn-dark my-1" value="Submit" />
</form>
</div>
<div className="posts"> {/* <div className="posts">
{post.comments.map((c: Comment, i: number) => ( {post.comments?.map((c: Comment, i: number) => (
<div className="post bg-white p-1 my-1" key={i}> <div className="post bg-white p-1 my-1" key={i}>
<div> <div>
<a href="profile.html"> <a href="profile.html">
@ -52,8 +67,24 @@ const PostPage: FC<Post> = () => (
</div> </div>
</div> </div>
))} ))}
</div> </div> */}
</section> </section>
); );
};
export default PostPage; /**
* Container to fetch id params from thr URI and pass it to Profile page
*/
const PostPageContainer: FC = () => {
const {id} = useParams();
const Component = compose<FC>(
firestoreConnect(() => [`${Collections.POSTS}/${id}`]),
connect(({firestore: {data}}: RootState) => ({
post: data.posts && data.posts[id],
})),
)(PostPage);
return <Component />;
};
export default PostPageContainer;