mirror of
https://github.com/rjNemo/devbook_ts
synced 2026-06-12 13:36:43 +00:00
fetch posts from db, can post and like posts
This commit is contained in:
parent
9b47db07dc
commit
7f28b6030a
2 changed files with 107 additions and 49 deletions
|
|
@ -5,10 +5,10 @@ import Comment from '../types/Comment';
|
||||||
*/
|
*/
|
||||||
interface Post {
|
interface Post {
|
||||||
id: string;
|
id: string;
|
||||||
userID: string;
|
userID?: string;
|
||||||
name: string;
|
name?: string;
|
||||||
text: string;
|
text: string;
|
||||||
avatarUrl: string;
|
avatarUrl?: string;
|
||||||
likes: string[];
|
likes: string[];
|
||||||
comments: Comment[];
|
comments: Comment[];
|
||||||
// date: Date;
|
// date: Date;
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,68 @@
|
||||||
import React, {FC} from 'react';
|
import React, {FC, useState} from 'react';
|
||||||
import Post, {dummyPost as post} from '../models/Post';
|
// Redux
|
||||||
import Header from '../components/Header';
|
import {compose} from '@reduxjs/toolkit';
|
||||||
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
import {connect, useSelector} from 'react-redux';
|
||||||
import {faThumbsUp, faThumbsDown} from '@fortawesome/free-solid-svg-icons';
|
import {firestoreConnect, WithFirestoreProps} from 'react-redux-firebase';
|
||||||
import Routes from '../constants/routes';
|
import {RootState} from '../store';
|
||||||
|
import {selectProfile} from '../store/firebase';
|
||||||
|
// Routing
|
||||||
import {Link} from 'react-router-dom';
|
import {Link} from 'react-router-dom';
|
||||||
|
import Routes from '../constants/routes';
|
||||||
|
// Style
|
||||||
|
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
||||||
|
import {faThumbsUp} from '@fortawesome/free-solid-svg-icons';
|
||||||
|
import Header from '../components/Header';
|
||||||
|
// Typing
|
||||||
|
import Post, {dummyPost as post} from '../models/Post';
|
||||||
|
import Collections from '../constants/collections';
|
||||||
|
|
||||||
|
interface IProps extends WithFirestoreProps {
|
||||||
|
posts: Post[];
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* A Dev's Posts list
|
* A Dev's Posts list
|
||||||
*/
|
*/
|
||||||
const Posts: FC = () => {
|
const Posts: FC<IProps> = ({posts, firestore, firebase}) => {
|
||||||
const posts: Post[] = [post, post];
|
const [text, setText] = useState('');
|
||||||
|
const {avatarUrl, displayName} = useSelector(selectProfile);
|
||||||
|
const id = firebase.auth().currentUser?.uid;
|
||||||
|
|
||||||
const removeLike = (e: React.MouseEvent<HTMLButtonElement>) =>
|
const newPost = {
|
||||||
new Error('Not implemented yet.');
|
userID: id,
|
||||||
const addLike = (e: React.MouseEvent<HTMLButtonElement>) =>
|
name: displayName,
|
||||||
new Error('Not implemented yet.');
|
text,
|
||||||
|
avatarUrl,
|
||||||
|
likes: [] as string[],
|
||||||
|
comments: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) =>
|
||||||
|
setText(e.target.value);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
firestore
|
||||||
|
.add(Collections.POSTS, newPost)
|
||||||
|
.then(res => {
|
||||||
|
console.log(res);
|
||||||
|
setText('');
|
||||||
|
})
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
};
|
||||||
|
// const removeLike = (e: React.MouseEvent<HTMLButtonElement>) =>
|
||||||
|
// new Error('Not implemented yet.');
|
||||||
|
const addLike = (postID: string) => (
|
||||||
|
e: React.MouseEvent<HTMLButtonElement>,
|
||||||
|
) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const post = posts.find(p => p.id === postID);
|
||||||
|
|
||||||
|
if (post) {
|
||||||
|
firestore
|
||||||
|
.update(`${Collections.POSTS}/${post.id}`, {likes: [...post.likes, id]})
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="container">
|
<section className="container">
|
||||||
|
|
@ -25,11 +72,18 @@ const Posts: FC = () => {
|
||||||
<h3>Say Something</h3>
|
<h3>Say Something</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form className="form my-1">
|
<form className="form my-1" onSubmit={handleSubmit}>
|
||||||
<textarea cols={30} rows={5} placeholder="Create a post"></textarea>
|
<textarea
|
||||||
|
cols={30}
|
||||||
|
rows={5}
|
||||||
|
placeholder="Create a post"
|
||||||
|
value={text}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
<input type="submit" value="Submit" className="btn btn-dark my-1" />
|
<input type="submit" value="Submit" className="btn btn-dark my-1" />
|
||||||
|
</form>
|
||||||
<div className="posts">
|
<div className="posts">
|
||||||
{posts.map((post: Post) => (
|
{posts?.map((post: Post) => (
|
||||||
<div className="post bg-white p-1 my-1" key={post.id}>
|
<div className="post bg-white p-1 my-1" key={post.id}>
|
||||||
<div>
|
<div>
|
||||||
<Link to={`${Routes.PROFILE}/${post.userID}`}>
|
<Link to={`${Routes.PROFILE}/${post.userID}`}>
|
||||||
|
|
@ -43,12 +97,12 @@ const Posts: FC = () => {
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p className="my-1">{post.text}</p>
|
<p className="my-1">{post.text}</p>
|
||||||
<button className="btn btn-light" onClick={addLike}>
|
<button className="btn btn-light" onClick={addLike(post.id)}>
|
||||||
<FontAwesomeIcon icon={faThumbsUp} /> {post.likes.length}
|
<FontAwesomeIcon icon={faThumbsUp} /> {post.likes?.length}
|
||||||
</button>
|
</button>
|
||||||
<button className="btn btn-light" onClick={removeLike}>
|
{/* <button className="btn btn-light" onClick={removeLike}>
|
||||||
<FontAwesomeIcon icon={faThumbsDown} />
|
<FontAwesomeIcon icon={faThumbsDown} />
|
||||||
</button>
|
</button> */}
|
||||||
<Link
|
<Link
|
||||||
to={`${Routes.POST}/${post.id}`}
|
to={`${Routes.POST}/${post.id}`}
|
||||||
className="btn btn-primary"
|
className="btn btn-primary"
|
||||||
|
|
@ -59,10 +113,14 @@ const Posts: FC = () => {
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Posts;
|
export default compose<FC>(
|
||||||
|
firestoreConnect(() => ['posts']), // or { collection: 'users' }
|
||||||
|
connect((state: RootState) => ({
|
||||||
|
posts: state.firestore.ordered.posts,
|
||||||
|
})),
|
||||||
|
)(Posts);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue