mirror of
https://github.com/rjNemo/devbook_ts
synced 2026-06-11 04:56:40 +00:00
can add comments
This commit is contained in:
parent
5d1bd0f8f9
commit
c62774c4d9
2 changed files with 45 additions and 25 deletions
|
|
@ -1,25 +1,46 @@
|
||||||
import React, {FC} from 'react';
|
import React, {FC} from 'react';
|
||||||
// Routing
|
// Routing
|
||||||
import {useParams, Link} from 'react-router-dom';
|
import {useParams, Link} from 'react-router-dom';
|
||||||
|
import Routes from '../constants/routes';
|
||||||
// Redux
|
// Redux
|
||||||
import {compose} from '@reduxjs/toolkit';
|
import {compose} from '@reduxjs/toolkit';
|
||||||
import {connect} from 'react-redux';
|
import {connect} from 'react-redux';
|
||||||
import {firestoreConnect} from 'react-redux-firebase';
|
import {firestoreConnect, WithFirestoreProps} from 'react-redux-firebase';
|
||||||
import {RootState} from '../store';
|
import {RootState} from '../store';
|
||||||
import Collections from '../constants/collections';
|
import Collections from '../constants/collections';
|
||||||
// Typing
|
// Typing
|
||||||
import Post from '../models/Post';
|
import Post from '../models/Post';
|
||||||
import Comment from '../types/Comment';
|
import Comment from '../types/Comment';
|
||||||
import Routes from '../constants/routes';
|
// Form
|
||||||
|
import useForm from '../hooks';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps extends WithFirestoreProps {
|
||||||
post: Post;
|
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<IProps> = ({post}) => {
|
const PostPage: FC<IProps> = ({post, firestore}) => {
|
||||||
console.log(post);
|
const newComment: Comment = {
|
||||||
|
name: post.name ?? 'error',
|
||||||
|
avatarUrl: post.avatarUrl ?? 'error',
|
||||||
|
text: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const {formData, handleChange, resetForm} = useForm(newComment);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
firestore
|
||||||
|
.update(`${Collections.POSTS}/${post.id}`, {
|
||||||
|
comments: [...post.comments, formData],
|
||||||
|
})
|
||||||
|
.then(() => resetForm())
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="container">
|
<section className="container">
|
||||||
<Link to={Routes.POSTS} className="btn btn-light">
|
<Link to={Routes.POSTS} className="btn btn-light">
|
||||||
|
|
@ -42,32 +63,34 @@ const PostPage: FC<IProps> = ({post}) => {
|
||||||
<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>
|
</div>
|
||||||
<form className="form my-1">
|
<form className="form my-1" onSubmit={handleSubmit}>
|
||||||
<textarea
|
<textarea
|
||||||
name="text"
|
name="text"
|
||||||
cols={30}
|
cols={30}
|
||||||
rows={5}
|
rows={5}
|
||||||
placeholder="Comment on this post"
|
placeholder="Comment on this post"
|
||||||
|
value={formData.text}
|
||||||
|
onChange={handleChange}
|
||||||
></textarea>
|
></textarea>
|
||||||
<input type="submit" className="btn btn-dark my-1" value="Submit" />
|
<input type="submit" className="btn btn-dark my-1" value="Submit" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</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">
|
||||||
<img className="round-img" src={c.avatarUrl} alt={c.name} />
|
<img className="round-img" src={c.avatarUrl} alt={c.name} />
|
||||||
<h4>{c.name}</h4>
|
<h4>{c.name}</h4>
|
||||||
</a>
|
</a>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="my-1">{c.text}</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
))}
|
||||||
<p className="my-1">{c.text}</p>
|
</div>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div> */}
|
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
@ -80,7 +103,7 @@ const PostPageContainer: FC = () => {
|
||||||
const Component = compose<FC>(
|
const Component = compose<FC>(
|
||||||
firestoreConnect(() => [`${Collections.POSTS}/${id}`]),
|
firestoreConnect(() => [`${Collections.POSTS}/${id}`]),
|
||||||
connect(({firestore: {data}}: RootState) => ({
|
connect(({firestore: {data}}: RootState) => ({
|
||||||
post: data.posts && data.posts[id],
|
post: data.posts && {...data.posts[id], id},
|
||||||
})),
|
})),
|
||||||
)(PostPage);
|
)(PostPage);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,10 +43,7 @@ const Posts: FC<IProps> = ({posts, firestore, firebase}) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
firestore
|
firestore
|
||||||
.add(Collections.POSTS, newPost)
|
.add(Collections.POSTS, newPost)
|
||||||
.then(res => {
|
.then(() => setText(''))
|
||||||
console.log(res);
|
|
||||||
setText('');
|
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
.catch(err => console.error(err));
|
||||||
};
|
};
|
||||||
// const removeLike = (e: React.MouseEvent<HTMLButtonElement>) =>
|
// const removeLike = (e: React.MouseEvent<HTMLButtonElement>) =>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue