diff --git a/src/models/Post.ts b/src/models/Post.ts index 0c7dea9..5b64ba8 100644 --- a/src/models/Post.ts +++ b/src/models/Post.ts @@ -5,10 +5,10 @@ import Comment from '../types/Comment'; */ interface Post { id: string; - userID: string; - name: string; + userID?: string; + name?: string; text: string; - avatarUrl: string; + avatarUrl?: string; likes: string[]; comments: Comment[]; // date: Date; diff --git a/src/pages/Posts.tsx b/src/pages/Posts.tsx index 0817897..a2e03d6 100644 --- a/src/pages/Posts.tsx +++ b/src/pages/Posts.tsx @@ -1,21 +1,68 @@ -import React, {FC} from 'react'; -import Post, {dummyPost as post} from '../models/Post'; -import Header from '../components/Header'; -import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; -import {faThumbsUp, faThumbsDown} from '@fortawesome/free-solid-svg-icons'; -import Routes from '../constants/routes'; +import React, {FC, useState} from 'react'; +// Redux +import {compose} from '@reduxjs/toolkit'; +import {connect, useSelector} from 'react-redux'; +import {firestoreConnect, WithFirestoreProps} from 'react-redux-firebase'; +import {RootState} from '../store'; +import {selectProfile} from '../store/firebase'; +// Routing 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 */ -const Posts: FC = () => { - const posts: Post[] = [post, post]; +const Posts: FC = ({posts, firestore, firebase}) => { + const [text, setText] = useState(''); + const {avatarUrl, displayName} = useSelector(selectProfile); + const id = firebase.auth().currentUser?.uid; - const removeLike = (e: React.MouseEvent) => - new Error('Not implemented yet.'); - const addLike = (e: React.MouseEvent) => - new Error('Not implemented yet.'); + const newPost = { + userID: id, + name: displayName, + text, + avatarUrl, + likes: [] as string[], + comments: [], + }; + + const handleChange = (e: React.ChangeEvent) => + 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) => + // new Error('Not implemented yet.'); + const addLike = (postID: string) => ( + e: React.MouseEvent, + ) => { + 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 (
@@ -25,44 +72,55 @@ const Posts: FC = () => {

Say Something

-
- + +