forked from ReactTraining/hooks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPosts.js
113 lines (103 loc) · 3.3 KB
/
Posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useRef, useState, useEffect } from "react"
import { Link } from "app/packages/react-router-next"
import { format as formatDate } from "date-fns"
import { useAppState } from "app/app-state"
import useDocWithCache from "app/useDocWithCache"
import Avatar from "app/Avatar"
import NewPost from "app/NewPost"
import { FaPlusCircle, FaTrashAlt } from "react-icons/fa"
import AnimatedText from "app/AnimatedText"
import { deletePost } from "app/utils"
import usePosts from "app/usePosts"
export default function Posts({ params }) {
// passed params as props because animations can't "retain" context
const [{ auth }] = useAppState()
const { uid, date } = params
const user = useDocWithCache(`users/${uid}`)
const [adding, setAdding] = useState(false)
const [newPostId, setNewPostId] = useState(null)
const addRef = useRef()
const posts = usePosts(uid, { listen: !adding })
const canAdd = auth.uid === uid
const handleAddNew = () => setAdding(true)
const handleNewPostSuccess = post => {
setAdding(false)
setNewPostId(post.id)
}
useEffect(() => {
if (!adding && addRef.current) {
addRef.current.focus()
}
}, [adding])
const dayPosts = posts && posts.filter(post => post.date === date)
return posts && user ? (
<div className="Posts">
<Avatar uid={user.uid} size={100} />
<div className="Posts_content">
<h1 className="Posts_user_name">
<Link href={`/${user.uid}`}>{user.displayName}</Link>
</h1>
<h2 className="Posts_date">{formatDate(date, "MMM Do, YYYY")}</h2>
<div className="Posts_posts">
{dayPosts.length > 0 ? (
dayPosts.map((post, index) => (
<Post key={post.id} post={post} isNew={post.id === newPostId} />
))
) : (
<div className="Posts_empty">No posts today.</div>
)}
</div>
{canAdd &&
(adding ? (
<div className="Posts_adding">
<NewPost
takeFocus={adding}
date={date}
onSuccess={handleNewPostSuccess}
showAvatar={false}
/>
</div>
) : (
<div className="Posts_add">
<button
ref={addRef}
className="Posts_add_button icon_button"
onClick={handleAddNew}
>
<FaPlusCircle />{" "}
<span>{posts.length > 0 ? "Add another" : "Add one"}</span>
</button>
</div>
))}
</div>
</div>
) : null
}
function Post({ post, isNew }) {
const [{ auth }] = useAppState()
const canDelete = auth.uid === post.uid
const handleDelete = () => deletePost(post.id)
return (
<div className="Post">
<div className="Post_title">
<div className="Post_minutes">
{isNew ? (
<AnimatedText children={post.minutes} />
) : (
<span>{post.minutes}</span>
)}{" "}
Minutes
</div>
{canDelete && (
<button
className="Post_delete_button icon_button"
onClick={handleDelete}
>
<FaTrashAlt /> <span>Delete</span>
</button>
)}
</div>
<div className="Post_message">{post.message}</div>
</div>
)
}