-
Notifications
You must be signed in to change notification settings - Fork 1
/
useUserProjects.tsx
34 lines (28 loc) · 859 Bytes
/
useUserProjects.tsx
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
import * as React from "react"
import db from "utils/firestore"
import * as Types from "types"
export default function useUserProjects(uid: string) {
const [status, setStatus] = React.useState<"error" | "loading" | "ready">(
"loading",
)
const [projects, setProjects] = React.useState<Types.ProjectData[]>([])
React.useEffect(() => {
const ref = db.collection("users").doc(uid).collection("projects")
ref.get().then((snapshot) => {
setStatus("ready")
setProjects(
snapshot.docs.map(
(doc) => ({ id: doc.id, ...doc.data() } as Types.ProjectData),
),
)
})
return ref.onSnapshot((snapshot) => {
setProjects(
snapshot.docs.map(
(doc) => ({ id: doc.id, ...doc.data() } as Types.ProjectData),
),
)
})
}, [uid])
return { status, projects }
}