-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPost.tsx
More file actions
67 lines (58 loc) · 1.56 KB
/
Copy pathPost.tsx
File metadata and controls
67 lines (58 loc) · 1.56 KB
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
import { useEffect, useState } from "react"
import { db, auth } from "../firebase";
import {
doc,
updateDoc,
deleteDoc,
serverTimestamp,
} from "firebase/firestore"
import type { Post } from "../pages/Dashboard"
import { postsRef } from "../pages/Dashboard";
async function updatePost(postId: string, newText: string) {
if (!auth.currentUser) return
const ref = doc(postsRef, postId)
await updateDoc(ref, {
text: newText,
updatedAt: serverTimestamp(),
})
}
async function deletePost(postId: string) {
if (!auth.currentUser) return
await deleteDoc(doc(postsRef, postId))
}
export function PostItem({ post }: { post: Post }) {
const [editing, setEditing] = useState(false)
const [value, setValue] = useState(post.text)
const isOwner = auth.currentUser?.uid === post.authorId
return (
<div style={{ marginBottom: "1rem" }}>
<strong>{post.authorId.slice(0, 6)}</strong>
{editing ? (
<>
<textarea value={value} onChange={e => setValue(e.target.value)} />
<button
onClick={() => {
updatePost(post.id!, value)
setEditing(false)
}}
>
Save
</button>
</>
) : (
<div>{post.text}</div>
)}
{isOwner && !editing && (
<>
<button onClick={() => setEditing(true)}>Edit</button>
<button
onClick={() => deletePost(post.id!)}
style={{ marginLeft: "0.5rem", color: "red" }}
>
Delete
</button>
</>
)}
</div>
)
}